diff --git a/cmd/grifter.go b/cmd/grifter.go index f742936..0a56ab3 100644 --- a/cmd/grifter.go +++ b/cmd/grifter.go @@ -1,14 +1,15 @@ package cmd import ( + "bufio" + "fmt" + "github.com/pkg/errors" "html/template" "os" "path" "path/filepath" "strings" "sync" - - "github.com/pkg/errors" ) const exePath = ".grifter/main.go" @@ -69,7 +70,20 @@ func newGrifter(name string) (*grifter, error) { return g, errors.Errorf("There is no directory named 'grifts'. Run '%s init' or switch to the appropriate directory", name) } g.GriftsAbsolutePath = filepath.ToSlash(filepath.Join(currentPath, "grifts")) - g.GriftsPackagePath = filepath.ToSlash(filepath.Join(path.Base(currentPath), "grifts")) + + if f, err := os.Open("go.mod"); err == nil { + //go.mod exists, take package path from it + reader := bufio.NewReader(f) + first, err := reader.ReadString('\n') + if err != nil { + return g, errors.New("Cannot read go.mod") + } + packagePath := strings.TrimSuffix(strings.Split(first, " ")[1], "\n") + g.GriftsPackagePath = fmt.Sprintf("%s/grifts", packagePath) + } else { + g.GriftsPackagePath = filepath.ToSlash(filepath.Join(path.Base(currentPath), "grifts")) + } + } return g, nil @@ -80,7 +94,6 @@ func (g *grifter) Setup() error { if err != nil { return errors.WithStack(err) } - err = os.MkdirAll(filepath.Dir(exePath), 0755) if err != nil { return errors.WithStack(err) @@ -94,7 +107,6 @@ func (g *grifter) Setup() error { if err != nil { return errors.WithStack(err) } - return nil }