-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
31 lines (29 loc) · 1.06 KB
/
command.go
File metadata and controls
31 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package cobrashell
import "github.com/spf13/cobra"
// Command returns a *cobra.Command with Use "shell" that starts an interactive
// shell wrapping the binary described by cfg. It is intended to be added as a
// subcommand of a Cobra CLI so that users can run `mybinary shell` to enter an
// interactive session:
//
// rootCmd.AddCommand(cobrashell.Command(cobrashell.Config{
// BinaryPath: os.Args[0],
// Prompt: "myapp> ",
// }))
//
// When BinaryPath is set to os.Args[0], the shell wraps the running binary
// itself. New resolves the path to an absolute path immediately, so it remains
// valid even if the process changes its working directory.
func Command(cfg Config) *cobra.Command {
return &cobra.Command{
Use: "shell",
Short: "Start an interactive shell",
Long: "Start an interactive shell that wraps this binary's commands " +
"with tab completion and persistent history.",
Args: cobra.NoArgs,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
return New(cfg).Run()
},
}
}