refactor(cli): hide deprecated/internal subcommands and flags

This commit is contained in:
L. Yeung 2024-09-17 03:16:43 +08:00 committed by Jan De Dobbeleer
parent d2e04b6654
commit 9ce9ed72bb
6 changed files with 174 additions and 136 deletions

View file

@ -15,56 +15,68 @@ import (
)
// debugCmd represents the prompt command
var debugCmd = &cobra.Command{
Use: "debug [bash|zsh|fish|powershell|pwsh|cmd|nu|tcsh|elvish|xonsh]",
Short: "Print the prompt in debug mode",
Long: "Print the prompt in debug mode.",
ValidArgs: supportedShells,
Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) {
startTime := time.Now()
if len(args) == 0 {
_ = cmd.Help()
return
}
env := &runtime.Terminal{
CmdFlags: &runtime.Flags{
Config: configFlag,
Debug: true,
PWD: pwd,
Shell: args[0],
Plain: plain,
},
}
env.Init()
defer env.Close()
cfg := config.Load(env)
// add variables to the environment
env.Var = cfg.Var
terminal.Init(shell.GENERIC)
terminal.BackgroundColor = cfg.TerminalBackground.ResolveTemplate(env)
terminal.Colors = cfg.MakeColors()
terminal.Plain = plain
eng := &prompt.Engine{
Config: cfg,
Env: env,
Plain: plain,
}
fmt.Print(eng.PrintDebug(startTime, build.Version))
},
}
var debugCmd = createDebugCmd()
func init() {
debugCmd.Flags().StringVar(&pwd, "pwd", "", "current working directory")
debugCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for")
debugCmd.Flags().BoolVarP(&plain, "plain", "p", false, "plain text output (no ANSI)")
RootCmd.AddCommand(debugCmd)
}
func createDebugCmd() *cobra.Command {
debugCmd := &cobra.Command{
Use: "debug [bash|zsh|fish|powershell|pwsh|cmd|nu|tcsh|elvish|xonsh]",
Short: "Print the prompt in debug mode",
Long: "Print the prompt in debug mode.",
ValidArgs: supportedShells,
Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) {
startTime := time.Now()
if len(args) == 0 {
_ = cmd.Help()
return
}
env := &runtime.Terminal{
CmdFlags: &runtime.Flags{
Config: configFlag,
Debug: true,
PWD: pwd,
Shell: args[0],
Plain: plain,
},
}
env.Init()
defer env.Close()
cfg := config.Load(env)
// add variables to the environment
env.Var = cfg.Var
terminal.Init(shell.GENERIC)
terminal.BackgroundColor = cfg.TerminalBackground.ResolveTemplate(env)
terminal.Colors = cfg.MakeColors()
terminal.Plain = plain
eng := &prompt.Engine{
Config: cfg,
Env: env,
Plain: plain,
}
fmt.Print(eng.PrintDebug(startTime, build.Version))
},
}
debugCmd.Flags().StringVar(&pwd, "pwd", "", "current working directory")
debugCmd.Flags().BoolVarP(&plain, "plain", "p", false, "plain text output (no ANSI)")
// Deprecated flags, should be kept to avoid breaking CLI integration.
debugCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for")
// Hide flags that are deprecated or for internal use only.
_ = debugCmd.Flags().MarkHidden("shell")
return debugCmd
}

View file

@ -30,7 +30,15 @@ var (
"xonsh",
}
initCmd = &cobra.Command{
initCmd = createInitCmd()
)
func init() {
RootCmd.AddCommand(initCmd)
}
func createInitCmd() *cobra.Command {
initCmd := &cobra.Command{
Use: "init [bash|zsh|fish|powershell|pwsh|cmd|nu|tcsh|elvish|xonsh]",
Short: "Initialize your shell and config",
Long: `Initialize your shell and config.
@ -46,15 +54,20 @@ See the documentation to initialize your shell: https://ohmyposh.dev/docs/instal
runInit(args[0])
},
}
)
func init() {
initCmd.Flags().BoolVarP(&printOutput, "print", "p", false, "print the init script")
initCmd.Flags().BoolVarP(&strict, "strict", "s", false, "run in strict mode")
initCmd.Flags().BoolVarP(&manual, "manual", "m", false, "enable/disable manual mode")
initCmd.Flags().BoolVar(&debug, "debug", false, "enable/disable debug mode")
// Deprecated flags, should be kept to avoid breaking CLI integration.
initCmd.Flags().BoolVarP(&manual, "manual", "m", false, "enable/disable manual mode")
// Hide flags that are deprecated or for internal use only.
_ = initCmd.Flags().MarkHidden("manual")
_ = initCmd.MarkPersistentFlagRequired("config")
RootCmd.AddCommand(initCmd)
return initCmd
}
func runInit(shellName string) {
@ -68,7 +81,6 @@ func runInit(shellName string) {
Shell: shellName,
Config: configFlag,
Strict: strict,
Manual: manual,
Debug: debug,
},
}

View file

@ -30,74 +30,80 @@ var (
)
// printCmd represents the prompt command
var printCmd = &cobra.Command{
Use: "print [debug|primary|secondary|transient|right|tooltip|valid|error]",
Short: "Print the prompt/context",
Long: "Print one of the prompts based on the location/use-case.",
ValidArgs: []string{
"debug",
"primary",
"secondary",
"transient",
"right",
"tooltip",
"valid",
"error",
},
Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
flags := &runtime.Flags{
Config: configFlag,
PWD: pwd,
PSWD: pswd,
ErrorCode: status,
PipeStatus: pipestatus,
ExecutionTime: timing,
StackCount: stackCount,
TerminalWidth: terminalWidth,
Eval: eval,
Shell: shellName,
ShellVersion: shellVersion,
Plain: plain,
Primary: args[0] == "primary",
Cleared: cleared,
NoExitCode: noStatus,
Column: column,
JobCount: jobCount,
}
eng := prompt.New(flags)
defer eng.Env.Close()
switch args[0] {
case "debug":
fmt.Print(eng.ExtraPrompt(prompt.Debug))
case "primary":
fmt.Print(eng.Primary())
case "secondary":
fmt.Print(eng.ExtraPrompt(prompt.Secondary))
case "transient":
fmt.Print(eng.ExtraPrompt(prompt.Transient))
case "right":
fmt.Print(eng.RPrompt())
case "tooltip":
fmt.Print(eng.Tooltip(command))
case "valid":
fmt.Print(eng.ExtraPrompt(prompt.Valid))
case "error":
fmt.Print(eng.ExtraPrompt(prompt.Error))
default:
_ = cmd.Help()
}
},
}
var printCmd = createPrintCmd()
func init() {
RootCmd.AddCommand(printCmd)
}
func createPrintCmd() *cobra.Command {
printCmd := &cobra.Command{
Use: "print [debug|primary|secondary|transient|right|tooltip|valid|error]",
Short: "Print the prompt/context",
Long: "Print one of the prompts based on the location/use-case.",
ValidArgs: []string{
"debug",
"primary",
"secondary",
"transient",
"right",
"tooltip",
"valid",
"error",
},
Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
flags := &runtime.Flags{
Config: configFlag,
PWD: pwd,
PSWD: pswd,
ErrorCode: status,
PipeStatus: pipestatus,
ExecutionTime: timing,
StackCount: stackCount,
TerminalWidth: terminalWidth,
Eval: eval,
Shell: shellName,
ShellVersion: shellVersion,
Plain: plain,
Primary: args[0] == "primary",
Cleared: cleared,
NoExitCode: noStatus,
Column: column,
JobCount: jobCount,
}
eng := prompt.New(flags)
defer eng.Env.Close()
switch args[0] {
case "debug":
fmt.Print(eng.ExtraPrompt(prompt.Debug))
case "primary":
fmt.Print(eng.Primary())
case "secondary":
fmt.Print(eng.ExtraPrompt(prompt.Secondary))
case "transient":
fmt.Print(eng.ExtraPrompt(prompt.Transient))
case "right":
fmt.Print(eng.RPrompt())
case "tooltip":
fmt.Print(eng.Tooltip(command))
case "valid":
fmt.Print(eng.ExtraPrompt(prompt.Valid))
case "error":
fmt.Print(eng.ExtraPrompt(prompt.Error))
default:
_ = cmd.Help()
}
},
}
printCmd.Flags().StringVar(&pwd, "pwd", "", "current working directory")
printCmd.Flags().StringVar(&pswd, "pswd", "", "current working directory (according to pwsh)")
printCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for")
@ -115,10 +121,15 @@ func init() {
printCmd.Flags().IntVar(&column, "column", 0, "the column position of the cursor")
printCmd.Flags().IntVar(&jobCount, "job-count", 0, "number of background jobs")
// Deprecated flags, keep to not break CLI integration
// Deprecated flags, should be kept to avoid breaking CLI integration.
printCmd.Flags().IntVarP(&status, "error", "e", 0, "last exit code")
printCmd.Flags().BoolVar(&noStatus, "no-exit-code", false, "no valid exit code (cancelled or no command yet)")
printCmd.Flags().BoolVar(&cached, "cached", false, "use a cached prompt")
RootCmd.AddCommand(printCmd)
// Hide flags that are deprecated or for internal use only.
_ = printCmd.Flags().MarkHidden("error")
_ = printCmd.Flags().MarkHidden("no-exit-code")
_ = printCmd.Flags().MarkHidden("cached")
return printCmd
}

View file

@ -6,10 +6,11 @@ import (
// promptCmd represents the prompt command
var promptCmd = &cobra.Command{
Use: "prompt",
Short: "Set up the prompt for your shell (deprecated)",
Long: `Set up the prompt for your shell. (deprecated)`,
Args: cobra.NoArgs,
Use: "prompt",
Short: "Set up the prompt for your shell (deprecated)",
Long: `Set up the prompt for your shell. (deprecated)`,
Hidden: true,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
_ = cmd.Help()
},
@ -17,8 +18,8 @@ var promptCmd = &cobra.Command{
func init() {
// legacy support
promptCmd.AddCommand(initCmd)
promptCmd.AddCommand(debugCmd)
promptCmd.AddCommand(printCmd)
promptCmd.AddCommand(createInitCmd())
promptCmd.AddCommand(createDebugCmd())
promptCmd.AddCommand(createPrintCmd())
RootCmd.AddCommand(promptCmd)
}

View file

@ -49,7 +49,13 @@ var (
func init() {
RootCmd.PersistentFlags().StringVarP(&configFlag, "config", "c", "", "config file path")
RootCmd.Flags().BoolVarP(&initialize, "init", "i", false, "init (deprecated)")
RootCmd.Flags().BoolVar(&displayVersion, "version", false, "version")
RootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell (deprecated)")
// Deprecated flags, should be kept to avoid breaking CLI integration.
RootCmd.Flags().BoolVarP(&initialize, "init", "i", false, "init")
RootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell")
// Hide flags that are deprecated or for internal use only.
_ = RootCmd.Flags().MarkHidden("init")
_ = RootCmd.Flags().MarkHidden("shell")
}

View file

@ -49,10 +49,6 @@ func Init(env runtime.Environment, feats Features) string {
additionalParams += " --strict"
}
if env.Flags().Manual {
additionalParams += " --manual"
}
var command, config string
switch shell {