Skip to content

Commit

Permalink
read package path from go.mod if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrkren committed Oct 23, 2018
1 parent 1ca2108 commit 3f419b5
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cmd/grifter.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -94,7 +107,6 @@ func (g *grifter) Setup() error {
if err != nil {
return errors.WithStack(err)
}

return nil
}

Expand Down

0 comments on commit 3f419b5

Please sign in to comment.