Skip to content

Commit c8a4e8d

Browse files
committed
refactor: env vars for override of file locations
1 parent 6d4d59f commit c8a4e8d

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

src/internal/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"gopkg.in/yaml.v3"
55
"os"
66
"rclone-manager/internal/constants"
7+
"rclone-manager/internal/environment"
78
)
89

910
type Config struct {
@@ -22,7 +23,8 @@ type Config struct {
2223
}
2324

2425
func LoadConfig() (*Config, error) {
25-
data, err := os.ReadFile(constants.YAMLPath)
26+
yamlPath := environment.GetEnvWithFallback(constants.YAMLPathEnvVar, constants.DefaultYAMLPath)
27+
data, err := os.ReadFile(yamlPath)
2628
if err != nil {
2729
return nil, err
2830
}

src/internal/constants/constants.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const (
2828

2929
// Constants data files
3030
const (
31-
YAMLPath = "/data/config.yaml"
32-
RcloneConf = "/data/rclone.conf"
31+
YAMLPathEnvVar = "RCLONE_MANAGER_CONFIG_YAML"
32+
DefaultYAMLPath = "/data/config.yaml"
33+
RcloneConfEnvVar = "RCLONE_MANAGER_RCLONE_CONF"
34+
DefaultRcloneConf = "/data/rclone.conf"
3335
)

src/internal/environment/environment.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ func PrepareEnvironment(envVars map[string]string) []string {
2525
return mapToSlice(envMap)
2626
}
2727

28+
func GetEnvWithFallback(key, defaultValue string) string {
29+
value := os.Getenv(key)
30+
if value == "" {
31+
return defaultValue
32+
}
33+
return value
34+
}
35+
2836
func splitEnv(env string) []string {
2937
parts := make([]string, 2)
3038
idx := strings.Index(env, "=")

src/internal/rclone_manager/rclone.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/rs/zerolog"
55
"rclone-manager/internal/config"
66
"rclone-manager/internal/constants"
7+
"rclone-manager/internal/environment"
78
"rclone-manager/internal/mount_manager"
89
"rclone-manager/internal/serve_manager"
910
"rclone-manager/internal/watcher"
@@ -41,9 +42,12 @@ func InitializeRClone(logger zerolog.Logger) {
4142
go mount_manager.InitializeMountEndpoints(conf, logger, &processLock)
4243
}
4344

45+
yamlPath := environment.GetEnvWithFallback(constants.YAMLPathEnvVar, constants.DefaultYAMLPath)
46+
rcloneConfPath := environment.GetEnvWithFallback(constants.RcloneConfEnvVar, constants.DefaultRcloneConf)
47+
4448
filesToWatch := []string{
45-
constants.YAMLPath,
46-
constants.RcloneConf,
49+
yamlPath,
50+
rcloneConfPath,
4751
}
4852

4953
watcher.StartNewFileWatcher(filesToWatch, reloadConfig, logger)

0 commit comments

Comments
 (0)