Skip to content

Commit

Permalink
feat: add flag to init cmd to specify output file path (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
skoenig committed Mar 27, 2024
1 parent 46cfdd5 commit 1a731f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
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

0 comments on commit 1a731f6

Please sign in to comment.