Skip to content

Commit

Permalink
read package path from go.mod if it exists (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrkren authored and markbates committed Oct 26, 2018
1 parent 1ca2108 commit fce0597
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
26 changes: 23 additions & 3 deletions cmd/grifter.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package cmd

import (
"fmt"
"github.com/pkg/errors"
"github.com/rogpeppe/go-internal/modfile"
"html/template"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"sync"

"github.com/pkg/errors"
)

const exePath = ".grifter/main.go"
Expand Down Expand Up @@ -69,7 +71,24 @@ 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"))

// check for go module to see if we can get go.mod
if os.Getenv("GO111MODULE") == "on" {
moddata, err := ioutil.ReadFile("go.mod")
if err != nil {
return g, errors.New("go.mod cannot be read or does not exist while go module is enabled.")
}
packagePath := modfile.ModulePath(moddata)
if packagePath == "" {
return g, errors.New("go.mod is malformed.")
}
g.GriftsPackagePath = fmt.Sprintf("%s/grifts", packagePath)
} else {
// no go module, infer package path from current directory
g.GriftsPackagePath = filepath.ToSlash(filepath.Join(path.Base(currentPath), "grifts"))
}


}

return g, nil
Expand Down Expand Up @@ -101,3 +120,4 @@ func (g *grifter) Setup() error {
func (g *grifter) TearDown() error {
return os.RemoveAll(filepath.Dir(exePath))
}

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.0.0-alpha
github.com/stretchr/testify v1.2.2
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.0.0-alpha h1:zJtNjia0DW6sKVGzv5i+3TwGRJEJmaPnU9F12IRf/gI=
github.com/rogpeppe/go-internal v1.0.0-alpha/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=

0 comments on commit fce0597

Please sign in to comment.