Skip to content

Commit

Permalink
Bug fixes from testing the actual cli
Browse files Browse the repository at this point in the history
  • Loading branch information
matang28 committed Nov 25, 2019
1 parent 2244877 commit ec18220
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 31 deletions.
9 changes: 3 additions & 6 deletions cmd/packman/controllers/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"fmt"
"github.com/securenative/packman/internal"
"github.com/securenative/packman/internal/etc"
"github.com/urfave/cli"
)

Expand All @@ -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)
}
15 changes: 6 additions & 9 deletions cmd/packman/controllers/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@ 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 <path> <remote_url> [-flagName flagValue]...",
Usage: "packman unpack <remote_url> <path> [-flagName flagValue]...",
UsageText: "unpacking a template project with the given flags",
Action: func(c *cli.Context) error {
return unpack(c)
},
}

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)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion internal/business/template_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 13 additions & 4 deletions internal/data/generic_script_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion internal/data/git_remote_storage.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions internal/etc/args.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 22 additions & 9 deletions internal/etc/console.go
Original file line number Diff line number Diff line change
@@ -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...)
}
10 changes: 9 additions & 1 deletion internal/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/packman.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ec18220

Please sign in to comment.