Skip to content

Commit a4cd23c

Browse files
authored
Feat/create path flag (#3113)
* created path flag * typo-fixes * typo-fix * goimports * fixes * required fixes * docs updated * fix/binding-path
1 parent 596a122 commit a4cd23c

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

cmd/create.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ NAME
3737
3838
SYNOPSIS
3939
{{.Name}} create [-l|--language] [-t|--template] [-r|--repository]
40-
[-c|--confirm] [-v|--verbose] [path]
40+
[-p |--path] [-c|--confirm] [-v|--verbose]
4141
4242
DESCRIPTION
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.
174177
func 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

docs/reference/func_create.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NAME
1010

1111
SYNOPSIS
1212
func create [-l|--language] [-t|--template] [-r|--repository]
13-
[-c|--confirm] [-v|--verbose] [path]
13+
[-p |--path] [-c|--confirm] [-v|--verbose]
1414

1515
DESCRIPTION
1616
Creates a new function project.
@@ -70,6 +70,7 @@ func create
7070
-c, --confirm Prompt to confirm options interactively ($FUNC_CONFIRM)
7171
-h, --help help for create
7272
-l, --language string Language Runtime (see help text for list) ($FUNC_LANGUAGE)
73+
-p, --path string Path to the function project directory ($FUNC_PATH) (default ".")
7374
-r, --repository string URI to a Git repository containing the specified template ($FUNC_REPOSITORY)
7475
-t, --template string Function template. (see help text for list) ($FUNC_TEMPLATE) (default "http")
7576
-v, --verbose Print verbose logs ($FUNC_VERBOSE)

0 commit comments

Comments
 (0)