Skip to content

Commit

Permalink
support init with specified template (#343)
Browse files Browse the repository at this point in the history
* support kusion init with specified template

* fix judgement bug

* upgrade code style
  • Loading branch information
healthjyk committed May 12, 2023
1 parent 86d50b0 commit 650fb02
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
3 changes: 3 additions & 0 deletions pkg/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func NewCmdInit() *cobra.Command {
},
}

cmd.Flags().StringVar(
&o.TemplateName, "template-name", "",
i18n.T("The template name; if not specified, a prompt will request it"))
cmd.Flags().StringVar(
&o.ProjectName, "project-name", "",
i18n.T("The project name; if not specified, a prompt will request it"))
Expand Down
82 changes: 46 additions & 36 deletions pkg/cmd/init/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
const jsonOutput = "json"

type InitOptions struct {
TemplateNameOrURL string
Online bool
ProjectName string
Force bool
Yes bool
CustomParamsJSON string
TemplateRepoPath string
TemplateName string
Online bool
ProjectName string
Force bool
Yes bool
CustomParamsJSON string
}

func NewInitOptions() *InitOptions {
Expand All @@ -32,13 +33,13 @@ func NewInitOptions() *InitOptions {

func (o *InitOptions) Complete(args []string) error {
if o.Online { // use online templates, official link or user-specified link
o.TemplateNameOrURL = getURL(args)
o.TemplateRepoPath = onlineTemplateRepoPath(args)
} else { // use offline templates, internal templates or user-specified local dir
path, err := getPath(args)
path, err := localTemplateRepoPath(args)
if err != nil {
return err
}
o.TemplateNameOrURL = path
o.TemplateRepoPath = path
}
return nil
}
Expand All @@ -48,15 +49,15 @@ func (o *InitOptions) Validate() error {
return nil
}
// offline mode may need to generate templates
if err := validatePath(o.TemplateNameOrURL); err != nil {
if err := validateLocalTemplateRepoPath(o.TemplateRepoPath); err != nil {
return err
}
return nil
}

func (o *InitOptions) Run() error {
// Retrieve the template repo.
repo, err := retrieveTemplateRepo(o.TemplateNameOrURL, o.Online)
repo, err := retrieveTemplateRepo(o.TemplateRepoPath, o.Online)
if err != nil {
return err
}
Expand All @@ -70,8 +71,24 @@ func (o *InitOptions) Run() error {

// Choose template
var template scaffold.Template
if template, err = chooseTemplate(templates); err != nil {
return err
if o.TemplateName == "" {
// if template name is not specified, choose template by prompt
if template, err = chooseTemplate(templates); err != nil {
return err
}
} else {
// if template name is specified, find template from repo data
var templateExist bool
for _, t := range templates {
if t.Name == o.TemplateName {
templateExist = true
template = t
break
}
}
if !templateExist {
return fmt.Errorf("template %s does not exist", o.TemplateName)
}
}

// Parse customParams if not empty
Expand Down Expand Up @@ -182,10 +199,9 @@ func (o *InitOptions) Run() error {
}

type TemplatesOptions struct {
Online bool
URL string
Path string
Output string
TemplateRepoPath string
Online bool
Output string
}

func NewTemplatesOptions() *TemplatesOptions {
Expand All @@ -195,12 +211,12 @@ func NewTemplatesOptions() *TemplatesOptions {
func (o *TemplatesOptions) Complete(args []string, online bool) error {
o.Online = online
if o.Online {
o.URL = getURL(args)
o.TemplateRepoPath = onlineTemplateRepoPath(args)
} else {
if path, err := getPath(args); err != nil {
if path, err := localTemplateRepoPath(args); err != nil {
return err
} else {
o.Path = path
o.TemplateRepoPath = path
}
}
return nil
Expand All @@ -211,22 +227,16 @@ func (o *TemplatesOptions) Validate() error {
return errors.New("invalid output type, supported types: json")
}
if !o.Online {
if err := validatePath(o.Path); err != nil {
if err := validateLocalTemplateRepoPath(o.TemplateRepoPath); err != nil {
return err
}
}
return nil
}

func (o *TemplatesOptions) Run() error {
var templateName string
if o.Online {
templateName = o.URL
} else {
templateName = o.Path
}
// retrieve template repo
repo, err := retrieveTemplateRepo(templateName, o.Online)
repo, err := retrieveTemplateRepo(o.TemplateRepoPath, o.Online)
if err != nil {
return err
}
Expand All @@ -247,18 +257,18 @@ func (o *TemplatesOptions) Run() error {
return nil
}

// getURL parses url from args, called when --online is true.
func getURL(args []string) string {
// onlineTemplateRepoPath parses url from args, called when --online is true.
func onlineTemplateRepoPath(args []string) string {
if len(args) > 0 {
// user-specified link
return args[0]
}
return "" // use official link
}

// getPath parses path from args, if not specified, use default InternalTemplateDir,
// localTemplateRepoPath parses path from args, if not specified, use default InternalTemplateDir,
// called when --online is false.
func getPath(args []string) (string, error) {
func localTemplateRepoPath(args []string) (string, error) {
if len(args) > 0 {
// user-specified local dir
return args[0], nil
Expand All @@ -272,8 +282,8 @@ func getPath(args []string) (string, error) {
}
}

// validatePath checks the path is valid or not.
func validatePath(path string) error {
// validateLocalTemplateRepoPath checks the path is valid or not.
func validateLocalTemplateRepoPath(path string) error {
// offline mode may need to generate templates
internalTemplateDir, err := scaffold.GetTemplateDir(scaffold.InternalTemplateDir)
if err != nil {
Expand All @@ -289,8 +299,8 @@ func validatePath(path string) error {
}

// retrieveTemplateRepo gets template repos from online or local, with specified url or path.
func retrieveTemplateRepo(templateName string, online bool) (scaffold.TemplateRepository, error) {
return scaffold.RetrieveTemplates(templateName, online)
func retrieveTemplateRepo(templateRepoPath string, online bool) (scaffold.TemplateRepository, error) {
return scaffold.RetrieveTemplates(templateRepoPath, online)
}

// deleteTemplateRepo is used to delete the files of the template repos, log warn if failed.
Expand Down

0 comments on commit 650fb02

Please sign in to comment.