From 64759906aa698a2ed2f36f0c037b543412ccb54b Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 26 Oct 2018 15:16:20 -0400 Subject: [PATCH] fixes support for mods outside the gopath --- cmd/grifter.go | 42 ++++++++++++++++++++++++++---------------- cmd/templates.go | 2 ++ go.mod | 5 ++--- go.sum | 8 ++++++-- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/cmd/grifter.go b/cmd/grifter.go index e90781d..e42410d 100644 --- a/cmd/grifter.go +++ b/cmd/grifter.go @@ -2,8 +2,6 @@ package cmd import ( "fmt" - "github.com/pkg/errors" - "github.com/rogpeppe/go-internal/modfile" "html/template" "io/ioutil" "os" @@ -11,6 +9,10 @@ import ( "path/filepath" "strings" "sync" + + "github.com/gobuffalo/envy" + "github.com/pkg/errors" + "github.com/rogpeppe/go-internal/modfile" ) const exePath = ".grifter/main.go" @@ -73,7 +75,7 @@ func newGrifter(name string) (*grifter, error) { g.GriftsAbsolutePath = filepath.ToSlash(filepath.Join(currentPath, "grifts")) // check for go module to see if we can get go.mod - if os.Getenv("GO111MODULE") == "on" { + if envy.Mods() { 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.") @@ -88,30 +90,39 @@ func newGrifter(name string) (*grifter, error) { g.GriftsPackagePath = filepath.ToSlash(filepath.Join(path.Base(currentPath), "grifts")) } - } return g, nil } func (g *grifter) Setup() error { - t, err := template.New("main").Parse(mainTmpl) - if err != nil { - return errors.WithStack(err) - } - err = os.MkdirAll(filepath.Dir(exePath), 0755) + root := filepath.Dir(exePath) + err := os.MkdirAll(root, 0755) if err != nil { return errors.WithStack(err) } - f, err := os.Create(exePath) - if err != nil { - return errors.WithStack(err) + + tmpls := map[string]string{} + tmpls[exePath] = mainTmpl + if envy.Mods() { + tmpls[filepath.Join(root, "go.mod")] = modTmpl } + for k, v := range tmpls { + t, err := template.New(k).Parse(v) + if err != nil { + return errors.WithStack(err) + } - err = t.Execute(f, g) - if err != nil { - return errors.WithStack(err) + f, err := os.Create(k) + if err != nil { + return errors.WithStack(err) + } + + err = t.Execute(f, g) + if err != nil { + return errors.WithStack(err) + } } return nil @@ -120,4 +131,3 @@ func (g *grifter) Setup() error { func (g *grifter) TearDown() error { return os.RemoveAll(filepath.Dir(exePath)) } - diff --git a/cmd/templates.go b/cmd/templates.go index 7570a35..74ccb38 100644 --- a/cmd/templates.go +++ b/cmd/templates.go @@ -1,5 +1,7 @@ package cmd +var modTmpl = `module grifter` + var mainTmpl = ` package main diff --git a/go.mod b/go.mod index b98a223..1785fa8 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,8 @@ module github.com/markbates/grift require ( - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gobuffalo/envy v1.6.6 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/rogpeppe/go-internal v1.0.0 github.com/stretchr/testify v1.2.2 ) diff --git a/go.sum b/go.sum index 034f54a..6f0a24a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ 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/gobuffalo/envy v1.6.6 h1:QTIrr8kp0NBhJsjwP04jSnuXB2u6QDJVwESVuFiQWUY= +github.com/gobuffalo/envy v1.6.6/go.mod h1:N+GkhhZ/93bGZc6ZKhJLP6+m+tCNPKwgSpH9kaifseQ= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= 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= @@ -7,8 +11,8 @@ 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/rogpeppe/go-internal v1.0.0 h1:o4VLZ5jqHE+HahLT6drNtSGTrrUA3wPBmtpgqtdbClo= +github.com/rogpeppe/go-internal v1.0.0/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=