From ad2a63728d9bc5103a795eeaf9d1ada1bf1fce1b Mon Sep 17 00:00:00 2001 From: skoenig Date: Sun, 24 Mar 2024 19:58:22 +0100 Subject: [PATCH] feat: add flag to init cmd to specify output file path --- pkg/cmd/init/init.go | 12 +++++++++--- pkg/cmd/init/options.go | 16 +++++++++++++--- pkg/cmd/init/options_test.go | 2 ++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/init/init.go b/pkg/cmd/init/init.go index 123abbd0..96b8af73 100644 --- a/pkg/cmd/init/init.go +++ b/pkg/cmd/init/init.go @@ -13,13 +13,16 @@ func NewCmd() *cobra.Command { long = i18n.T(` This command initializes the scaffolding for a demo project with the name of the current directory to help users quickly get started. - - Note that current directory needs to be an empty directory.`) + + Note that target directory needs to be an empty directory.`) example = i18n.T(` # Initialize a demo project with the name of the current directory mkdir quickstart && cd quickstart - kusion init`) + kusion init + + # Initialize the demo project in a different target directory + kusion init --target projects/my-demo-project`) ) o := NewOptions() @@ -39,5 +42,8 @@ func NewCmd() *cobra.Command { }, } + cmd.Flags().StringVarP(&o.ProjectDir, "target", "t", "", + i18n.T("specify the target direcotry")) + return cmd } diff --git a/pkg/cmd/init/options.go b/pkg/cmd/init/options.go index 0e63abcc..e696a9cc 100644 --- a/pkg/cmd/init/options.go +++ b/pkg/cmd/init/options.go @@ -11,12 +11,20 @@ import ( var ErrNotEmptyArgs = errors.New("no args accepted") type Options struct { - Name string + Name string + Flags +} + +type Flags struct { ProjectDir string } func NewOptions() *Options { - return &Options{} + return &Options{ + Flags: Flags{ + ProjectDir: "", + }, + } } func (o *Options) Complete(args []string) error { @@ -29,8 +37,10 @@ func (o *Options) Complete(args []string) error { return err } - o.ProjectDir = dir o.Name = name + if o.ProjectDir == "" { + o.ProjectDir = dir + } return nil } diff --git a/pkg/cmd/init/options_test.go b/pkg/cmd/init/options_test.go index 1b8aa0b0..882a59e8 100644 --- a/pkg/cmd/init/options_test.go +++ b/pkg/cmd/init/options_test.go @@ -40,10 +40,12 @@ func TestOptions_Complete(t *testing.T) { }).Build() opts := NewOptions() + opts.Flags.ProjectDir = "/dir/to/my-project" args := []string{} err := opts.Complete(args) assert.Nil(t, err) + assert.Equal(t, "/dir/to/my-project", opts.ProjectDir) }) }) }