[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:
# - $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"

View File

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

View File

@ -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,10 +85,10 @@ func main() {
cmd.Execute(&ctrl, AppVersion)
}
func initConfig(cliConfigPath string) {
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(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.SetDefault("endpoint.api", constants.EnteApiUrl)
@ -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 {