initConfig reads in config file and ENV variables if set
()
| 106 | |
| 107 | // initConfig reads in config file and ENV variables if set |
| 108 | func initConfig() { |
| 109 | if cfgFile != "" { |
| 110 | // Use config file from the flag |
| 111 | viper.SetConfigFile(cfgFile) |
| 112 | } else { |
| 113 | // Walk up from cwd, adding each ancestor as a config search path. |
| 114 | // Nearest directory is added first (highest priority in viper). |
| 115 | // Stop at .git boundary or filesystem root. |
| 116 | dir, err := filepath.Abs(".") |
| 117 | if err == nil { |
| 118 | for { |
| 119 | viper.AddConfigPath(dir) |
| 120 | // Stop if we hit a .git directory (project boundary) |
| 121 | if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil { |
| 122 | break |
| 123 | } |
| 124 | parent := filepath.Dir(dir) |
| 125 | if parent == dir { |
| 126 | break |
| 127 | } |
| 128 | dir = parent |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Add home directory as final fallback |
| 133 | home, err := os.UserHomeDir() |
| 134 | if err == nil { |
| 135 | viper.AddConfigPath(home) |
| 136 | } |
| 137 | |
| 138 | viper.SetConfigType("yaml") |
| 139 | viper.SetConfigName(".taskmd") |
| 140 | } |
| 141 | |
| 142 | // Read in environment variables that match |
| 143 | viper.SetEnvPrefix("TASKMD") |
| 144 | viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) |
| 145 | viper.AutomaticEnv() |
| 146 | |
| 147 | // If a config file is found, read it in |
| 148 | if err := viper.ReadInConfig(); err == nil && verbose { |
| 149 | fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // GetGlobalFlags returns a struct with all global flag values |
| 154 | func GetGlobalFlags() GlobalFlags { |
no outgoing calls