diff --git a/main.go b/main.go index d63489b..1b13999 100644 --- a/main.go +++ b/main.go @@ -280,8 +280,6 @@ func wskPropertySet(apihost, auth string) error { } func runNuv(baseDir string, args []string) error { - localFolder := os.Getenv("NUV_PWD") - err := Nuv(baseDir, args[1:]) if err == nil { return nil @@ -292,7 +290,7 @@ func runNuv(baseDir string, args []string) error { var taskNotFoundErr *TaskNotFoundErr if errors.As(err, &taskNotFoundErr) { trace("task not found, looking for plugin:", args[1]) - plgDir, err := findTaskInPlugins(localFolder, args[1]) + plgDir, err := findTaskInPlugins(args[1]) if err != nil { return taskNotFoundErr } @@ -317,8 +315,7 @@ func setupNuvPwd() { } func buildConfigMap(nuvRootPath string, configPath string) (*config.ConfigMap, error) { - localDir := os.Getenv("NUV_PWD") - plgNuvRootMap, err := GetNuvRootPlugins(localDir) + plgNuvRootMap, err := GetNuvRootPlugins() if err != nil { return nil, err } diff --git a/nuv.go b/nuv.go index 89a8cf0..bb23917 100644 --- a/nuv.go +++ b/nuv.go @@ -210,7 +210,7 @@ func Nuv(base string, args []string) error { err := help() if !isSubCmd { fmt.Println() - return printPluginsHelp(parent(base)) + return printPluginsHelp() } return err } diff --git a/plugin.go b/plugin.go index 285ae7b..5ae1202 100644 --- a/plugin.go +++ b/plugin.go @@ -129,8 +129,8 @@ func checkGitRepo(url string) (bool, string) { return false, "" } -func printPluginsHelp(localDir string) error { - plgs, err := newPlugins(localDir) +func printPluginsHelp() error { + plgs, err := newPlugins() if err != nil { return err } @@ -143,8 +143,8 @@ func printPluginsHelp(localDir string) error { // If the same plugin is found in both folders, the one in the local folder // is used. // Useful to build the config map including the plugin configs -func GetNuvRootPlugins(localDir string) (map[string]string, error) { - plgs, err := newPlugins(localDir) +func GetNuvRootPlugins() (map[string]string, error) { + plgs, err := newPlugins() if err != nil { return nil, err } @@ -171,8 +171,8 @@ func GetNuvRootPlugins(localDir string) (map[string]string, error) { // findTaskInPlugins returns the path to the plugin containing the task // or an error if the task is not found -func findTaskInPlugins(localDir string, plg string) (string, error) { - plgs, err := newPlugins(localDir) +func findTaskInPlugins(plg string) (string, error) { + plgs, err := newPlugins() if err != nil { return "", err } @@ -202,7 +202,8 @@ type plugins struct { nuv []string } -func newPlugins(localDir string) (*plugins, error) { +func newPlugins() (*plugins, error) { + localDir := os.Getenv("NUV_PWD") localOlarisFolders := make([]string, 0) nuvOlarisFolders := make([]string, 0) diff --git a/plugin_test.go b/plugin_test.go index 5a1bfc7..1406390 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -70,8 +70,9 @@ func TestGetAllNuvRootPlugins(t *testing.T) { t.Run("success: get all the nuvroots.json from plugins with 1 plugin", func(t *testing.T) { tempDir := t.TempDir() plgFolder := setupPluginTest(tempDir, t) + os.Setenv("NUV_PWD", tempDir) - nuvRoots, err := GetNuvRootPlugins(tempDir) + nuvRoots, err := GetNuvRootPlugins() require.NoError(t, err) require.Len(t, nuvRoots, 1) require.Equal(t, joinpath(plgFolder, NUVROOT), nuvRoots[getPluginName(plgFolder)]) @@ -79,6 +80,7 @@ func TestGetAllNuvRootPlugins(t *testing.T) { t.Run("success: get all the nuvroots.json from plugins with 2 plugins", func(t *testing.T) { tempDir := t.TempDir() + os.Setenv("NUV_PWD", tempDir) plgFolder := setupPluginTest(tempDir, t) // create the olaris-test2 folder @@ -96,7 +98,7 @@ func TestGetAllNuvRootPlugins(t *testing.T) { err = copyFile(nuvfileYML, filepath.Join(olarisTestDir, "nuvfile.yml")) require.NoError(t, err) - nuvRoots, err := GetNuvRootPlugins(tempDir) + nuvRoots, err := GetNuvRootPlugins() require.NoError(t, err) require.Len(t, nuvRoots, 2) require.Equal(t, joinpath(plgFolder, NUVROOT), nuvRoots[getPluginName(plgFolder)]) @@ -105,9 +107,10 @@ func TestGetAllNuvRootPlugins(t *testing.T) { t.Run("empty: no plugins folder found (olaris-*)", func(t *testing.T) { tempDir := t.TempDir() + os.Setenv("NUV_PWD", tempDir) // Test when the folder is not found - nuvRoots, err := GetNuvRootPlugins(tempDir) + nuvRoots, err := GetNuvRootPlugins() require.NoError(t, err) require.Empty(t, nuvRoots) }) @@ -116,18 +119,20 @@ func TestGetAllNuvRootPlugins(t *testing.T) { func TestFindPluginTask(t *testing.T) { t.Run("success: plugin task found in ./olaris-test", func(t *testing.T) { tempDir := t.TempDir() + os.Setenv("NUV_PWD", tempDir) plgFolder := setupPluginTest(tempDir, t) - fld, err := findTaskInPlugins(tempDir, "test") + fld, err := findTaskInPlugins("test") require.NoError(t, err) require.Equal(t, plgFolder, fld) }) t.Run("error: no plugins folder found (olaris-*)", func(t *testing.T) { tempDir := t.TempDir() + os.Setenv("NUV_PWD", tempDir) // Test when the folder is not found - fld, err := findTaskInPlugins(tempDir, "grep") + fld, err := findTaskInPlugins("grep") require.Error(t, err) require.Empty(t, fld) }) @@ -138,7 +143,9 @@ func TestNewPlugins(t *testing.T) { tempDir := t.TempDir() plgFolder := setupPluginTest(tempDir, t) - p, err := newPlugins(tempDir) + os.Setenv("NUV_PWD", tempDir) + + p, err := newPlugins() require.NoError(t, err) require.NotNil(t, p) require.Len(t, p.local, 1) @@ -147,7 +154,8 @@ func TestNewPlugins(t *testing.T) { t.Run("non existent local dir results in empty local field", func(t *testing.T) { localDir := "/path/to/nonexistent/dir" - p, err := newPlugins(localDir) + os.Setenv("NUV_PWD", localDir) + p, err := newPlugins() require.NoError(t, err) require.NotNil(t, p) require.Len(t, p.local, 0)