feat(init): improve performance

This commit is contained in:
Jan De Dobbeleer 2022-11-02 11:48:19 +01:00 committed by Jan De Dobbeleer
parent 05fed0dbab
commit b73ac4bdc2
13 changed files with 37 additions and 22 deletions

View file

@ -57,7 +57,7 @@ You can do the following:
}
func init() { //nolint:gochecknoinits
rootCmd.AddCommand(getCache)
RootCmd.AddCommand(getCache)
}
func editFileWithEditor(file string) {

View file

@ -40,5 +40,5 @@ You can export, migrate or edit the config.`,
}
func init() { //nolint:gochecknoinits
rootCmd.AddCommand(configCmd)
RootCmd.AddCommand(configCmd)
}

View file

@ -56,5 +56,5 @@ var debugCmd = &cobra.Command{
}
func init() { //nolint:gochecknoinits
rootCmd.AddCommand(debugCmd)
RootCmd.AddCommand(debugCmd)
}

View file

@ -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)
}

View file

@ -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")
}

View file

@ -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) {

View file

@ -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)
}

View file

@ -20,5 +20,5 @@ func init() { //nolint:gochecknoinits
promptCmd.AddCommand(initCmd)
promptCmd.AddCommand(debugCmd)
promptCmd.AddCommand(printCmd)
rootCmd.AddCommand(promptCmd)
RootCmd.AddCommand(promptCmd)
}

View file

@ -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)")
}

View file

@ -18,5 +18,5 @@ var versionCmd = &cobra.Command{
}
func init() { //nolint:gochecknoinits
rootCmd.AddCommand(versionCmd)
RootCmd.AddCommand(versionCmd)
}

View file

@ -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)

View file

@ -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)
}

18
src/main_test.go Normal file
View file

@ -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()
}
}