X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=main.go;h=c8effe627e94a7af7c1077c9fb5ca4a40309e26a;hb=f48c7c3164b0568de45d7ccc88b0ce7724a17cc4;hp=197541d3c356d672421025bf5bcd676b575d6e76;hpb=e54535129f23970619042a328ad9a139bba21c5d;p=src%2Fxds%2Fxds-cli.git diff --git a/main.go b/main.go index 197541d..c8effe6 100644 --- a/main.go +++ b/main.go @@ -75,14 +75,28 @@ var IOsk *socketio_client.Client // exitError exists this program with the specified error func exitError(code int, f string, a ...interface{}) { + earlyDisplay() err := fmt.Sprintf(f, a...) fmt.Fprintf(os.Stderr, err+"\n") os.Exit(code) } +// earlyDebug Used to log info before logger has been initialized +var earlyDebug []string + +func earlyPrintf(format string, args ...interface{}) { + earlyDebug = append(earlyDebug, fmt.Sprintf(format, args...)) +} + +func earlyDisplay() { + for _, str := range earlyDebug { + Log.Infof("%s", str) + } + earlyDebug = []string{} +} + // main func main() { - var earlyDebug []string // Allow to set app name from cli (useful for debugging) if AppName == "" { @@ -189,6 +203,19 @@ func main() { initCmdExec(&app.Commands) initCmdMisc(&app.Commands) + // Add --config option to all commands to support --config option either before or after command verb + // IOW support following both syntaxes: + // xds-cli exec --config myprj.conf ... + // xds-cli --config myprj.conf exec ... + for i, cmd := range app.Commands { + if len(cmd.Flags) > 0 { + app.Commands[i].Flags = append(cmd.Flags, cli.StringFlag{Hidden: true, Name: "config, c"}) + } + for j, subCmd := range cmd.Subcommands { + app.Commands[i].Subcommands[j].Flags = append(subCmd.Flags, cli.StringFlag{Hidden: true, Name: "config, c"}) + } + } + sort.Sort(cli.FlagsByName(app.Flags)) sort.Sort(cli.CommandsByName(app.Commands)) @@ -204,7 +231,7 @@ func main() { // Load config file if requested if confFile != "" { - earlyDebug = append(earlyDebug, fmt.Sprintf("confFile detected: %v", confFile)) + earlyPrintf("confFile detected: %v", confFile) if !common.Exists(confFile) { exitError(1, "Error env config file not found") } @@ -219,7 +246,7 @@ func main() { if err != nil { exitError(1, "Error reading env config file "+confFile) } - earlyDebug = append(earlyDebug, fmt.Sprintf("EnvConfFileMap: %v", EnvConfFileMap)) + earlyPrintf("EnvConfFileMap: %v", EnvConfFileMap) } app.Before = func(ctx *cli.Context) error { @@ -245,9 +272,7 @@ func main() { Log.Formatter = &logrus.TextFormatter{} Log.Infof("%s version: %s", AppName, app.Version) - for _, str := range earlyDebug { - Log.Infof("%s", str) - } + earlyDisplay() Log.Debugf("\nEnvironment: %v\n", os.Environ()) if err = XdsConnInit(ctx); err != nil { @@ -347,8 +372,11 @@ func XdsConnInit(ctx *cli.Context) error { return cli.NewExitError("ERROR while getting XDS config: "+err.Error(), 1) } svrCfg := xdsConf.Servers[XdsServerIndexGet()] - if serverURL != "" && (svrCfg.URL != serverURL || !svrCfg.Connected) { - svrCfg.URL = serverURL + if (serverURL != "" && svrCfg.URL != serverURL) || !svrCfg.Connected { + Log.Infof("Update XDS Server config: serverURL=%v, svrCfg=%v", serverURL, svrCfg) + if serverURL != "" { + svrCfg.URL = serverURL + } svrCfg.ConnRetry = 10 if err := XdsConfigSet(xdsConf); err != nil { return cli.NewExitError("ERROR while updating XDS server URL: "+err.Error(), 1)