Skip to content

Commit

Permalink
[cli] Default to ENTE_CLI_CONFIG_DIR, fallback to ENTE_CLI_CONFIG_PAT… (
Browse files Browse the repository at this point in the history
#3961)

…H for compatibility

## Description

## Tests
  • Loading branch information
ua741 authored Nov 6, 2024
2 parents 8e1b6d3 + 8ff6ab6 commit 82c1987
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cli/config.yaml.example
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 3 additions & 1 deletion cli/docs/selfhost.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
48 changes: 28 additions & 20 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 82c1987

Please sign in to comment.