-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dependency package to handle installation of packages inside cont…
…ainers Signed-off-by: Lou Marvin Caraig <[email protected]>
- Loading branch information
1 parent
2430b37
commit 01b5629
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |