mirror of https://github.com/knative/func.git
printing helpful shorthand command (#816)
* printing helpful shorthand command * Addressed review comments * Resolved rebase conflicts * Using cmd.Root().Name() for getting the executable name
This commit is contained in:
parent
b97fe9c4ec
commit
efb7996da7
|
@ -113,7 +113,7 @@ func runCreate(cmd *cobra.Command, args []string, newClient ClientFactory) (err
|
||||||
// Config
|
// Config
|
||||||
// Create a config based on args. Also uses the newClient to create a
|
// Create a config based on args. Also uses the newClient to create a
|
||||||
// temporary client for completing options such as available runtimes.
|
// temporary client for completing options such as available runtimes.
|
||||||
cfg, err := newCreateConfig(args, newClient)
|
cfg, err := newCreateConfig(cmd, args, newClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -159,7 +159,7 @@ func runCreateHelp(cmd *cobra.Command, args []string, newClient ClientFactory) {
|
||||||
|
|
||||||
tpl := createHelpTemplate(cmd)
|
tpl := createHelpTemplate(cmd)
|
||||||
|
|
||||||
cfg, err := newCreateConfig(args, newClient)
|
cfg, err := newCreateConfig(cmd, args, newClient)
|
||||||
failSoft(err)
|
failSoft(err)
|
||||||
|
|
||||||
client := newClient(createConfigToClientOptions(cfg))
|
client := newClient(createConfigToClientOptions(cfg))
|
||||||
|
@ -210,7 +210,7 @@ type createConfig struct {
|
||||||
// The client constructor function is used to create a transient client for
|
// The client constructor function is used to create a transient client for
|
||||||
// accessing things like the current valid templates list, and uses the
|
// accessing things like the current valid templates list, and uses the
|
||||||
// current value of the config at time of prompting.
|
// current value of the config at time of prompting.
|
||||||
func newCreateConfig(args []string, newClient ClientFactory) (cfg createConfig, err error) {
|
func newCreateConfig(cmd *cobra.Command, args []string, newClient ClientFactory) (cfg createConfig, err error) {
|
||||||
var (
|
var (
|
||||||
path string
|
path string
|
||||||
dirName string
|
dirName string
|
||||||
|
@ -259,7 +259,13 @@ func newCreateConfig(args []string, newClient ClientFactory) (cfg createConfig,
|
||||||
|
|
||||||
// IN confirm mode. If also in an interactive terminal, run prompts.
|
// IN confirm mode. If also in an interactive terminal, run prompts.
|
||||||
if interactiveTerminal() {
|
if interactiveTerminal() {
|
||||||
return cfg.prompt(client)
|
createdCfg, err := cfg.prompt(client)
|
||||||
|
if err != nil {
|
||||||
|
return createdCfg, err
|
||||||
|
}
|
||||||
|
fmt.Println("Command:")
|
||||||
|
fmt.Println(singleCommand(cmd, args, createdCfg))
|
||||||
|
return createdCfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Confirming, but noninteractive
|
// Confirming, but noninteractive
|
||||||
|
@ -278,6 +284,26 @@ func newCreateConfig(args []string, newClient ClientFactory) (cfg createConfig,
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// singleCommand that could be used by the current user to minimally recreate the current state.
|
||||||
|
func singleCommand(cmd *cobra.Command, args []string, cfg createConfig) string {
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString(cmd.Root().Name()) // process executable
|
||||||
|
b.WriteString(" -l " + cfg.Runtime) // language runtime is required
|
||||||
|
if cmd.Flags().Lookup("template").Changed {
|
||||||
|
b.WriteString(" -t " + cfg.Template)
|
||||||
|
}
|
||||||
|
if cmd.Flags().Lookup("repository").Changed {
|
||||||
|
b.WriteString(" -r " + cfg.Repository)
|
||||||
|
}
|
||||||
|
if cmd.Flags().Lookup("verbose").Changed {
|
||||||
|
b.WriteString(fmt.Sprintf(" -v %v", cfg.Verbose))
|
||||||
|
}
|
||||||
|
if len(args) > 0 {
|
||||||
|
b.WriteString(" " + cfg.Path) // optional trailing <path> argument
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
// Validate the current state of the config, returning any errors.
|
// Validate the current state of the config, returning any errors.
|
||||||
// Note this is a deeper validation using a client already configured with a
|
// Note this is a deeper validation using a client already configured with a
|
||||||
// preliminary config object from flags/config, such that the client instance
|
// preliminary config object from flags/config, such that the client instance
|
||||||
|
@ -477,7 +503,7 @@ type flagCompletionFunc func(*cobra.Command, []string, string) ([]string, cobra.
|
||||||
|
|
||||||
func newRuntimeCompletionFunc(newClient ClientFactory) flagCompletionFunc {
|
func newRuntimeCompletionFunc(newClient ClientFactory) flagCompletionFunc {
|
||||||
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
cfg, err := newCreateConfig(args, newClient)
|
cfg, err := newCreateConfig(cmd, args, newClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error creating client config for flag completion: %v", err)
|
fmt.Fprintf(os.Stderr, "error creating client config for flag completion: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -488,7 +514,7 @@ func newRuntimeCompletionFunc(newClient ClientFactory) flagCompletionFunc {
|
||||||
|
|
||||||
func newTemplateCompletionFunc(newClient ClientFactory) flagCompletionFunc {
|
func newTemplateCompletionFunc(newClient ClientFactory) flagCompletionFunc {
|
||||||
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
cfg, err := newCreateConfig(args, newClient)
|
cfg, err := newCreateConfig(cmd, args, newClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error creating client config for flag completion: %v", err)
|
fmt.Fprintf(os.Stderr, "error creating client config for flag completion: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue