@@ -18,12 +18,13 @@ import (
1818const jsonOutput = "json"
1919
2020type InitOptions struct {
21- TemplateNameOrURL string
22- Online bool
23- ProjectName string
24- Force bool
25- Yes bool
26- CustomParamsJSON string
21+ TemplateRepoPath string
22+ TemplateName string
23+ Online bool
24+ ProjectName string
25+ Force bool
26+ Yes bool
27+ CustomParamsJSON string
2728}
2829
2930func NewInitOptions () * InitOptions {
@@ -32,13 +33,13 @@ func NewInitOptions() *InitOptions {
3233
3334func (o * InitOptions ) Complete (args []string ) error {
3435 if o .Online { // use online templates, official link or user-specified link
35- o .TemplateNameOrURL = getURL (args )
36+ o .TemplateRepoPath = onlineTemplateRepoPath (args )
3637 } else { // use offline templates, internal templates or user-specified local dir
37- path , err := getPath (args )
38+ path , err := localTemplateRepoPath (args )
3839 if err != nil {
3940 return err
4041 }
41- o .TemplateNameOrURL = path
42+ o .TemplateRepoPath = path
4243 }
4344 return nil
4445}
@@ -48,15 +49,15 @@ func (o *InitOptions) Validate() error {
4849 return nil
4950 }
5051 // offline mode may need to generate templates
51- if err := validatePath (o .TemplateNameOrURL ); err != nil {
52+ if err := validateLocalTemplateRepoPath (o .TemplateRepoPath ); err != nil {
5253 return err
5354 }
5455 return nil
5556}
5657
5758func (o * InitOptions ) Run () error {
5859 // Retrieve the template repo.
59- repo , err := retrieveTemplateRepo (o .TemplateNameOrURL , o .Online )
60+ repo , err := retrieveTemplateRepo (o .TemplateRepoPath , o .Online )
6061 if err != nil {
6162 return err
6263 }
@@ -70,8 +71,24 @@ func (o *InitOptions) Run() error {
7071
7172 // Choose template
7273 var template scaffold.Template
73- if template , err = chooseTemplate (templates ); err != nil {
74- return err
74+ if o .TemplateName == "" {
75+ // if template name is not specified, choose template by prompt
76+ if template , err = chooseTemplate (templates ); err != nil {
77+ return err
78+ }
79+ } else {
80+ // if template name is specified, find template from repo data
81+ var templateExist bool
82+ for _ , t := range templates {
83+ if t .Name == o .TemplateName {
84+ templateExist = true
85+ template = t
86+ break
87+ }
88+ }
89+ if ! templateExist {
90+ return fmt .Errorf ("template %s does not exist" , o .TemplateName )
91+ }
7592 }
7693
7794 // Parse customParams if not empty
@@ -182,10 +199,9 @@ func (o *InitOptions) Run() error {
182199}
183200
184201type TemplatesOptions struct {
185- Online bool
186- URL string
187- Path string
188- Output string
202+ TemplateRepoPath string
203+ Online bool
204+ Output string
189205}
190206
191207func NewTemplatesOptions () * TemplatesOptions {
@@ -195,12 +211,12 @@ func NewTemplatesOptions() *TemplatesOptions {
195211func (o * TemplatesOptions ) Complete (args []string , online bool ) error {
196212 o .Online = online
197213 if o .Online {
198- o .URL = getURL (args )
214+ o .TemplateRepoPath = onlineTemplateRepoPath (args )
199215 } else {
200- if path , err := getPath (args ); err != nil {
216+ if path , err := localTemplateRepoPath (args ); err != nil {
201217 return err
202218 } else {
203- o .Path = path
219+ o .TemplateRepoPath = path
204220 }
205221 }
206222 return nil
@@ -211,22 +227,16 @@ func (o *TemplatesOptions) Validate() error {
211227 return errors .New ("invalid output type, supported types: json" )
212228 }
213229 if ! o .Online {
214- if err := validatePath (o .Path ); err != nil {
230+ if err := validateLocalTemplateRepoPath (o .TemplateRepoPath ); err != nil {
215231 return err
216232 }
217233 }
218234 return nil
219235}
220236
221237func (o * TemplatesOptions ) Run () error {
222- var templateName string
223- if o .Online {
224- templateName = o .URL
225- } else {
226- templateName = o .Path
227- }
228238 // retrieve template repo
229- repo , err := retrieveTemplateRepo (templateName , o .Online )
239+ repo , err := retrieveTemplateRepo (o . TemplateRepoPath , o .Online )
230240 if err != nil {
231241 return err
232242 }
@@ -247,18 +257,18 @@ func (o *TemplatesOptions) Run() error {
247257 return nil
248258}
249259
250- // getURL parses url from args, called when --online is true.
251- func getURL (args []string ) string {
260+ // onlineTemplateRepoPath parses url from args, called when --online is true.
261+ func onlineTemplateRepoPath (args []string ) string {
252262 if len (args ) > 0 {
253263 // user-specified link
254264 return args [0 ]
255265 }
256266 return "" // use official link
257267}
258268
259- // getPath parses path from args, if not specified, use default InternalTemplateDir,
269+ // localTemplateRepoPath parses path from args, if not specified, use default InternalTemplateDir,
260270// called when --online is false.
261- func getPath (args []string ) (string , error ) {
271+ func localTemplateRepoPath (args []string ) (string , error ) {
262272 if len (args ) > 0 {
263273 // user-specified local dir
264274 return args [0 ], nil
@@ -272,8 +282,8 @@ func getPath(args []string) (string, error) {
272282 }
273283}
274284
275- // validatePath checks the path is valid or not.
276- func validatePath (path string ) error {
285+ // validateLocalTemplateRepoPath checks the path is valid or not.
286+ func validateLocalTemplateRepoPath (path string ) error {
277287 // offline mode may need to generate templates
278288 internalTemplateDir , err := scaffold .GetTemplateDir (scaffold .InternalTemplateDir )
279289 if err != nil {
@@ -289,8 +299,8 @@ func validatePath(path string) error {
289299}
290300
291301// retrieveTemplateRepo gets template repos from online or local, with specified url or path.
292- func retrieveTemplateRepo (templateName string , online bool ) (scaffold.TemplateRepository , error ) {
293- return scaffold .RetrieveTemplates (templateName , online )
302+ func retrieveTemplateRepo (templateRepoPath string , online bool ) (scaffold.TemplateRepository , error ) {
303+ return scaffold .RetrieveTemplates (templateRepoPath , online )
294304}
295305
296306// deleteTemplateRepo is used to delete the files of the template repos, log warn if failed.
0 commit comments