Skip to content

Commit

Permalink
Add install command
Browse files Browse the repository at this point in the history
Signed-off-by: Lou Marvin Caraig <[email protected]>
  • Loading branch information
se7entyse7en committed Jun 27, 2020
1 parent e8d6568 commit b342ff6
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cmd/install.go
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")
}

0 comments on commit b342ff6

Please sign in to comment.