[cli] Default to ENTE_CLI_CONFIG_DIR, fallback to ENTE_CLI_CONFIG_PATH for compatibility

This commit is contained in:
Neeraj Gupta 2024-11-06 19:09:26 +05:30
parent fb710ac41b
commit 8ff6ab6c04
3 changed files with 32 additions and 22 deletions

View File

@ -1,7 +1,7 @@
# You can put this configuration file in the following locations: # You can put this configuration file in the following locations:
# - $HOME/.ente/config.yaml # - $HOME/.ente/config.yaml
# - config.yaml in the current working directory # - config.yaml in the current working directory
# - $ENTE_CLI_CONFIG_PATH/config.yaml # - $ENTE_CLI_CONFIG_DIR/config.yaml
endpoint: endpoint:
api: "http://localhost:8080" api: "http://localhost:8080"

View File

@ -1,8 +1,10 @@
## Self Hosting ## Self Hosting
If you are self-hosting the server, you can still configure CLI to export data & perform basic admin actions. 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. 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 ```yaml
endpoint: endpoint:

View File

@ -18,29 +18,29 @@ import (
var AppVersion = "0.2.2" var AppVersion = "0.2.2"
func main() { func main() {
cliDBPath, err := GetCLIConfigPath() cliConfigDir, err := GetCLIConfigDir()
if secrets.IsRunningInContainer() { if secrets.IsRunningInContainer() {
cliDBPath = constants.CliDataPath cliConfigDir = constants.CliDataPath
_, err := internal.ValidateDirForWrite(cliDBPath) _, err := internal.ValidateDirForWrite(cliConfigDir)
if err != nil { 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 { if err != nil {
log.Fatalf("Could not create cli config path\n%v\n", err) log.Fatalf("Could not create cli config path\n%v\n", err)
} }
initConfig(cliDBPath) initConfig(cliConfigDir)
newCliPath := fmt.Sprintf("%s/ente-cli.db", cliDBPath) newCliDBPath := filepath.Join(cliConfigDir, "ente-cli.db")
if !strings.HasPrefix(cliDBPath, "/") { if !strings.HasPrefix(cliConfigDir, "/") {
oldCliPath := fmt.Sprintf("%sente-cli.db", cliDBPath) oldCliPath := fmt.Sprintf("%sente-cli.db", cliConfigDir)
if _, err := os.Stat(oldCliPath); err == nil { if _, err := os.Stat(oldCliPath); err == nil {
log.Printf("migrating old cli db from %s to %s\n", oldCliPath, newCliPath) log.Printf("migrating old cli db from %s to %s\n", oldCliPath, newCliDBPath)
if err := os.Rename(oldCliPath, newCliPath); err != nil { if err := os.Rename(oldCliPath, newCliDBPath); err != nil {
log.Fatalf("Could not rename old cli db\n%v\n", err) 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 err != nil {
if strings.Contains(err.Error(), "timeout") { if strings.Contains(err.Error(), "timeout") {
@ -85,11 +85,11 @@ func main() {
cmd.Execute(&ctrl, AppVersion) cmd.Execute(&ctrl, AppVersion)
} }
func initConfig(cliConfigPath string) { func initConfig(cliConfigDir string) {
viper.SetConfigName("config") // name of config file (without extension) 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.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(cliConfigDir + "/") // path to look for the config file in
viper.AddConfigPath(".") // optionally look for config in the working directory viper.AddConfigPath(".") // optionally look for config in the working directory
viper.SetDefault("endpoint.api", constants.EnteApiUrl) viper.SetDefault("endpoint.api", constants.EnteApiUrl)
viper.SetDefault("endpoint.accounts", constants.EnteAccountUrl) 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. // GetCLIConfigDir returns the path to the .ente-cli folder and creates it if it doesn't exist.
func GetCLIConfigPath() (string, error) { func GetCLIConfigDir() (string, error) {
if os.Getenv("ENTE_CLI_CONFIG_PATH") != "" { var configDir = os.Getenv("ENTE_CLI_CONFIG_DIR")
return os.Getenv("ENTE_CLI_CONFIG_PATH"), nil 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 // Get the user's home directory
homeDir, err := os.UserHomeDir() homeDir, err := os.UserHomeDir()
if err != nil { if err != nil {