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

refactor: replace cmd arguments from string array to map #492

Merged
merged 1 commit into from
Aug 24, 2023
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
37 changes: 18 additions & 19 deletions pkg/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

previewcmd "kusionstack.io/kusion/pkg/cmd/preview"
"kusionstack.io/kusion/pkg/cmd/spec"
"kusionstack.io/kusion/pkg/cmd/util"
"kusionstack.io/kusion/pkg/engine/backend"
_ "kusionstack.io/kusion/pkg/engine/backend/init"
"kusionstack.io/kusion/pkg/engine/operation"
Expand All @@ -26,42 +25,42 @@ import (
"kusionstack.io/kusion/pkg/util/pretty"
)

// ApplyOptions defines flags for the `apply` command
type ApplyOptions struct {
previewcmd.PreviewOptions
ApplyFlag
// Options defines flags for the `apply` command
type Options struct {
previewcmd.Options
Flag
}

type ApplyFlag struct {
type Flag struct {
Yes bool
DryRun bool
Watch bool
}

// NewApplyOptions returns a new ApplyOptions instance
func NewApplyOptions() *ApplyOptions {
return &ApplyOptions{
PreviewOptions: *previewcmd.NewPreviewOptions(),
func NewApplyOptions() *Options {
return &Options{
Options: *previewcmd.NewPreviewOptions(),
}
}

func (o *ApplyOptions) Complete(args []string) {
o.CompileOptions.Complete(args)
func (o *Options) Complete(args []string) {
o.Options.Complete(args)
}

func (o *ApplyOptions) Validate() error {
return o.CompileOptions.Validate()
func (o *Options) Validate() error {
return o.Options.Validate()
}

func (o *ApplyOptions) Run() error {
func (o *Options) Run() error {
// Set no style
if o.NoStyle {
pterm.DisableStyling()
pterm.DisableColor()
}

// Parse project and stack of work directory
project, stack, err := projectstack.DetectProjectAndStack(o.CompileOptions.WorkDir)
project, stack, err := projectstack.DetectProjectAndStack(o.Options.WorkDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -101,7 +100,7 @@ func (o *ApplyOptions) Run() error {
}

// Compute changes for preview
changes, err := previewcmd.Preview(&o.PreviewOptions, stateStorage, sp, project, stack)
changes, err := previewcmd.Preview(&o.Options, stateStorage, sp, project, stack)
if err != nil {
return err
}
Expand Down Expand Up @@ -188,7 +187,7 @@ func (o *ApplyOptions) Run() error {
// return err
// }
func Apply(
o *ApplyOptions,
o *Options,
storage states.StateStorage,
planResources *models.Spec,
changes *opsmodels.Changes,
Expand Down Expand Up @@ -293,7 +292,7 @@ func Apply(
close(ac.MsgCh)
} else {
// parse cluster in arguments
cluster := util.ParseClusterArgument(o.Arguments)
cluster := o.Arguments["cluster"]
_, st := ac.Apply(&operation.ApplyRequest{
Request: opsmodels.Request{
Tenant: changes.Project().Tenant,
Expand Down Expand Up @@ -332,7 +331,7 @@ func Apply(
// return err
// }
func Watch(
o *ApplyOptions,
o *Options,
planResources *models.Spec,
changes *opsmodels.Changes,
) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/check/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ func TestNewCmdCheck(t *testing.T) {
t.Run("", func(t *testing.T) {
defer monkey.UnpatchAll()

monkey.Patch((*compile.CompileOptions).Complete, func(o *compile.CompileOptions, args []string) error {
monkey.Patch((*compile.Options).Complete, func(o *compile.Options, args []string) error {
o.Output = "stdout"
return nil
})
monkey.Patch((*compile.CompileOptions).Run, func(*compile.CompileOptions) error {
monkey.Patch((*compile.Options).Run, func(*compile.Options) error {
return nil
})

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewCmdCompile() *cobra.Command {
# Compile main.k and write result into output.yaml
kusion compile main.k -o output.yaml

# Complie without output style and color
# Compile without output style and color
kusion compile --no-style=true`)
)

Expand Down Expand Up @@ -71,12 +71,12 @@ func NewCmdCompile() *cobra.Command {
return cmd
}

func (o *CompileOptions) AddCompileFlags(cmd *cobra.Command) {
func (o *Options) AddCompileFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.WorkDir, "workdir", "w", "",
i18n.T("Specify the work directory"))
cmd.Flags().StringSliceVarP(&o.Settings, "setting", "Y", []string{},
i18n.T("Specify the command line setting files"))
cmd.Flags().StringArrayVarP(&o.Arguments, "argument", "D", []string{},
cmd.Flags().StringToStringVarP(&o.Arguments, "argument", "D", map[string]string{},
i18n.T("Specify the top-level argument"))
cmd.Flags().StringSliceVarP(&o.Overrides, "overrides", "O", []string{},
i18n.T("Specify the configuration override path and value"))
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/compile/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

func TestNewCmdCompile(t *testing.T) {
m1 := mockey.Mock((*CompileOptions).Complete).To(func(o *CompileOptions, args []string) error {
m1 := mockey.Mock((*Options).Complete).To(func(o *Options, args []string) error {
o.Output = "stdout"
return nil
}).Build()
m2 := mockey.Mock((*CompileOptions).Run).To(func(*CompileOptions) error {
m2 := mockey.Mock((*Options).Run).To(func(*Options) error {
return nil
}).Build()
defer m1.UnPatch()
Expand Down
24 changes: 12 additions & 12 deletions pkg/cmd/compile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import (
"kusionstack.io/kusion/pkg/projectstack"
)

type CompileOptions struct {
type Options struct {
IsCheck bool
Filenames []string
CompileFlags
Flags
}

type CompileFlags struct {
type Flags struct {
Output string
WorkDir string
Settings []string
Arguments []string
Arguments map[string]string
Overrides []string
DisableNone bool
OverrideAST bool
Expand All @@ -33,23 +33,23 @@ type CompileFlags struct {

const Stdout = "stdout"

func NewCompileOptions() *CompileOptions {
return &CompileOptions{
func NewCompileOptions() *Options {
return &Options{
Filenames: []string{},
CompileFlags: CompileFlags{
Flags: Flags{
Settings: []string{},
Arguments: []string{},
Arguments: map[string]string{},
Overrides: []string{},
},
}
}

func (o *CompileOptions) Complete(args []string) error {
func (o *Options) Complete(args []string) error {
o.Filenames = args
return o.PreSet(projectstack.IsStack)
}

func (o *CompileOptions) Validate() error {
func (o *Options) Validate() error {
var wrongFiles []string
for _, filename := range o.Filenames {
if filepath.Ext(filename) != ".k" {
Expand All @@ -62,7 +62,7 @@ func (o *CompileOptions) Validate() error {
return nil
}

func (o *CompileOptions) Run() error {
func (o *Options) Run() error {
// Set no style
if o.NoStyle {
pterm.DisableStyling()
Expand Down Expand Up @@ -114,7 +114,7 @@ func (o *CompileOptions) Run() error {
return nil
}

func (o *CompileOptions) PreSet(preCheck func(cur string) bool) error {
func (o *Options) PreSet(preCheck func(cur string) bool) error {
curDir := o.WorkDir
if o.WorkDir == "" {
curDir, _ = os.Getwd()
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/deps/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestDepsOptions_Validate(t *testing.T) {
}
for _, tc := range tCases {
t.Run(tc.name, func(t *testing.T) {
opt := DepsOptions{
opt := Options{
workDir: tc.workDir,
Direct: tc.direct,
Only: tc.only,
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestDepsOptions_Run(t *testing.T) {
}
for _, tc := range tCases {
t.Run(tc.direct, func(t *testing.T) {
opt := DepsOptions{
opt := Options{
workDir: tc.workDir,
Direct: tc.direct,
Only: tc.only,
Expand All @@ -156,7 +156,7 @@ func TestDepsOptions_Run(t *testing.T) {
}

func TestDepsOptions_Run2(t *testing.T) {
opt := DepsOptions{
opt := Options{
workDir: workDir,
Direct: "up",
Focus: []string{
Expand Down
19 changes: 12 additions & 7 deletions pkg/cmd/deps/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"kusionstack.io/kusion/pkg/projectstack"
)

type DepsOptions struct {
type Options struct {
workDir string
Direct string
Focus []string
Expand Down Expand Up @@ -55,11 +55,11 @@ func (s stringSet) toSlice() []string {
return result
}

func NewDepsOptions() *DepsOptions {
return &DepsOptions{}
func NewDepsOptions() *Options {
return &Options{}
}

func (o *DepsOptions) Complete(args []string) {
func (o *Options) Complete(args []string) {
if len(args) > 0 {
o.workDir = args[0]
}
Expand All @@ -69,7 +69,7 @@ func (o *DepsOptions) Complete(args []string) {
}
}

func (o *DepsOptions) Validate() error {
func (o *Options) Validate() error {
if o.Only != "project" && o.Only != "stack" {
return fmt.Errorf("invalid output downstream type. supported types: project, stack")
}
Expand All @@ -94,7 +94,7 @@ func (o *DepsOptions) Validate() error {
return nil
}

func (o *DepsOptions) Run() (err error) {
func (o *Options) Run() (err error) {
workDir, err := filepath.Abs(o.workDir)
if err != nil {
return
Expand Down Expand Up @@ -177,7 +177,12 @@ func (o *DepsOptions) Run() (err error) {
//
// This is a very time-consuming function based on the FindAllProjectsFrom API of kusion and the ListDownStreamFiles API of kcl.
// Do not call this function with high frequency and please ensure at least 10 seconds interval when calling.
func findDownStreams(workDir string, projects []*projectstack.Project, focusPaths, shouldIgnore stringSet, projectOnly bool) (downStreams stringSet, err error) {
func findDownStreams(
workDir string,
projects []*projectstack.Project,
focusPaths, shouldIgnore stringSet,
projectOnly bool,
) (downStreams stringSet, err error) {
entrances := emptyStringSet() // all the entrance files in the work directory
entranceIndex := make(map[string]stringSet) // entrance file index to the corresponding projects/stacks
downStreams = emptyStringSet() // might be downstream projects or downstream stacks, according to the projectOnly option
Expand Down
26 changes: 13 additions & 13 deletions pkg/cmd/destroy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@ import (
"kusionstack.io/kusion/pkg/util/signals"
)

type DestroyOptions struct {
compilecmd.CompileOptions
type Options struct {
compilecmd.Options
Operator string
Yes bool
Detail bool
backend.BackendOps
}

func NewDestroyOptions() *DestroyOptions {
return &DestroyOptions{
CompileOptions: *compilecmd.NewCompileOptions(),
func NewDestroyOptions() *Options {
return &Options{
Options: *compilecmd.NewCompileOptions(),
}
}

func (o *DestroyOptions) Complete(args []string) {
o.CompileOptions.Complete(args)
func (o *Options) Complete(args []string) {
o.Options.Complete(args)
}

func (o *DestroyOptions) Validate() error {
return o.CompileOptions.Validate()
func (o *Options) Validate() error {
return o.Options.Validate()
}

func (o *DestroyOptions) Run() error {
func (o *Options) Run() error {
// listen for interrupts or the SIGTERM signal
signals.HandleInterrupt()
// Parse project and stack of work directory
project, stack, err := projectstack.DetectProjectAndStack(o.CompileOptions.WorkDir)
project, stack, err := projectstack.DetectProjectAndStack(o.Options.WorkDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func (o *DestroyOptions) Run() error {
return nil
}

func (o *DestroyOptions) preview(
func (o *Options) preview(
planResources *models.Spec, project *projectstack.Project,
stack *projectstack.Stack, stateStorage states.StateStorage,
) (*opsmodels.Changes, error) {
Expand Down Expand Up @@ -157,7 +157,7 @@ func (o *DestroyOptions) preview(
return opsmodels.NewChanges(project, stack, rsp.Order), nil
}

func (o *DestroyOptions) destroy(planResources *models.Spec, changes *opsmodels.Changes, stateStorage states.StateStorage) error {
func (o *Options) destroy(planResources *models.Spec, changes *opsmodels.Changes, stateStorage states.StateStorage) error {
do := &operation.DestroyOperation{
Operation: opsmodels.Operation{
Stack: changes.Stack(),
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ func TestNewCmdDiff(t *testing.T) {
defer func() { os.Stdin = originalOSStdin }()

cmd := NewCmdDiff()
assert.Nil(t, cmd.Flags().Set("diff-mode", DiffModeLive))
assert.Nil(t, cmd.Flags().Set("diff-mode", ModeLive))
cmd.SetArgs([]string{"testdata/pod-full.yaml"})
err = cmd.Execute()
assert.Nil(t, err)
})

t.Run("diff by files with flags", func(t *testing.T) {
cmd := NewCmdDiff()
assert.Nil(t, cmd.Flags().Set("diff-mode", DiffModeIgnoreAdded))
assert.Nil(t, cmd.Flags().Set("diff-mode", ModeIgnoreAdded))
assert.Nil(t, cmd.Flags().Set("output", diffutil.OutputRaw))
assert.Nil(t, cmd.Flags().Set("sort-by-kubernetes-resource", "true"))
assert.Nil(t, cmd.Flags().Set("swap", "true"))
Expand All @@ -106,7 +106,7 @@ func TestNewCmdDiff(t *testing.T) {
defer func() { os.Stdin = originalOSStdin }()

cmd := NewCmdDiff()
assert.Nil(t, cmd.Flags().Set("diff-mode", DiffModeIgnoreAdded))
assert.Nil(t, cmd.Flags().Set("diff-mode", ModeIgnoreAdded))
assert.Nil(t, cmd.Flags().Set("output", diffutil.OutputRaw))
assert.Nil(t, cmd.Flags().Set("sort-by-kubernetes-resource", "true"))
assert.Nil(t, cmd.Flags().Set("swap", "true"))
Expand Down
Loading
Loading