+ // Early and manual processing of --config option in order to set XDS_xxx
+ // variables before parsing of option by app cli
+ // 1/ from command line option: "--config myConfig.json"
+ // 2/ from environment variable XDS_CONFIG
+ // 3/ $HOME/.xds/cli/cli-config.env file
+ // 4/ /etc/xds/cli/cli-config.env file
+ searchIn := make([]string, 0, 4)
+ for idx, a := range os.Args[1:] {
+ if a == "-c" || a == "--config" || a == "-config" {
+ searchIn = append(searchIn, os.Args[idx+2])
+ break
+ }
+ }
+ searchIn = append(searchIn, os.Getenv("XDS_CONFIG"))
+ if usrHome := common.GetUserHome(); usrHome != "" {
+ searchIn = append(searchIn, path.Join(usrHome, ".xds", "cli", defaultConfigEnvFilename))
+ }
+ searchIn = append(searchIn, path.Join("/etc", "xds", "cli", defaultConfigEnvFilename))
+
+ // Use the first existing env config file
+ confFile := ""
+ for _, p := range searchIn {
+ if pr, err := common.ResolveEnvVar(p); err == nil {
+ earlyPrintf("Check if confFile exists : %v", pr)
+ if common.Exists(pr) {
+ confFile = pr
+ break
+ }
+ }
+ }
+
+ // Load config file if requested
+ if confFile != "" {
+ earlyPrintf("Used confFile: %v", confFile)
+ // Load config file variables that will overwrite env variables
+ err := godotenv.Overload(confFile)
+ if err != nil {
+ exitError(1, "Error loading env config file "+confFile)
+ }
+
+ // Keep confFile settings in a map
+ EnvConfFileMap, err = godotenv.Read(confFile)
+ if err != nil {
+ exitError(1, "Error reading env config file "+confFile)
+ }
+ earlyPrintf("EnvConfFileMap: %v", EnvConfFileMap)
+ }
+