From 33b56a22575bcdc07ae29a7481caf128002fa928 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Thu, 13 Feb 2025 11:11:21 +0530 Subject: [PATCH] [cli] Skip cli init for version,docs & help --- cli/main.go | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/cli/main.go b/cli/main.go index 990d751b01..44ea65aef8 100644 --- a/cli/main.go +++ b/cli/main.go @@ -50,18 +50,21 @@ func main() { } } - // Define a set of commands that do not require KeyHolder initialisation. - skipKeyHolderCommands := map[string]struct{}{"version": {}, "docs": {}, "help": {}} + // Define a set of commands that do not require KeyHolder or cli initialisation. + skipInitCommands := map[string]struct{}{"version": {}, "docs": {}, "help": {}} var keyHolder *secrets.KeyHolder - // Only initialise KeyHolder if the command isn't in the skip list. + shouldInit := len(os.Args) > 1 if len(os.Args) > 1 { - if _, skip := skipKeyHolderCommands[os.Args[1]]; !skip { - keyHolder = secrets.NewKeyHolder(secrets.GetOrCreateClISecret()) + if _, skip := skipInitCommands[os.Args[1]]; skip { + shouldInit = false } } + if shouldInit { + keyHolder = secrets.NewKeyHolder(secrets.GetOrCreateClISecret()) + } ctrl := pkg.ClICtrl{ Client: api.NewClient(api.Params{ Debug: viper.GetBool("log.http"), @@ -71,16 +74,10 @@ func main() { KeyHolder: keyHolder, } - err = ctrl.Init() - if err != nil { - panic(err) + if len(os.Args) == 1 { + // If no arguments are passed, show help + os.Args = append(os.Args, "help") } - defer func() { - if err := db.Close(); err != nil { - panic(err) - } - }() - if len(os.Args) == 2 && os.Args[1] == "docs" { log.Println("Generating docs") err = cmd.GenerateDocs() @@ -89,9 +86,16 @@ func main() { } return } - if len(os.Args) == 1 { - // If no arguments are passed, show help - os.Args = append(os.Args, "help") + if shouldInit { + err = ctrl.Init() + if err != nil { + panic(err) + } + defer func() { + if err := db.Close(); err != nil { + panic(err) + } + }() } if os.Args[1] == "version" && viper.GetString("endpoint.api") != constants.EnteApiUrl { log.Printf("Custom endpoint: %s\n", viper.GetString("endpoint.api")) @@ -120,10 +124,10 @@ func initConfig(cliConfigDir string) { func GetCLIConfigDir() (string, error) { var configDir = os.Getenv("ENTE_CLI_CONFIG_DIR") - if configDir == "" { - // for backward compatibility, check for ENTE_CLI_CONFIG_PATH - configDir = os.Getenv("ENTE_CLI_CONFIG_PATH") - } + if configDir == "" { + // for backward compatibility, check for ENTE_CLI_CONFIG_PATH + configDir = os.Getenv("ENTE_CLI_CONFIG_PATH") + } if configDir != "" { // remove trailing slash (for all OS)