3737
3838SYNOPSIS
3939 {{.Name}} create [-l|--language] [-t|--template] [-r|--repository]
40- [-c|--confirm] [-v|--verbose] [path ]
40+ [-p |--path] [- c|--confirm] [-v|--verbose]
4141
4242DESCRIPTION
4343 Creates a new function project.
@@ -71,7 +71,7 @@ EXAMPLES
7171 $ {{.Name}} create -l go -t cloudevents myfunc
7272 ` ,
7373 SuggestFor : []string {"vreate" , "creaet" , "craete" , "new" },
74- PreRunE : bindEnv ("language" , "template" , "repository" , "confirm" , "verbose" ),
74+ PreRunE : bindEnv ("language" , "template" , "repository" , "confirm" , "verbose" , "path" ),
7575 Aliases : []string {"init" },
7676 RunE : func (cmd * cobra.Command , args []string ) error {
7777 return runCreate (cmd , args , newClient )
@@ -90,7 +90,10 @@ EXAMPLES
9090 cmd .Flags ().StringP ("repository" , "r" , "" , "URI to a Git repository containing the specified template ($FUNC_REPOSITORY)" )
9191
9292 addConfirmFlag (cmd , cfg .Confirm )
93- // TODO: refactor to use --path like all the other commands
93+ // Add --path flag (default ".") for consistency with other commands.
94+ // Retain positional [path] for backward compatibility (warned later).
95+ cmd .Flags ().StringP ("path" , "p" , "." , "Path to the function project directory ($FUNC_PATH)" )
96+
9497 addVerboseFlag (cmd , cfg .Verbose )
9598
9699 // Help Action
@@ -173,22 +176,29 @@ type createConfig struct {
173176// current value of the config at time of prompting.
174177func newCreateConfig (cmd * cobra.Command , args []string , newClient ClientFactory ) (cfg createConfig , err error ) {
175178 var (
176- path string
179+ pathFlag string
177180 dirName string
178181 absolutePath string
179182 )
180183
181- if len (args ) >= 1 {
182- path = args [0 ]
184+ pathFlag = viper .GetString ("path" )
185+
186+ finalPath := "."
187+ if pathFlag != "" && pathFlag != "." {
188+ finalPath = pathFlag
189+ } else if len (args ) >= 1 && args [0 ] != "" {
190+ finalPath = args [0 ]
183191 }
184192
193+ dirName , absolutePath = deriveNameAndAbsolutePathFromPath (finalPath )
194+
185195 // Convert the path to an absolute path, and extract the ending directory name
186196 // as the function name. TODO: refactor to be git-like with no name up-front
187197 // and set instead as a named one-to-many deploy target.
188- dirName , absolutePath = deriveNameAndAbsolutePathFromPath (path )
189198
190199 // Config is the final default values based off the execution context.
191200 // When prompting, these become the defaults presented.
201+
192202 cfg = createConfig {
193203 Name : dirName , // TODO: refactor to be git-like
194204 Path : absolutePath ,
@@ -198,6 +208,7 @@ func newCreateConfig(cmd *cobra.Command, args []string, newClient ClientFactory)
198208 Confirm : viper .GetBool ("confirm" ),
199209 Verbose : viper .GetBool ("verbose" ),
200210 }
211+
201212 // If not in confirm/prompting mode, this cfg structure is complete.
202213 if ! cfg .Confirm {
203214 return
0 commit comments