-
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.
Signed-off-by: Lou Marvin Caraig <[email protected]>
- Loading branch information
1 parent
e8d6568
commit b342ff6
Showing
1 changed file
with
64 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,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/se7entyse7en/pydockenv/internal/dependency" | ||
"github.com/se7entyse7en/pydockenv/log" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var installCmd = &cobra.Command{ | ||
Use: "install [package_1] [package_2] ... [package_n]", | ||
Short: "Install packages from args or from a requirements file", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
logger := log.Logger | ||
err := log.SetupLogger(cmd) | ||
if err != nil { | ||
logger.WithError(err).Fatal("Cannot setup logger") | ||
} | ||
|
||
cmdArgs, err := parseInstallArgs(cmd, args) | ||
if err != nil { | ||
logger.WithError(err).Fatal("Cannot parse arguments") | ||
} | ||
|
||
ctxLogger := logger.WithFields(logrus.Fields{ | ||
"packages": cmdArgs.Packages, | ||
"file": cmdArgs.RequirementsFile, | ||
}) | ||
ctxLogger.Info("Installing packages...") | ||
|
||
if err := dependency.Install(&dependency.Requirements{ | ||
FileName: cmdArgs.RequirementsFile, | ||
Packages: dependency.Packages{RawDependencies: cmdArgs.Packages}, | ||
}); err != nil { | ||
logger.WithError(err).Fatal("Cannot install packages") | ||
} | ||
|
||
ctxLogger.Info("Packages installed!") | ||
}, | ||
} | ||
|
||
func parseInstallArgs(cmd *cobra.Command, args []string) (*installArgs, error) { | ||
requirementsFile, err := getStringArg(cmd, "file") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &installArgs{ | ||
Packages: args, | ||
RequirementsFile: requirementsFile, | ||
}, nil | ||
} | ||
|
||
type installArgs struct { | ||
Packages []string | ||
RequirementsFile string | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(installCmd) | ||
|
||
installCmd.Flags().StringP("file", "f", "", "Requirements file") | ||
} |