Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed CurrentPackage() since envy only support go module mode #39

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}