From: Sebastien Douheret Date: Fri, 4 Jan 2019 14:51:29 +0000 (+0100) Subject: Add cli-config.env file support X-Git-Tag: 7.90.0^0 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-cli.git;a=commitdiff_plain;h=03e64dc8ad5634c56b5fd7f61b9336fd33e7c3cb Add cli-config.env file support XDS environment configuration can be set using various methods. Here is the list of various methods, ordered by priority: 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 Change-Id: I1b59c05e583336fe05459de91a35d47cf5ddf670 Signed-off-by: Sebastien Douheret --- diff --git a/.vscode/launch.json b/.vscode/launch.json index b792099..96d60b3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,6 @@ "env": { "GOPATH": "${workspaceRoot}/../../../../../..:${env:GOPATH}", "XDS_APPNAME": "xds-cli", - "XDS_AGENT_URL": "localhost:8800", "XDS_LOGLEVEL": "debug" }, "args": ["misc", "version"], @@ -26,7 +25,6 @@ "env": { "GOPATH": "${workspaceRoot}/../../../../../..:${env:GOPATH}", "XDS_APPNAME": "xds-cli", - "XDS_AGENT_URL": "localhost:8800", "XDS_LOGLEVEL": "debug" }, "args": ["sdks", "list"], diff --git a/main.go b/main.go index 9ac751c..c28e1be 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2018 "IoT.bzh" + * Copyright (C) 2017-2019 "IoT.bzh" * Author Sebastien Douheret * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,6 +23,7 @@ package main import ( "fmt" "os" + "path" "regexp" "sort" "strings" @@ -57,8 +58,9 @@ var AppSubVersion = "unknown-dev" // Application details const ( - appCopyright = "Copyright (C) 2017-2018 IoT.bzh - Apache-2.0" - defaultLogLevel = "error" + appCopyright = "Copyright (C) 2017-2019 IoT.bzh - Apache-2.0" + defaultLogLevel = "error" + defaultConfigEnvFilename = "cli-config.env" ) // Log Global variable that hold logger @@ -240,27 +242,40 @@ func main() { // Early and manual processing of --config option in order to set XDS_xxx // variables before parsing of option by app cli - confFile := os.Getenv("XDS_CONFIG") + // 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" { - confFile = os.Args[idx+2] + 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("confFile detected: %v", confFile) - confFile, err := common.ResolveEnvVar(confFile) - if err != nil { - exitError(1, "Error while resolving confFile: %v", err) - } - earlyPrintf("Resolved confFile: %v", confFile) - if !common.Exists(confFile) { - exitError(1, "Error env config file not found") - } + earlyPrintf("Used confFile: %v", confFile) // Load config file variables that will overwrite env variables - err = godotenv.Overload(confFile) + err := godotenv.Overload(confFile) if err != nil { exitError(1, "Error loading env config file "+confFile) }