Skip to content

Commit

Permalink
Merge pull request #39 from gochigo/deprecate-currentpackage
Browse files Browse the repository at this point in the history
removed CurrentPackage() since envy only support go module mode
  • Loading branch information
paganotoni committed Nov 3, 2021
2 parents 3dbe4c9 + 28917e4 commit 9d76507
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
41 changes: 3 additions & 38 deletions envy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -68,11 +67,6 @@ func loadEnv() {
}
}

// Mods always returns true, GOPATH isn't supported
func Mods() bool {
return true
}

// Reload the ENV variables. Useful if
// an external ENV manager has been used
func Reload() {
Expand Down Expand Up @@ -215,32 +209,12 @@ func GoPaths() []string {
return strings.Split(gp, ":")
}

func importPath(path string) string {
path = strings.TrimPrefix(path, "/private")
for _, gopath := range GoPaths() {
srcpath := filepath.Join(gopath, "src")
rel, err := filepath.Rel(srcpath, path)
if err == nil {
return filepath.ToSlash(rel)
}
}

// fallback to trim
rel := strings.TrimPrefix(path, filepath.Join(GoPath(), "src"))
rel = strings.TrimPrefix(rel, string(filepath.Separator))
return filepath.ToSlash(rel)
}

// CurrentModule will attempt to return the module name from `go.mod` if
// modules are enabled.
// If modules are not enabled it will fallback to using CurrentPackage instead.
// CurrentModule will attempt to return the module name from `go.mod`.
// GOPATH isn't supported, no fallback to `CurrentPackage()` anymore.
func CurrentModule() (string, error) {
if !Mods() {
return CurrentPackage(), nil
}
moddata, err := ioutil.ReadFile("go.mod")
if err != nil {
return "", errors.New("go.mod cannot be read or does not exist while go module is enabled")
return "", errors.New("go.mod cannot be read or does not exist")
}
packagePath := modfile.ModulePath(moddata)
if packagePath == "" {
Expand All @@ -249,15 +223,6 @@ func CurrentModule() (string, error) {
return packagePath, nil
}

// CurrentPackage attempts to figure out the current package name from the PWD
// Use CurrentModule for a more accurate package name.
func CurrentPackage() string {
if Mods() {
}
pwd, _ := os.Getwd()
return importPath(pwd)
}

func Environ() []string {
gil.RLock()
defer gil.RUnlock()
Expand Down
10 changes: 7 additions & 3 deletions envy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ func Test_GoPaths(t *testing.T) {
})
}

func Test_CurrentPackage(t *testing.T) {
func Test_CurrentModule(t *testing.T) {
r := require.New(t)
r.Equal("github.com/gobuffalo/envy", CurrentPackage())
mod, err := CurrentModule()
r.NoError(err)
r.Equal("github.com/gobuffalo/envy", mod)
}

// Env files loading
Expand Down Expand Up @@ -210,5 +212,7 @@ func Test_GOPATH_Not_Set(t *testing.T) {
r.Equal("/go", Get("GOPATH", "notset"))
})

r.Equal("github.com/gobuffalo/envy", CurrentPackage())
mod, err := CurrentModule()
r.NoError(err)
r.Equal("github.com/gobuffalo/envy", mod)
}

0 comments on commit 9d76507

Please sign in to comment.