Skip to content

Commit ec18220

Browse files
committed
Bug fixes from testing the actual cli
1 parent 2244877 commit ec18220

10 files changed

Lines changed: 125 additions & 31 deletions

File tree

‎cmd/packman/controllers/render.go‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers
33
import (
44
"fmt"
55
"github.com/securenative/packman/internal"
6+
"github.com/securenative/packman/internal/etc"
67
"github.com/urfave/cli"
78
)
89

@@ -17,16 +18,12 @@ var RenderController = cli.Command{
1718
}
1819

1920
func render(c *cli.Context) error {
20-
if c.NArg() != 1 {
21+
if c.NArg() < 1 {
2122
return fmt.Errorf("unpack expects exactly 1 argument but got %d arguments", c.NArg())
2223
}
2324

2425
path := c.Args().Get(0)
25-
flagsMap := make(map[string]string)
26-
27-
for _, flagName := range c.FlagNames() {
28-
flagsMap[flagName] = c.String(flagName)
29-
}
26+
flagsMap := etc.ArgsToFlagsMap(c.Args()[1:])
3027

3128
return internal.M.TemplatingService.Render(path, fmt.Sprintf("%s-rendered", path), flagsMap)
3229
}

‎cmd/packman/controllers/unpack.go‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,28 @@ package controllers
33
import (
44
"fmt"
55
"github.com/securenative/packman/internal"
6+
"github.com/securenative/packman/internal/etc"
67
"github.com/urfave/cli"
78
)
89

910
var UnpackController = cli.Command{
1011
Name: "unpack",
1112
Aliases: []string{"u"},
12-
Usage: "packman unpack <path> <remote_url> [-flagName flagValue]...",
13+
Usage: "packman unpack <remote_url> <path> [-flagName flagValue]...",
1314
UsageText: "unpacking a template project with the given flags",
1415
Action: func(c *cli.Context) error {
1516
return unpack(c)
1617
},
1718
}
1819

1920
func unpack(c *cli.Context) error {
20-
if c.NArg() != 2 {
21+
if c.NArg() < 2 {
2122
return fmt.Errorf("unpack expects exactly 2 arguments but got %d arguments", c.NArg())
2223
}
2324

24-
path := c.Args().Get(0)
25-
remote := c.Args().Get(1)
26-
flagsMap := make(map[string]string)
27-
28-
for _, flagName := range c.FlagNames() {
29-
flagsMap[flagName] = c.String(flagName)
30-
}
25+
remote := c.Args().Get(0)
26+
path := c.Args().Get(1)
27+
flagsMap := etc.ArgsToFlagsMap(c.Args()[2:])
3128

3229
return internal.M.TemplatingService.Unpack(remote, path, flagsMap)
3330
}

‎go.mod‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module github.com/securenative/packman
33
go 1.12
44

55
require (
6+
github.com/fatih/color v1.7.0
7+
github.com/mattn/go-colorable v0.1.4 // indirect
8+
github.com/mattn/go-isatty v0.0.10 // indirect
69
github.com/otiai10/copy v1.0.2
710
github.com/stretchr/testify v1.3.0
811
github.com/urfave/cli v1.22.2

‎internal/business/template_service.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ func (this *templateService) Render(templatePath string, packagePath string, fla
4545
}
4646
return nil
4747
})
48+
if err != nil {
49+
return err
50+
}
51+
52+
err = os.RemoveAll(filepath.Join(packagePath, "packman"))
53+
if err != nil {
54+
return err
55+
}
4856

49-
return err
57+
return nil
5058
}
5159

5260
func (this *templateService) Pack(remoteUrl string, packagePath string) error {

‎internal/data/generic_script_engine.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,23 @@ func (this *genericScriptEngine) Run(scriptPath string, flags map[string]string)
3535
cmdArgs = append(cmdArgs, replyFile)
3636

3737
cmd := exec.Command(mainCommand, cmdArgs...)
38-
etc.PrintInfo(fmt.Sprintf("Running %s script file with: '%s'\n", scriptPath, cmd.String()))
38+
etc.PrintInfo(fmt.Sprintf("Running '%s'", cmd.String()))
3939
result, err := cmd.CombinedOutput()
4040
if err != nil {
41+
if result != nil {
42+
etc.PrintError(" FAILED\n")
43+
etc.PrintError(string(result) + "\n")
44+
}
4145
return nil, err
4246
}
47+
etc.PrintSuccess(" OK\n")
48+
etc.PrintResponse(string(result))
4349

44-
etc.PrintResponse(string(result) + "\n")
45-
etc.PrintSuccess("Script was run successfully.\n")
46-
etc.PrintInfo("Trying to read reply file: %s...\n", replyFile)
50+
etc.PrintInfo("Trying to read reply file: %s...", replyFile)
4751
content, err := etc.ReadFile(replyFile)
4852
if err != nil {
53+
etc.PrintError(" FAILED\n")
54+
etc.PrintError("Unable to read reply file from: %s\n", replyFile)
4955
return nil, err
5056
}
5157

@@ -58,6 +64,9 @@ func (this *genericScriptEngine) Run(scriptPath string, flags map[string]string)
5864
os.Remove(flagsFile)
5965
os.Remove(replyFile)
6066

67+
etc.PrintSuccess(" OK\n")
68+
etc.PrettyPrintJson(out)
69+
6170
return out, nil
6271
}
6372

‎internal/data/git_remote_storage.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package data
22

33
import (
4+
"github.com/securenative/packman/internal/etc"
45
"gopkg.in/src-d/go-git.v4"
56
"gopkg.in/src-d/go-git.v4/plumbing/object"
67
"gopkg.in/src-d/go-git.v4/plumbing/transport"
@@ -32,6 +33,7 @@ func (this *gitRemoteStorage) getAuth() transport.AuthMethod {
3233
}
3334

3435
func (this *gitRemoteStorage) Pull(remotePath string, localPath string) error {
36+
etc.PrintInfo("Pulling %s into %s...\n", remotePath, localPath)
3537
_, err := git.PlainClone(localPath, false, &git.CloneOptions{
3638
URL: remotePath,
3739
Auth: this.getAuth(),
@@ -79,6 +81,6 @@ func (this *gitRemoteStorage) Push(localPath string, remotePath string) error {
7981
if err != nil {
8082
return err
8183
}
82-
84+
etc.PrintSuccess("Project was pushed to %s successfully.\n", remotePath)
8385
return nil
8486
}

‎internal/etc/args.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package etc
2+
3+
import "strings"
4+
5+
func ArgsToFlagsMap(args []string) map[string]string {
6+
out := make(map[string]string)
7+
for idx, arg := range args {
8+
if isFlagKey(arg) {
9+
key := strings.TrimPrefix(arg, "-")
10+
out[key] = args[idx+1]
11+
}
12+
}
13+
return out
14+
}
15+
16+
func isFlagKey(arg string) bool {
17+
if strings.HasPrefix(arg, "-") {
18+
return true
19+
}
20+
return false
21+
}

‎internal/etc/console.go‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
package etc
22

3-
import "fmt"
4-
5-
func PrintHeader(message string, args ...interface{}) {
6-
fmt.Printf(message, args...)
7-
}
3+
import (
4+
"encoding/json"
5+
"github.com/fatih/color"
6+
)
87

98
func PrintInfo(message string, args ...interface{}) {
10-
fmt.Printf(message, args...)
9+
c := color.New(color.FgCyan)
10+
_, _ = c.Printf(message, args...)
1111
}
1212

1313
func PrintResponse(message string, args ...interface{}) {
14-
fmt.Printf(message, args...)
14+
c := color.New(color.FgYellow).Add(color.Italic)
15+
_, _ = c.Printf(message, args...)
16+
}
17+
18+
func PrettyPrintJson(m map[string]interface{}) {
19+
bytes, err := json.MarshalIndent(m, "", " ")
20+
if err != nil {
21+
PrintError(err.Error())
22+
return
23+
}
24+
25+
PrintResponse("%s\n", string(bytes))
1526
}
1627

1728
func PrintError(message string, args ...interface{}) {
18-
fmt.Printf(message, args...)
29+
c := color.New(color.FgRed).Add(color.Bold)
30+
_, _ = c.Printf(message, args...)
1931
}
2032

2133
func PrintSuccess(message string, args ...interface{}) {
22-
fmt.Printf(message, args...)
34+
c := color.New(color.FgGreen).Add(color.Bold)
35+
_, _ = c.Printf(message, args...)
2336
}

‎internal/module.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package internal
33
import (
44
"github.com/securenative/packman/internal/business"
55
"github.com/securenative/packman/internal/data"
6+
"os"
7+
"path/filepath"
68
)
79

810
type Module struct {
@@ -18,7 +20,13 @@ type Module struct {
1820
var M *Module
1921

2022
func init() {
21-
localStorage, err := data.NewFileLocalStorage("")
23+
home, err := os.UserHomeDir()
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
localFilePath := filepath.Join(home, "packman_config.json")
29+
localStorage, err := data.NewFileLocalStorage(localFilePath)
2230
if err != nil {
2331
panic(err)
2432
}

‎pkg/packman.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package packman
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"os"
7+
)
8+
9+
func ReadFlags() map[string]string {
10+
var out map[string]string
11+
path := os.Args[1]
12+
flagsContent, err := ioutil.ReadFile(path)
13+
if err != nil {
14+
panic(err)
15+
}
16+
17+
err = json.Unmarshal(flagsContent, &out)
18+
if err != nil {
19+
panic(err)
20+
}
21+
22+
return out
23+
}
24+
25+
func WriteReply(model interface{}) {
26+
bytes, err := json.Marshal(model)
27+
if err != nil {
28+
panic(err)
29+
}
30+
31+
path := os.Args[2]
32+
err = ioutil.WriteFile(path, bytes, os.ModePerm)
33+
if err != nil {
34+
panic(err)
35+
}
36+
}

0 commit comments

Comments
 (0)
⌂ 换网址