[cli] Skip cli init for version,docs & help

This commit is contained in:
Neeraj Gupta
2025-02-13 11:11:21 +05:30
parent 9abdfd2555
commit 33b56a2257

View File

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