From b73ac4bdc27e042eef175006d6fce5e5bb9d644e Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 2 Nov 2022 11:48:19 +0100 Subject: [PATCH] feat(init): improve performance --- src/cli/cache.go | 2 +- src/cli/config.go | 2 +- src/cli/debug.go | 2 +- src/cli/font.go | 2 +- src/cli/get.go | 2 +- src/cli/init.go | 2 +- src/cli/print.go | 2 +- src/cli/prompt.go | 2 +- src/cli/root.go | 12 ++++++------ src/cli/version.go | 2 +- src/engine/config.go | 7 +++---- src/environment/shell.go | 4 +--- src/main_test.go | 18 ++++++++++++++++++ 13 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 src/main_test.go diff --git a/src/cli/cache.go b/src/cli/cache.go index 66851720..8c32f0cd 100644 --- a/src/cli/cache.go +++ b/src/cli/cache.go @@ -57,7 +57,7 @@ You can do the following: } func init() { //nolint:gochecknoinits - rootCmd.AddCommand(getCache) + RootCmd.AddCommand(getCache) } func editFileWithEditor(file string) { diff --git a/src/cli/config.go b/src/cli/config.go index 3cfe386c..560976ee 100644 --- a/src/cli/config.go +++ b/src/cli/config.go @@ -40,5 +40,5 @@ You can export, migrate or edit the config.`, } func init() { //nolint:gochecknoinits - rootCmd.AddCommand(configCmd) + RootCmd.AddCommand(configCmd) } diff --git a/src/cli/debug.go b/src/cli/debug.go index 36dcdc6f..e9605f8e 100644 --- a/src/cli/debug.go +++ b/src/cli/debug.go @@ -56,5 +56,5 @@ var debugCmd = &cobra.Command{ } func init() { //nolint:gochecknoinits - rootCmd.AddCommand(debugCmd) + RootCmd.AddCommand(debugCmd) } diff --git a/src/cli/font.go b/src/cli/font.go index b1c0c4be..29ec7952 100644 --- a/src/cli/font.go +++ b/src/cli/font.go @@ -48,5 +48,5 @@ This command is used to install fonts and configure the font in your terminal. ) func init() { //nolint:gochecknoinits - rootCmd.AddCommand(fontCmd) + RootCmd.AddCommand(fontCmd) } diff --git a/src/cli/get.go b/src/cli/get.go index 0efbb1af..c8777372 100644 --- a/src/cli/get.go +++ b/src/cli/get.go @@ -60,6 +60,6 @@ This command is used to get the value of the following variables: } func init() { //nolint:gochecknoinits - rootCmd.AddCommand(getCmd) + RootCmd.AddCommand(getCmd) getCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for") } diff --git a/src/cli/init.go b/src/cli/init.go index 0f36c1d3..55a1d692 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -45,7 +45,7 @@ func init() { //nolint:gochecknoinits initCmd.Flags().BoolVarP(&strict, "strict", "s", false, "run in strict mode") initCmd.Flags().BoolVarP(&manual, "manual", "m", false, "enable/disable manual mode") _ = initCmd.MarkPersistentFlagRequired("config") - rootCmd.AddCommand(initCmd) + RootCmd.AddCommand(initCmd) } func runInit(shellName string) { diff --git a/src/cli/print.go b/src/cli/print.go index 866b0286..24be3d62 100644 --- a/src/cli/print.go +++ b/src/cli/print.go @@ -128,5 +128,5 @@ func init() { //nolint:gochecknoinits printCmd.Flags().StringVar(&command, "command", "", "tooltip command") printCmd.Flags().BoolVarP(&plain, "plain", "p", false, "plain text output (no ANSI)") printCmd.Flags().BoolVar(&eval, "eval", false, "output the prompt for eval") - rootCmd.AddCommand(printCmd) + RootCmd.AddCommand(printCmd) } diff --git a/src/cli/prompt.go b/src/cli/prompt.go index 12d3e712..31d5cd90 100644 --- a/src/cli/prompt.go +++ b/src/cli/prompt.go @@ -20,5 +20,5 @@ func init() { //nolint:gochecknoinits promptCmd.AddCommand(initCmd) promptCmd.AddCommand(debugCmd) promptCmd.AddCommand(printCmd) - rootCmd.AddCommand(promptCmd) + RootCmd.AddCommand(promptCmd) } diff --git a/src/cli/root.go b/src/cli/root.go index 90b487be..a2ae81bc 100644 --- a/src/cli/root.go +++ b/src/cli/root.go @@ -15,7 +15,7 @@ var ( cliVersion string ) -var rootCmd = &cobra.Command{ +var RootCmd = &cobra.Command{ Use: "oh-my-posh", Short: "oh-my-posh is a tool to render your prompt", Long: `oh-my-posh is a cross platform tool to render your prompt. @@ -37,7 +37,7 @@ on getting started, have a look at the docs at https://ohmyposh.dev`, func Execute(version string) { cliVersion = version - if err := rootCmd.Execute(); err != nil { + if err := RootCmd.Execute(); err != nil { os.Exit(1) } } @@ -49,8 +49,8 @@ var ( ) func init() { //nolint:gochecknoinits - rootCmd.PersistentFlags().StringVarP(&config, "config", "c", "", "config (required)") - rootCmd.Flags().BoolVarP(&initialize, "init", "i", false, "init (deprecated)") - rootCmd.Flags().BoolVar(&displayVersion, "version", false, "version") - rootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell (deprecated)") + RootCmd.PersistentFlags().StringVarP(&config, "config", "c", "", "config (required)") + RootCmd.Flags().BoolVarP(&initialize, "init", "i", false, "init (deprecated)") + RootCmd.Flags().BoolVar(&displayVersion, "version", false, "version") + RootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell (deprecated)") } diff --git a/src/cli/version.go b/src/cli/version.go index 0a5c9542..15bda543 100644 --- a/src/cli/version.go +++ b/src/cli/version.go @@ -18,5 +18,5 @@ var versionCmd = &cobra.Command{ } func init() { //nolint:gochecknoinits - rootCmd.AddCommand(versionCmd) + RootCmd.AddCommand(versionCmd) } diff --git a/src/engine/config.go b/src/engine/config.go index df12dcda..29e6248a 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -119,9 +119,6 @@ func loadConfig(env environment.Environment) *Config { defer env.Trace(time.Now(), "config.loadConfig") var cfg Config configFile := env.Flags().Config - if _, err := os.Stat(configFile); err != nil { - return defaultConfig() - } cfg.origin = configFile cfg.format = strings.TrimPrefix(filepath.Ext(configFile), ".") @@ -139,7 +136,9 @@ func loadConfig(env environment.Environment) *Config { }) err := config.LoadFiles(configFile) - cfg.exitWithError(err) + if err != nil { + return defaultConfig() + } err = config.BindStruct("", &cfg) cfg.exitWithError(err) diff --git a/src/environment/shell.go b/src/environment/shell.go index a51831e1..bb955d58 100644 --- a/src/environment/shell.go +++ b/src/environment/shell.go @@ -274,9 +274,7 @@ func (env *ShellEnvironment) resolveConfigPath() { configFile = filepath.Join(env.Home(), configFile) } if !filepath.IsAbs(configFile) { - if absConfigFile, err := filepath.Abs(configFile); err == nil { - configFile = absConfigFile - } + configFile = filepath.Join(env.Pwd(), configFile) } env.CmdFlags.Config = filepath.Clean(configFile) } diff --git a/src/main_test.go b/src/main_test.go new file mode 100644 index 00000000..41eadcf7 --- /dev/null +++ b/src/main_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "bytes" + "oh-my-posh/cli" + "testing" +) + +func BenchmarkInit(b *testing.B) { + cmd := cli.RootCmd + cmd.SetArgs([]string{"init", "fish", "--print", "--config", "err.omp.json"}) + out := bytes.NewBufferString("") + cmd.SetOut(out) + + for i := 0; i < b.N; i++ { + _ = cmd.Execute() + } +}