diff --git a/cli/config.yaml.example b/cli/config.yaml.example index e6c605095ac..a709e080ed3 100644 --- a/cli/config.yaml.example +++ b/cli/config.yaml.example @@ -1,7 +1,7 @@ # You can put this configuration file in the following locations: # - $HOME/.ente/config.yaml # - config.yaml in the current working directory -# - $ENTE_CLI_CONFIG_PATH/config.yaml +# - $ENTE_CLI_CONFIG_DIR/config.yaml endpoint: api: "http://localhost:8080" diff --git a/cli/docs/selfhost.md b/cli/docs/selfhost.md index d38f0325a58..b4e7a33d88a 100644 --- a/cli/docs/selfhost.md +++ b/cli/docs/selfhost.md @@ -1,8 +1,10 @@ + + ## Self Hosting If you are self-hosting the server, you can still configure CLI to export data & perform basic admin actions. To do this, first configure the CLI to point to your server. -Define a config.yaml and put it either in the same directory as CLI binary or path defined in env variable `ENTE_CLI_CONFIG_PATH` +Define a config.yaml and put it either in the same directory as CLI binary or path defined in env variable `ENTE_CLI_CONFIG_DIR` ```yaml endpoint: diff --git a/cli/main.go b/cli/main.go index 0ea9b7c8339..7b0312485e2 100644 --- a/cli/main.go +++ b/cli/main.go @@ -18,29 +18,29 @@ import ( var AppVersion = "0.2.2" func main() { - cliDBPath, err := GetCLIConfigPath() + cliConfigDir, err := GetCLIConfigDir() if secrets.IsRunningInContainer() { - cliDBPath = constants.CliDataPath - _, err := internal.ValidateDirForWrite(cliDBPath) + cliConfigDir = constants.CliDataPath + _, err := internal.ValidateDirForWrite(cliConfigDir) if err != nil { - log.Fatalf("Please mount a volume to %s\n%v\n", cliDBPath, err) + log.Fatalf("Please mount a volume to %s\n%v\n", cliConfigDir, err) } } if err != nil { log.Fatalf("Could not create cli config path\n%v\n", err) } - initConfig(cliDBPath) - newCliPath := fmt.Sprintf("%s/ente-cli.db", cliDBPath) - if !strings.HasPrefix(cliDBPath, "/") { - oldCliPath := fmt.Sprintf("%sente-cli.db", cliDBPath) + initConfig(cliConfigDir) + newCliDBPath := filepath.Join(cliConfigDir, "ente-cli.db") + if !strings.HasPrefix(cliConfigDir, "/") { + oldCliPath := fmt.Sprintf("%sente-cli.db", cliConfigDir) if _, err := os.Stat(oldCliPath); err == nil { - log.Printf("migrating old cli db from %s to %s\n", oldCliPath, newCliPath) - if err := os.Rename(oldCliPath, newCliPath); err != nil { + log.Printf("migrating old cli db from %s to %s\n", oldCliPath, newCliDBPath) + if err := os.Rename(oldCliPath, newCliDBPath); err != nil { log.Fatalf("Could not rename old cli db\n%v\n", err) } } } - db, err := pkg.GetDB(newCliPath) + db, err := pkg.GetDB(newCliDBPath) if err != nil { if strings.Contains(err.Error(), "timeout") { @@ -85,11 +85,11 @@ func main() { cmd.Execute(&ctrl, AppVersion) } -func initConfig(cliConfigPath string) { - viper.SetConfigName("config") // name of config file (without extension) - viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name - viper.AddConfigPath(cliConfigPath + "/") // path to look for the config file in - viper.AddConfigPath(".") // optionally look for config in the working directory +func initConfig(cliConfigDir string) { + viper.SetConfigName("config") // name of config file (without extension) + viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name + viper.AddConfigPath(cliConfigDir + "/") // path to look for the config file in + viper.AddConfigPath(".") // optionally look for config in the working directory viper.SetDefault("endpoint.api", constants.EnteApiUrl) viper.SetDefault("endpoint.accounts", constants.EnteAccountUrl) @@ -102,11 +102,19 @@ func initConfig(cliConfigPath string) { } } -// GetCLIConfigPath returns the path to the .ente-cli folder and creates it if it doesn't exist. -func GetCLIConfigPath() (string, error) { - if os.Getenv("ENTE_CLI_CONFIG_PATH") != "" { - return os.Getenv("ENTE_CLI_CONFIG_PATH"), nil +// GetCLIConfigDir returns the path to the .ente-cli folder and creates it if it doesn't exist. +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 != "" { + // remove trailing slash (for all OS) + configDir = strings.TrimSuffix(configDir, string(filepath.Separator)) + return configDir, nil + } + // Get the user's home directory homeDir, err := os.UserHomeDir() if err != nil {