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

feat: add flag to init cmd to specify output file path #954

Merged
merged 1 commit into from
Mar 27, 2024
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
12 changes: 9 additions & 3 deletions pkg/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -39,5 +42,8 @@ func NewCmd() *cobra.Command {
},
}

cmd.Flags().StringVarP(&o.ProjectDir, "target", "t", "",
i18n.T("specify the target direcotry"))

return cmd
}
16 changes: 13 additions & 3 deletions pkg/cmd/init/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/init/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
}
Expand Down
Loading