From 4915f0d3191c3d2603392d3c5dc2b081266f102c Mon Sep 17 00:00:00 2001 From: Paul Bergeron Date: Wed, 7 Feb 2018 15:59:52 -0800 Subject: [PATCH] Remove dependency on gobuffalo/envy Closes #45 Credit to @dcelasun for the basis of the code --- box.go | 3 +-- builder/builder_test.go | 4 ++-- env.go | 27 +++++++++++++++++++++++++++ packr/cmd/build.go | 4 ++-- packr/cmd/install.go | 6 +++--- 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 env.go diff --git a/box.go b/box.go index 8d5d1ae..7f50521 100644 --- a/box.go +++ b/box.go @@ -12,7 +12,6 @@ import ( "compress/gzip" - "github.com/gobuffalo/envy" "github.com/pkg/errors" ) @@ -34,7 +33,7 @@ func NewBox(path string) Box { cov := filepath.Join("_test", "_obj_test") cd = strings.Replace(cd, string(filepath.Separator)+cov, "", 1) if !filepath.IsAbs(cd) && cd != "" { - cd = filepath.Join(envy.GoPath(), "src", cd) + cd = filepath.Join(GoPath(), "src", cd) } return Box{ diff --git a/builder/builder_test.go b/builder/builder_test.go index d49ddf7..b1edffe 100644 --- a/builder/builder_test.go +++ b/builder/builder_test.go @@ -9,7 +9,7 @@ import ( "path/filepath" "testing" - "github.com/gobuffalo/envy" + "github.com/gobuffalo/packr" "github.com/stretchr/testify/require" ) @@ -91,7 +91,7 @@ func Test_Binary_Builds(t *testing.T) { r.NoError(err) os.Chdir(root) - cmd := exec.Command(envy.Get("GO_BIN", "go"), "build", "-v", "-o", "bin/example") + cmd := exec.Command(packr.GoBin(), "build", "-v", "-o", "bin/example") err = cmd.Run() r.NoError(err) diff --git a/env.go b/env.go new file mode 100644 index 0000000..2c744e7 --- /dev/null +++ b/env.go @@ -0,0 +1,27 @@ +package packr + +import ( + "go/build" + "os" + "strings" +) + +// GoPath returns the current GOPATH env var +// or if it's missing, the default. +func GoPath() string { + go_path := strings.Split(os.Getenv("GOPATH"), string(os.PathListSeparator)) + if len(go_path) == 0 || go_path[0] == "" { + return build.Default.GOPATH + } + return go_path[0] +} + +// GoBin returns the current GO_BIN env var +// or if it's missing, a default of "go" +func GoBin() string { + go_bin := os.Getenv("GO_BIN") + if go_bin == "" { + return "go" + } + return go_bin +} diff --git a/packr/cmd/build.go b/packr/cmd/build.go index 926d0d9..aaa640d 100644 --- a/packr/cmd/build.go +++ b/packr/cmd/build.go @@ -5,7 +5,7 @@ import ( "os" "os/exec" - "github.com/gobuffalo/envy" + "github.com/gobuffalo/packr" "github.com/gobuffalo/packr/builder" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -26,7 +26,7 @@ var buildCmd = &cobra.Command{ cargs := []string{"build"} cargs = append(cargs, args...) - cp := exec.Command(envy.Get("GO_BIN", "go"), cargs...) + cp := exec.Command(packr.GoBin(), cargs...) cp.Stderr = os.Stderr cp.Stdin = os.Stdin cp.Stdout = os.Stdout diff --git a/packr/cmd/install.go b/packr/cmd/install.go index 7fff8fa..1c8b166 100644 --- a/packr/cmd/install.go +++ b/packr/cmd/install.go @@ -7,7 +7,7 @@ import ( "path/filepath" "strings" - "github.com/gobuffalo/envy" + "github.com/gobuffalo/packr" "github.com/gobuffalo/packr/builder" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -22,7 +22,7 @@ var installCmd = &cobra.Command{ if len(args) > 0 { input = args[len(args)-1] if !strings.HasPrefix(input, ".") { - input = filepath.Join(envy.GoPath(), "src", input) + input = filepath.Join(packr.GoPath(), "src", input) if _, err := os.Stat(input); err != nil { return errors.WithStack(err) } @@ -37,7 +37,7 @@ var installCmd = &cobra.Command{ cargs := []string{"install"} cargs = append(cargs, args...) - cp := exec.Command(envy.Get("GO_BIN", "go"), cargs...) + cp := exec.Command(packr.GoBin(), cargs...) cp.Stderr = os.Stderr cp.Stdin = os.Stdin cp.Stdout = os.Stdout