Skip to content

Commit

Permalink
using go-internal/modfile to parse go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrkren committed Oct 25, 2018
1 parent 3f419b5 commit c2e4354
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cmd/grifter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"bufio"
"fmt"
"github.com/pkg/errors"
"github.com/rogpeppe/go-internal/modfile"
"html/template"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -65,25 +66,29 @@ func newGrifter(name string) (*grifter, error) {
g.GriftsAbsolutePath = filepath.ToSlash(filepath.Join(currentPath, "grifts"))
g.GriftsPackagePath = filepath.ToSlash(filepath.Join(p[1], "grifts"))
} else {
//is outside of gopath, dont loop to parent
//is outside of gopath, dont loop to parrent
if !hasGriftDir(currentPath) {
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"))

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')
// 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("Cannot read go.mod")
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.")
}
packagePath := strings.TrimSuffix(strings.Split(first, " ")[1], "\n")
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 All @@ -94,6 +99,7 @@ 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 @@ -107,9 +113,11 @@ func (g *grifter) Setup() error {
if err != nil {
return errors.WithStack(err)
}

return nil
}

func (g *grifter) TearDown() error {
return os.RemoveAll(filepath.Dir(exePath))
}

0 comments on commit c2e4354

Please sign in to comment.