diff options
author | Martin Treusch von Buttlar <martin.tvb@vitraum.de> | 2017-05-20 10:14:13 +0200 |
---|---|---|
committer | Martin Treusch von Buttlar <martin.tvb@vitraum.de> | 2017-05-20 10:14:13 +0200 |
commit | 372ce32e444c34c9107b5da22c847eb368466cc9 (patch) | |
tree | cb3ce02d0e52d242fcb15e581702775a82128c4d /cmd | |
parent | 0e98babf9b4f9084d7e29b0f484f64ba62dc265b (diff) |
add checks for 'ponzu new' fixes ponzu-cms/ponzu#146
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/ponzu/new.go | 58 | ||||
-rw-r--r-- | cmd/ponzu/new_test.go | 51 |
2 files changed, 103 insertions, 6 deletions
diff --git a/cmd/ponzu/new.go b/cmd/ponzu/new.go index dccf99a..94e4697 100644 --- a/cmd/ponzu/new.go +++ b/cmd/ponzu/new.go @@ -24,20 +24,66 @@ Errors will be reported, but successful commands return nothing.`, Example: `$ ponzu new myProject > New ponzu project created at $GOPATH/src/myProject`, RunE: func(cmd *cobra.Command, args []string) error { - return newProjectInDir(args[0]) + projectName := "ponzu" + if len(args) > 0 { + projectName = args[0] + } + return newProjectInDir(projectName) }, } -func newProjectInDir(path string) error { - // set path to be nested inside $GOPATH/src +func checkNmkAbs(gPath string) (string, error) { gopath, err := getGOPATH() if err != nil { + return "", err + } + gosrc := filepath.Join(gopath, "src") + + path := gPath + // support current directory + if path == "." { + path, err = os.Getwd() + if err != nil { + return "", err + } + } else { + path = filepath.Join(gosrc, path) + } + + // make sure path is inside $GOPATH/src + srcrel, err := filepath.Rel(gosrc, path) + if err != nil { + return "", err + } + if len(srcrel) >= 2 && srcrel[:2] == ".." { + return "", fmt.Errorf("path '%s' must be inside '%s'", gPath, gosrc) + } + if srcrel == "." { + return "", fmt.Errorf("path '%s' must not be %s", path, filepath.Join("GOPATH", "src")) + } + + _, err = os.Stat(path) + if err != nil && !os.IsNotExist(err) { + return "", err + } + if err == nil { + err = os.ErrExist + } else if os.IsNotExist(err) { + err = nil + } + + return path, err +} + +func newProjectInDir(path string) error { + path, err := checkNmkAbs(path) + if err != nil && !os.IsNotExist(err) { return err } - path = filepath.Join(gopath, "src", path) - // check if anything exists at the path, ask if it should be overwritten - if _, err = os.Stat(path); !os.IsNotExist(err) { + // path exists, ask if it should be overwritten + if os.IsNotExist(err) { + fmt.Printf("Using '%s' as project directory\n", path) fmt.Println("Path exists, overwrite contents? (y/N):") answer, err := getAnswer() diff --git a/cmd/ponzu/new_test.go b/cmd/ponzu/new_test.go new file mode 100644 index 0000000..a143d1a --- /dev/null +++ b/cmd/ponzu/new_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "os" + "path/filepath" + "testing" +) + +func TestNewCheckNmkAbs(t *testing.T) { + savedGOPATH := os.Getenv("GOPATH") + defer os.Setenv("GOPATH", savedGOPATH) + pwd, err := os.Getwd() + if err != nil { + t.Fatalf("Could not determine current working directory: %s", err) + } + + isNil := func(e error) bool { return e == nil } + + testTable := []struct { + base, wd, a, + wantP string + wantE func(e error) bool + }{{ + base: filepath.Join(pwd, "test-fixtures", "new"), + wd: filepath.Join("src", "existing"), + a: ".", + wantP: filepath.Join(pwd, "test-fixtures", "new", "src", "existing"), + wantE: os.IsExist, + }, { + base: filepath.Join(pwd, "test-fixtures", "new"), + wd: filepath.Join(""), + a: "non-existing", + wantP: filepath.Join(pwd, "test-fixtures", "new", "src", "non-existing"), + wantE: isNil, + }} + + for _, test := range testTable { + os.Setenv("GOPATH", test.base) + err = os.Chdir(filepath.Join(test.base, test.wd)) + if err != nil { + t.Fatalf("could not setup base: %s", err) + } + got, gotE := checkNmkAbs(test.a) + if got != test.wantP { + t.Errorf("got '%s', want: '%s'", got, test.wantP) + } + if !test.wantE(gotE) { + t.Errorf("got error '%s'", gotE) + } + } +} |