Skip to content

Commit 96b11a9

Browse files
committed
fix(cli): fix env var PROVIDERS_NPM_GLOBAL_FOLDER
1 parent c1b895e commit 96b11a9

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

config/static/static_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ type HttpProvider struct {
128128
}
129129

130130
type NpmProvider struct {
131-
GlobalFolders []string `yaml:"globalFolders" name:"global-folders" explode:"global-folders"`
131+
GlobalFolders []string `yaml:"globalFolders" name:"global-folders" explode:"global-folder"`
132132
Packages []NpmPackage `explode:"package"`
133133
}
134134

pkg/cli/bind.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func (f *flagConfigBinder) setValue(ctx *bindContext) error {
5050
}
5151
err := ctx.setFieldFromStruct()
5252
if err != nil {
53+
if len(ctx.paths) > 1 {
54+
ctx.paths = []string{strings.Join(ctx.paths, "-")}
55+
}
5356
if len(ctx.paths) == 1 {
5457
if arr, ok := ctx.value.([]string); ok {
5558
return f.explode(ctx.element, ctx.paths[0], arr)

pkg/cmd/mokapi/flags/providers_npm_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"mokapi/config/static"
55
"mokapi/pkg/cli"
66
"mokapi/pkg/cmd/mokapi"
7+
"os"
78
"testing"
89

910
"github.com/stretchr/testify/require"
@@ -64,3 +65,25 @@ func TestRoot_Providers_Npm(t *testing.T) {
6465
})
6566
}
6667
}
68+
69+
func TestProviderNpm_Env(t *testing.T) {
70+
key := "MOKAPI_PROVIDERS_NPM_GLOBAL_FOLDER"
71+
err := os.Setenv(key, "/npm")
72+
require.NoError(t, err)
73+
t.Cleanup(func() {
74+
_ = os.Unsetenv(key)
75+
})
76+
77+
cmd := mokapi.NewCmdMokapi()
78+
cmd.SetArgs([]string{})
79+
80+
cfg := static.NewConfig()
81+
cmd.Run = func(cmd *cli.Command, args []string) error {
82+
cfg = cmd.Config.(*static.Config)
83+
return nil
84+
}
85+
err = cmd.Execute()
86+
require.NoError(t, err)
87+
88+
require.Equal(t, []string{"/npm"}, cfg.Providers.Npm.GlobalFolders)
89+
}

0 commit comments

Comments
 (0)