Skip to content

Commit

Permalink
fixes support for mods outside the gopath
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Oct 26, 2018
1 parent fce0597 commit 6475990
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
42 changes: 26 additions & 16 deletions cmd/grifter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ 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/gobuffalo/envy"
"github.com/pkg/errors"
"github.com/rogpeppe/go-internal/modfile"
)

const exePath = ".grifter/main.go"
Expand Down Expand Up @@ -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.")
Expand All @@ -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
Expand All @@ -120,4 +131,3 @@ func (g *grifter) Setup() error {
func (g *grifter) TearDown() error {
return os.RemoveAll(filepath.Dir(exePath))
}

2 changes: 2 additions & 0 deletions cmd/templates.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cmd

var modTmpl = `module grifter`

var mainTmpl = `
package main
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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=
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=
Expand Down

0 comments on commit 6475990

Please sign in to comment.