From 466f9e72b8df22342c90b7e474c5f099c13f7b58 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 6 Nov 2024 06:32:59 +0100 Subject: [PATCH] fix(cli): revert breaking CLI change --- src/cli/init.go | 78 ++++++++++++++++++++++++++----------------------- src/cli/root.go | 12 ++++++++ 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/cli/init.go b/src/cli/init.go index 96410ddb..ef8763b9 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -51,43 +51,7 @@ See the documentation to initialize your shell: https://ohmyposh.dev/docs/instal return } - var startTime time.Time - if debug { - startTime = time.Now() - } - - env := &runtime.Terminal{ - CmdFlags: &runtime.Flags{ - Shell: args[0], - Config: configFlag, - Strict: strict, - Debug: debug, - }, - } - - env.Init() - defer env.Close() - - template.Init(env) - - cfg := config.Load(env) - - feats := cfg.Features() - - var output string - - switch { - case printOutput, debug: - output = shell.PrintInit(env, feats, &startTime) - default: - output = shell.Init(env, feats) - } - - if silent { - return - } - - fmt.Print(output) + runInit(args[0]) }, } @@ -99,3 +63,43 @@ See the documentation to initialize your shell: https://ohmyposh.dev/docs/instal return initCmd } + +func runInit(sh string) { + var startTime time.Time + if debug { + startTime = time.Now() + } + + env := &runtime.Terminal{ + CmdFlags: &runtime.Flags{ + Shell: sh, + Config: configFlag, + Strict: strict, + Debug: debug, + }, + } + + env.Init() + defer env.Close() + + template.Init(env) + + cfg := config.Load(env) + + feats := cfg.Features() + + var output string + + switch { + case printOutput, debug: + output = shell.PrintInit(env, feats, &startTime) + default: + output = shell.Init(env, feats) + } + + if silent { + return + } + + fmt.Print(output) +} diff --git a/src/cli/root.go b/src/cli/root.go index 90650df4..176d1598 100644 --- a/src/cli/root.go +++ b/src/cli/root.go @@ -2,6 +2,7 @@ package cli import ( "os" + "strings" "github.com/spf13/cobra" ) @@ -10,6 +11,8 @@ var ( configFlag string shellName string silent bool + // Deprecated flags, should be kept to avoid breaking CLI integration. + initialize bool ) var RootCmd = &cobra.Command{ @@ -20,6 +23,11 @@ It can use the same configuration everywhere to offer a consistent experience, regardless of where you are. For a detailed guide on getting started, have a look at the docs at https://ohmyposh.dev`, Run: func(cmd *cobra.Command, _ []string) { + if initialize { + runInit(strings.ToLower(shellName)) + return + } + _ = cmd.Help() }, } @@ -34,6 +42,10 @@ func Execute() { func init() { RootCmd.PersistentFlags().StringVarP(&configFlag, "config", "c", "", "config file path") + // 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.PersistentFlags().BoolVar(&silent, "silent", false, "do not print anything") _ = RootCmd.Flags().MarkHidden("silent")