Skip to content

Commit

Permalink
Add dependency package to handle installation of packages inside cont…
Browse files Browse the repository at this point in the history
…ainers

Signed-off-by: Lou Marvin Caraig <[email protected]>
  • Loading branch information
se7entyse7en committed Jun 6, 2020
1 parent 2430b37 commit 01b5629
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions internal/dependency/dependency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package dependency

import (
"fmt"

"github.com/docker/docker/api/types"
"github.com/se7entyse7en/pydockenv/internal/executor"
)

type Requirements struct {
FileName string
Packages
}

type Packages struct {
Dependencies map[string]string
RawDependencies []string
}

func Install(requirements *Requirements) error {
cmd := buildInstallCmd(requirements)
err := executor.Execute(cmd, &executor.ExecOptions{})
if err != nil {
return fmt.Errorf("cannot install requirements in container: %w", err)
}

return nil
}

func InstallForContainer(container types.ContainerJSON, requirements *Requirements) error {
cmd := buildInstallCmd(requirements)
err := executor.ExecuteForContainer(container, cmd,
&executor.ExecOptions{ByPassCheck: true})
if err != nil {
return fmt.Errorf("cannot install requirements in container: %w", err)
}

return nil
}

func Uninstall(packages *Packages, yes bool) error {
cmd := []string{"pip", "uninstall"}
cmd = append(cmd, parsePackages(packages)...)
if yes {
cmd = append(cmd, "-y")
}

err := executor.Execute(cmd, &executor.ExecOptions{})
if err != nil {
return fmt.Errorf("cannot uninstall requirements in container: %w", err)
}

return nil
}

func buildInstallCmd(requirements *Requirements) []string {
cmd := []string{"pip", "install"}
if requirements.FileName != "" {
cmd = append(cmd, "-r", requirements.FileName)
} else {
cmd = append(cmd, parsePackages(&requirements.Packages)...)
}

return cmd
}

func parsePackages(packages *Packages) []string {
if len(packages.RawDependencies) > 0 {
return packages.RawDependencies
}

var parsedPackages []string
for p, v := range packages.Dependencies {
parsedPackages = append(parsedPackages, fmt.Sprintf("%s%s", p, v))
}

return parsedPackages
}

0 comments on commit 01b5629

Please sign in to comment.