@@ -38,11 +38,29 @@ func (cfg *Config) IsLocalMode() bool {
3838 return cfg .Mode == constants .MODE_LOCAL
3939}
4040
41+ // getHomeDir returns the user's home directory with fallback for systemd
42+ // systemd services don't set HOME environment variable by default
43+ func getHomeDir () string {
44+ home := os .Getenv ("HOME" )
45+ if home == "" {
46+ // Fallback for systemd services running as root
47+ if os .Geteuid () == 0 {
48+ home = "/root"
49+ } else {
50+ // Try os.UserHomeDir() for non-root users
51+ if h , err := os .UserHomeDir (); err == nil {
52+ home = h
53+ }
54+ }
55+ }
56+ return home
57+ }
58+
4159// LoadConfig loads configuration from file and environment
4260func LoadConfig () (* Config , error ) {
4361 viper .SetConfigName ("config" )
4462 viper .SetConfigType ("yaml" )
45- viper .AddConfigPath (os . Getenv ( "HOME" ) + constants .CONFIG_DIR_NAME )
63+ viper .AddConfigPath (getHomeDir ( ) + constants .CONFIG_DIR_NAME )
4664 viper .AddConfigPath ("." )
4765
4866 // Set defaults for monitoring configuration
@@ -64,7 +82,7 @@ func LoadConfig() (*Config, error) {
6482
6583// SaveConfig saves configuration to file
6684func SaveConfig (cfg * Config ) error {
67- configDir := os . Getenv ( "HOME" ) + "/.catops"
85+ configDir := getHomeDir ( ) + "/.catops"
6886 if err := os .MkdirAll (configDir , 0755 ); err != nil {
6987 return err
7088 }
0 commit comments