-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add config cmd, including subcmds get,list,set,unset
- Loading branch information
Showing
21 changed files
with
1,031 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"kusionstack.io/kusion/pkg/cmd/config/get" | ||
"kusionstack.io/kusion/pkg/cmd/config/list" | ||
"kusionstack.io/kusion/pkg/cmd/config/set" | ||
"kusionstack.io/kusion/pkg/cmd/config/unset" | ||
"kusionstack.io/kusion/pkg/util/i18n" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
var ( | ||
short = i18n.T(`Interact with the Kusion config`) | ||
|
||
long = i18n.T(` | ||
Config contains the operation of Kusion configurations.`) | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "config", | ||
Short: short, | ||
Long: templates.LongDesc(long), | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
return cmd.Help() | ||
}, | ||
} | ||
|
||
getCmd := get.NewCmd() | ||
listCmd := list.NewCmd() | ||
setCmd := set.NewCmd() | ||
unsetCmd := unset.NewCmd() | ||
cmd.AddCommand(getCmd, listCmd, setCmd, unsetCmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewCmd(t *testing.T) { | ||
t.Run("successfully get config help", func(t *testing.T) { | ||
cmd := NewCmd() | ||
assert.NotNil(t, cmd) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package get | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"kusionstack.io/kusion/pkg/cmd/util" | ||
"kusionstack.io/kusion/pkg/util/i18n" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
var ( | ||
short = i18n.T(`Get a config item`) | ||
|
||
long = i18n.T(` | ||
This command gets the value of a specified kusion config item, where the config item must be registered.`) | ||
|
||
example = i18n.T(` | ||
# Get a config item | ||
kusion config get backends.current`) | ||
) | ||
|
||
o := NewOptions() | ||
cmd := &cobra.Command{ | ||
Use: "get", | ||
Short: short, | ||
Long: templates.LongDesc(long), | ||
Example: templates.Examples(example), | ||
DisableFlagsInUseLine: true, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
defer util.RecoverErr(&err) | ||
util.CheckErr(o.Complete(args)) | ||
util.CheckErr(o.Validate()) | ||
util.CheckErr(o.Run()) | ||
return | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package get | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bytedance/mockey" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewCmd(t *testing.T) { | ||
t.Run("successfully get config item", func(t *testing.T) { | ||
mockey.PatchConvey("mock cmd", t, func() { | ||
mockey.Mock((*Options).Complete).To(func(o *Options, args []string) error { | ||
o.Item = "backends.current" | ||
return nil | ||
}).Build() | ||
mockey.Mock((*Options).Run).Return(nil).Build() | ||
|
||
cmd := NewCmd() | ||
err := cmd.Execute() | ||
assert.Nil(t, err) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package get | ||
|
||
import ( | ||
"fmt" | ||
|
||
"kusionstack.io/kusion/pkg/cmd/config/util" | ||
"kusionstack.io/kusion/pkg/config" | ||
) | ||
|
||
type Options struct { | ||
Item string | ||
} | ||
|
||
func NewOptions() *Options { | ||
return &Options{} | ||
} | ||
|
||
func (o *Options) Complete(args []string) error { | ||
item, err := util.GetItemFromArgs(args) | ||
if err != nil { | ||
return err | ||
} | ||
o.Item = item | ||
return nil | ||
} | ||
|
||
func (o *Options) Validate() error { | ||
if err := util.ValidateItem(o.Item); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (o *Options) Run() error { | ||
val, err := config.GetEncodedConfigItem(o.Item) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print(val) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package get | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bytedance/mockey" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"kusionstack.io/kusion/pkg/config" | ||
) | ||
|
||
func TestOptions_Complete(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
args []string | ||
success bool | ||
expectedOpts *Options | ||
}{ | ||
{ | ||
name: "successfully complete options", | ||
args: []string{"backends.current"}, | ||
success: true, | ||
expectedOpts: &Options{ | ||
Item: "backends.current", | ||
}, | ||
}, | ||
{ | ||
name: "complete field invalid args", | ||
args: nil, | ||
success: false, | ||
expectedOpts: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
opts := NewOptions() | ||
err := opts.Complete(tc.args) | ||
assert.Equal(t, tc.success, err == nil) | ||
if tc.success { | ||
assert.Equal(t, tc.expectedOpts, opts) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestOptions_Validate(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
opts *Options | ||
success bool | ||
}{ | ||
{ | ||
name: "valid options", | ||
opts: &Options{ | ||
Item: "backends.current", | ||
}, | ||
success: true, | ||
}, | ||
{ | ||
name: "invalid options empty config item", | ||
opts: &Options{}, | ||
success: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := tc.opts.Validate() | ||
assert.Equal(t, tc.success, err == nil) | ||
}) | ||
} | ||
} | ||
|
||
func TestOptions_Run(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
opts *Options | ||
success bool | ||
}{ | ||
{ | ||
name: "successfully run", | ||
opts: &Options{ | ||
Item: "backends.current", | ||
}, | ||
success: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockey.PatchConvey("mock get config item", t, func() { | ||
mockey.Mock(config.GetEncodedConfigItem).Return("", nil).Build() | ||
|
||
err := tc.opts.Run() | ||
assert.Equal(t, tc.success, err == nil) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package list | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
configutil "kusionstack.io/kusion/pkg/cmd/config/util" | ||
"kusionstack.io/kusion/pkg/cmd/util" | ||
"kusionstack.io/kusion/pkg/config" | ||
"kusionstack.io/kusion/pkg/util/i18n" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
var ( | ||
short = i18n.T(`List all config items`) | ||
|
||
long = i18n.T(` | ||
This command lists all the kusion config items and their values.`) | ||
|
||
example = i18n.T(` | ||
# List config items | ||
kusion config list`) | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: short, | ||
Long: templates.LongDesc(long), | ||
Example: templates.Examples(example), | ||
DisableFlagsInUseLine: true, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
defer util.RecoverErr(&err) | ||
util.CheckErr(Validate(args)) | ||
util.CheckErr(Run()) | ||
return | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func Validate(args []string) error { | ||
return configutil.ValidateNoArg(args) | ||
} | ||
|
||
func Run() error { | ||
cfg, err := config.GetConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
content, err := yaml.Marshal(cfg) | ||
if err != nil { | ||
return fmt.Errorf("yaml marshal config configuration failed: %w", err) | ||
} | ||
fmt.Print(string(content)) | ||
return nil | ||
} |
Oops, something went wrong.