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:
Senthilnathan M 2022-03-02 01:26:42 +05:30 committed by GitHub
parent b97fe9c4ec
commit efb7996da7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 6 deletions

View File

@ -113,7 +113,7 @@ func runCreate(cmd *cobra.Command, args []string, newClient ClientFactory) (err
// Config
// Create a config based on args. Also uses the newClient to create a
// temporary client for completing options such as available runtimes.
cfg, err := newCreateConfig(args, newClient)
cfg, err := newCreateConfig(cmd, args, newClient)
if err != nil {
return
}
@ -159,7 +159,7 @@ func runCreateHelp(cmd *cobra.Command, args []string, newClient ClientFactory) {
tpl := createHelpTemplate(cmd)
cfg, err := newCreateConfig(args, newClient)
cfg, err := newCreateConfig(cmd, args, newClient)
failSoft(err)
client := newClient(createConfigToClientOptions(cfg))
@ -210,7 +210,7 @@ type createConfig struct {
// The client constructor function is used to create a transient client for
// accessing things like the current valid templates list, and uses the
// 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 (
path 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.
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
@ -278,6 +284,26 @@ func newCreateConfig(args []string, newClient ClientFactory) (cfg createConfig,
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.
// Note this is a deeper validation using a client already configured with a
// 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 {
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 {
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 {
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 {
fmt.Fprintf(os.Stderr, "error creating client config for flag completion: %v", err)
}