diff --git a/cmd/packman/controllers/render.go b/cmd/packman/controllers/render.go index 87c2d16..c7069e8 100644 --- a/cmd/packman/controllers/render.go +++ b/cmd/packman/controllers/render.go @@ -3,6 +3,7 @@ package controllers import ( "fmt" "github.com/securenative/packman/internal" + "github.com/securenative/packman/internal/etc" "github.com/urfave/cli" ) @@ -17,16 +18,12 @@ var RenderController = cli.Command{ } func render(c *cli.Context) error { - if c.NArg() != 1 { + if c.NArg() < 1 { return fmt.Errorf("unpack expects exactly 1 argument but got %d arguments", c.NArg()) } path := c.Args().Get(0) - flagsMap := make(map[string]string) - - for _, flagName := range c.FlagNames() { - flagsMap[flagName] = c.String(flagName) - } + flagsMap := etc.ArgsToFlagsMap(c.Args()[1:]) return internal.M.TemplatingService.Render(path, fmt.Sprintf("%s-rendered", path), flagsMap) } diff --git a/cmd/packman/controllers/unpack.go b/cmd/packman/controllers/unpack.go index 9837f0e..7090faf 100644 --- a/cmd/packman/controllers/unpack.go +++ b/cmd/packman/controllers/unpack.go @@ -3,13 +3,14 @@ package controllers import ( "fmt" "github.com/securenative/packman/internal" + "github.com/securenative/packman/internal/etc" "github.com/urfave/cli" ) var UnpackController = cli.Command{ Name: "unpack", Aliases: []string{"u"}, - Usage: "packman unpack [-flagName flagValue]...", + Usage: "packman unpack [-flagName flagValue]...", UsageText: "unpacking a template project with the given flags", Action: func(c *cli.Context) error { return unpack(c) @@ -17,17 +18,13 @@ var UnpackController = cli.Command{ } func unpack(c *cli.Context) error { - if c.NArg() != 2 { + if c.NArg() < 2 { return fmt.Errorf("unpack expects exactly 2 arguments but got %d arguments", c.NArg()) } - path := c.Args().Get(0) - remote := c.Args().Get(1) - flagsMap := make(map[string]string) - - for _, flagName := range c.FlagNames() { - flagsMap[flagName] = c.String(flagName) - } + remote := c.Args().Get(0) + path := c.Args().Get(1) + flagsMap := etc.ArgsToFlagsMap(c.Args()[2:]) return internal.M.TemplatingService.Unpack(remote, path, flagsMap) } diff --git a/go.mod b/go.mod index c162de2..9d1d538 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,9 @@ module github.com/securenative/packman go 1.12 require ( + github.com/fatih/color v1.7.0 + github.com/mattn/go-colorable v0.1.4 // indirect + github.com/mattn/go-isatty v0.0.10 // indirect github.com/otiai10/copy v1.0.2 github.com/stretchr/testify v1.3.0 github.com/urfave/cli v1.22.2 diff --git a/internal/business/template_service.go b/internal/business/template_service.go index d990ba7..59ea500 100644 --- a/internal/business/template_service.go +++ b/internal/business/template_service.go @@ -45,8 +45,16 @@ func (this *templateService) Render(templatePath string, packagePath string, fla } return nil }) + if err != nil { + return err + } + + err = os.RemoveAll(filepath.Join(packagePath, "packman")) + if err != nil { + return err + } - return err + return nil } func (this *templateService) Pack(remoteUrl string, packagePath string) error { diff --git a/internal/data/generic_script_engine.go b/internal/data/generic_script_engine.go index ba242a8..88ae07e 100644 --- a/internal/data/generic_script_engine.go +++ b/internal/data/generic_script_engine.go @@ -35,17 +35,23 @@ func (this *genericScriptEngine) Run(scriptPath string, flags map[string]string) cmdArgs = append(cmdArgs, replyFile) cmd := exec.Command(mainCommand, cmdArgs...) - etc.PrintInfo(fmt.Sprintf("Running %s script file with: '%s'\n", scriptPath, cmd.String())) + etc.PrintInfo(fmt.Sprintf("Running '%s'", cmd.String())) result, err := cmd.CombinedOutput() if err != nil { + if result != nil { + etc.PrintError(" FAILED\n") + etc.PrintError(string(result) + "\n") + } return nil, err } + etc.PrintSuccess(" OK\n") + etc.PrintResponse(string(result)) - etc.PrintResponse(string(result) + "\n") - etc.PrintSuccess("Script was run successfully.\n") - etc.PrintInfo("Trying to read reply file: %s...\n", replyFile) + etc.PrintInfo("Trying to read reply file: %s...", replyFile) content, err := etc.ReadFile(replyFile) if err != nil { + etc.PrintError(" FAILED\n") + etc.PrintError("Unable to read reply file from: %s\n", replyFile) return nil, err } @@ -58,6 +64,9 @@ func (this *genericScriptEngine) Run(scriptPath string, flags map[string]string) os.Remove(flagsFile) os.Remove(replyFile) + etc.PrintSuccess(" OK\n") + etc.PrettyPrintJson(out) + return out, nil } diff --git a/internal/data/git_remote_storage.go b/internal/data/git_remote_storage.go index 32f40dd..b095a15 100644 --- a/internal/data/git_remote_storage.go +++ b/internal/data/git_remote_storage.go @@ -1,6 +1,7 @@ package data import ( + "github.com/securenative/packman/internal/etc" "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/transport" @@ -32,6 +33,7 @@ func (this *gitRemoteStorage) getAuth() transport.AuthMethod { } func (this *gitRemoteStorage) Pull(remotePath string, localPath string) error { + etc.PrintInfo("Pulling %s into %s...\n", remotePath, localPath) _, err := git.PlainClone(localPath, false, &git.CloneOptions{ URL: remotePath, Auth: this.getAuth(), @@ -79,6 +81,6 @@ func (this *gitRemoteStorage) Push(localPath string, remotePath string) error { if err != nil { return err } - + etc.PrintSuccess("Project was pushed to %s successfully.\n", remotePath) return nil } diff --git a/internal/etc/args.go b/internal/etc/args.go new file mode 100644 index 0000000..1d853e2 --- /dev/null +++ b/internal/etc/args.go @@ -0,0 +1,21 @@ +package etc + +import "strings" + +func ArgsToFlagsMap(args []string) map[string]string { + out := make(map[string]string) + for idx, arg := range args { + if isFlagKey(arg) { + key := strings.TrimPrefix(arg, "-") + out[key] = args[idx+1] + } + } + return out +} + +func isFlagKey(arg string) bool { + if strings.HasPrefix(arg, "-") { + return true + } + return false +} diff --git a/internal/etc/console.go b/internal/etc/console.go index 450bf30..e598901 100644 --- a/internal/etc/console.go +++ b/internal/etc/console.go @@ -1,23 +1,36 @@ package etc -import "fmt" - -func PrintHeader(message string, args ...interface{}) { - fmt.Printf(message, args...) -} +import ( + "encoding/json" + "github.com/fatih/color" +) func PrintInfo(message string, args ...interface{}) { - fmt.Printf(message, args...) + c := color.New(color.FgCyan) + _, _ = c.Printf(message, args...) } func PrintResponse(message string, args ...interface{}) { - fmt.Printf(message, args...) + c := color.New(color.FgYellow).Add(color.Italic) + _, _ = c.Printf(message, args...) +} + +func PrettyPrintJson(m map[string]interface{}) { + bytes, err := json.MarshalIndent(m, "", " ") + if err != nil { + PrintError(err.Error()) + return + } + + PrintResponse("%s\n", string(bytes)) } func PrintError(message string, args ...interface{}) { - fmt.Printf(message, args...) + c := color.New(color.FgRed).Add(color.Bold) + _, _ = c.Printf(message, args...) } func PrintSuccess(message string, args ...interface{}) { - fmt.Printf(message, args...) + c := color.New(color.FgGreen).Add(color.Bold) + _, _ = c.Printf(message, args...) } diff --git a/internal/module.go b/internal/module.go index fab9413..07fa48e 100644 --- a/internal/module.go +++ b/internal/module.go @@ -3,6 +3,8 @@ package internal import ( "github.com/securenative/packman/internal/business" "github.com/securenative/packman/internal/data" + "os" + "path/filepath" ) type Module struct { @@ -18,7 +20,13 @@ type Module struct { var M *Module func init() { - localStorage, err := data.NewFileLocalStorage("") + home, err := os.UserHomeDir() + if err != nil { + panic(err) + } + + localFilePath := filepath.Join(home, "packman_config.json") + localStorage, err := data.NewFileLocalStorage(localFilePath) if err != nil { panic(err) } diff --git a/pkg/packman.go b/pkg/packman.go new file mode 100644 index 0000000..750ba18 --- /dev/null +++ b/pkg/packman.go @@ -0,0 +1,36 @@ +package packman + +import ( + "encoding/json" + "io/ioutil" + "os" +) + +func ReadFlags() map[string]string { + var out map[string]string + path := os.Args[1] + flagsContent, err := ioutil.ReadFile(path) + if err != nil { + panic(err) + } + + err = json.Unmarshal(flagsContent, &out) + if err != nil { + panic(err) + } + + return out +} + +func WriteReply(model interface{}) { + bytes, err := json.Marshal(model) + if err != nil { + panic(err) + } + + path := os.Args[2] + err = ioutil.WriteFile(path, bytes, os.ModePerm) + if err != nil { + panic(err) + } +}