Skip to content

Commit

Permalink
feat: update init cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-hm19 committed Mar 6, 2024
1 parent e80f90e commit ff120b9
Show file tree
Hide file tree
Showing 26 changed files with 477 additions and 2,062 deletions.
15 changes: 1 addition & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/didi/gendry v1.7.0
github.com/djherbis/times v1.5.0
github.com/evanphx/json-patch v4.12.0+incompatible
github.com/go-git/go-git/v5 v5.11.0
github.com/go-sql-driver/mysql v1.6.0
github.com/go-test/deep v1.0.3
github.com/goccy/go-yaml v1.11.0
Expand Down Expand Up @@ -90,6 +89,7 @@ require (
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/go-git/v5 v5.11.0 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
Expand Down Expand Up @@ -129,7 +129,6 @@ require (
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/aliyun/aliyun-secretsmanager-client-go v1.1.4
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
Expand All @@ -146,12 +145,10 @@ require (
github.com/aws/smithy-go v1.17.0 // indirect
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/jsonv v1.1.3 // indirect
github.com/chai2010/protorpc v1.1.4 // indirect
github.com/cheggaaa/pb v1.0.29 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/containerd/containerd v1.7.5 // indirect
Expand All @@ -177,7 +174,6 @@ require (
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
Expand Down Expand Up @@ -235,8 +231,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opentracing/basictracer-go v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/otiai10/copy v1.12.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -249,8 +243,6 @@ require (
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
Expand All @@ -260,9 +252,6 @@ require (
github.com/thoas/go-funk v0.9.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tweekmonster/luser v0.0.0-20161003172636-3fa38070dbd7 // indirect
github.com/uber/jaeger-client-go v2.30.0+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
Expand Down Expand Up @@ -291,11 +280,9 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
k8s.io/klog/v2 v2.100.1
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
lukechampine.com/frand v1.4.2 // indirect
oras.land/oras-go v1.2.3 // indirect
oras.land/oras-go/v2 v2.3.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20211028080628-e2786a622600 // indirect
)
39 changes: 0 additions & 39 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewKusionctlCmd(o KusionctlOptions) *cobra.Command {
Message: "Configuration Commands:",
Commands: []*cobra.Command{
workspace.NewCmd(),
cmdinit.NewCmdInit(),
cmdinit.NewCmd(),
build.NewCmdBuild(),
},
},
Expand Down
103 changes: 16 additions & 87 deletions pkg/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,112 +3,41 @@ package init
import (
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"

"kusionstack.io/kusion/pkg/cmd/util"
"kusionstack.io/kusion/pkg/util/i18n"
)

func NewCmdInit() *cobra.Command {
func NewCmd() *cobra.Command {
var (
initShort = i18n.T(`Initialize the scaffolding for a project`)

initLong = i18n.T(`
This command initializes the scaffolding for a project, generating a project from an appointed template with correct structure.
The scaffold templates can be retrieved from local or online. The built-in templates are used by default, self-defined templates are also supported by assigning the template repository path.`)
short = i18n.T(`Initialize the scaffolding for a demo project`)

initExample = i18n.T(`
# Initialize a project from internal templates
kusion init
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.`)

# Initialize a project from default online templates
kusion init --online=true
# Initialize a project from a specific online template
kusion init https://github.com/<user>/<repo> --online=true
# Initialize a project from a specific local template
kusion init /path/to/templates`)
example = i18n.T(`
# Initialize a demo project with the name of the current directory
mkdir quickstart && cd quickstart
kusion init`)
)

o := NewInitOptions()
o := NewOptions()
cmd := &cobra.Command{
Use: "init",
Short: initShort,
Long: templates.LongDesc(initLong),
Example: templates.Examples(initExample),
DisableFlagsInUseLine: true,
Use: "init",
Short: short,
Long: templates.LongDesc(long),
Example: templates.Examples(example),
SilenceErrors: 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
},
}

cmd.Flags().StringVar(
&o.TemplateName, "template-name", "",
i18n.T("Initialize with specified template. If not specified, a prompt will request it"))
cmd.Flags().StringVar(
&o.ProjectName, "project-name", "",
i18n.T("Initialize with specified project name. If not specified, a prompt will request it"))
cmd.Flags().BoolVar(
&o.Force, "force", false,
i18n.T("Force generating the scaffolding files, even if it would change the existing files"))
cmd.PersistentFlags().BoolVar(
&o.Online, "online", false,
i18n.T("Use templates from online repository to initialize project, or use locally cached templates"))
cmd.Flags().BoolVar(
&o.Yes, "yes", false,
i18n.T("Skip prompts and proceed with default values"))
cmd.Flags().StringVar(
&o.CustomParamsJSON, "custom-params", "",
i18n.T("Custom params in JSON. If specified, it will be used as the template default value and skip prompts"))

templatesCmd := newCmdTemplates()
cmd.AddCommand(templatesCmd)
return cmd
}

func newCmdTemplates() *cobra.Command {
var (
templatesShort = i18n.T(`List templates used to initialize a project`)

templatesLong = i18n.T(`
This command gets the descriptions and definitions of the templates which are used to initialize the project scaffolding.`)

templatesExample = i18n.T(`
# Get name and description of internal templates
kusion init templates
# Get templates from specific templates repository
kusion init templates https://github.com/<user>/<repo> --online=true`)
)

o := NewTemplatesOptions()
cmd := &cobra.Command{
Use: "templates",
Short: templatesShort,
Long: templates.LongDesc(templatesLong),
Example: templates.Examples(templatesExample),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer util.RecoverErr(&err)
online, err := cmd.InheritedFlags().GetBool("online")
if err != nil {
return err
}
util.CheckErr(o.Complete(args, online))
util.CheckErr(o.Validate())
util.CheckErr(o.Run())
return
},
}

cmd.Flags().StringVarP(
&o.Output, "output", "o", "",
i18n.T("Specify the output format of templates. If specified, only support json for now; if not, template name and description is given"))

return cmd
}
23 changes: 10 additions & 13 deletions pkg/cmd/init/init_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package init

import (
"os"
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"
)

func Test_CmdInit(t *testing.T) {
mockey.PatchConvey("cmd init", t, func() {
// patch human interact
patchChooseTemplate()
patchPromptValue()
func TestNewCmd(t *testing.T) {
t.Run("successfully initiate a demo project", func(t *testing.T) {
mockey.PatchConvey("mock complete, validate and run", t, func() {
mockey.Mock((*Options).Complete).Return(nil).Build()
mockey.Mock((*Options).Validate).Return(nil).Build()
mockey.Mock((*Options).Run).Return(nil).Build()

cmd := NewCmdInit()
_ = cmd.Flags().Set("project-name", "test")
// clean data
defer os.RemoveAll("test")

err := cmd.Execute()
assert.Nil(t, err)
cmd := NewCmd()
err := cmd.Execute()
assert.Nil(t, err)
})
})
}
Loading

0 comments on commit ff120b9

Please sign in to comment.