From 4e4c509763261de7881baeb460b37380a846fb0e Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Thu, 18 Jun 2020 10:48:52 -0700 Subject: [PATCH] feat(influxd): refactor run command to use cli.Program to respect config file --- CHANGELOG.md | 1 + cmd/influxd/launcher/launcher.go | 39 +++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32dad0982e8..c25118b87f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ 1. [18595](https://github.com/influxdata/influxdb/pull/18595): Add ability to skip resources in a template by kind or by metadata.name 1. [18600](https://github.com/influxdata/influxdb/pull/18600): Extend influx apply with resource filter capabilities 1. [18601](https://github.com/influxdata/influxdb/pull/18601): Provide active config running influx config without args +1. [18606](https://github.com/influxdata/influxdb/pull/18606): Enable influxd binary to look for a config file on startup ## v2.0.0-beta.12 [2020-06-12] diff --git a/cmd/influxd/launcher/launcher.go b/cmd/influxd/launcher/launcher.go index 04f6b97daac..2834c362aaa 100644 --- a/cmd/influxd/launcher/launcher.go +++ b/cmd/influxd/launcher/launcher.go @@ -87,19 +87,19 @@ const ( // NewCommand creates the command to run influxdb. func NewCommand() *cobra.Command { l := NewLauncher() - cmd := &cobra.Command{ - Use: "run", - Short: "Start the influxd server (default)", - Run: func(cmd *cobra.Command, args []string) { + + prog := cli.Program{ + Name: "run", + Opts: launcherOpts(l), + Run: func() error { // exit with SIGINT and SIGTERM ctx := context.Background() ctx = signals.WithStandardSignals(ctx) if err := l.run(ctx); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) + return err } else if !l.Running() { - os.Exit(1) + return errors.New("the daemon is already running") } var wg sync.WaitGroup @@ -120,23 +120,37 @@ func NewCommand() *cobra.Command { defer cancel() l.Shutdown(ctx) wg.Wait() + + return nil }, } - buildLauncherCommand(l, cmd) + cmd := cli.NewCommand(&prog) + cmd.Long = ` + Start up the daemon configured with flags/env vars/config file. + The order of precedence for config options are as follows (1 highest, 3 lowest): + 1. flags + 2. env vars + 3. config file + + A config file can be provided via the INFLUXD_CONFIG_FILE env var. If a file is + not provided via an env var, influxd will look in the current directory for a + config.yaml file. If one does not exist, then it will continue unchanged. +` + cmd.AddCommand(inspect.NewCommand()) return cmd } var vaultConfig vault.Config -func buildLauncherCommand(l *Launcher, cmd *cobra.Command) { +func launcherOpts(l *Launcher) []cli.Opt { dir, err := fs.InfluxDir() if err != nil { panic(fmt.Errorf("failed to determine influx directory: %v", err)) } - opts := []cli.Opt{ + return []cli.Opt{ { DestP: &l.logLevel, Flag: "log-level", @@ -312,7 +326,10 @@ func buildLauncherCommand(l *Launcher, cmd *cobra.Command) { Desc: "feature flag overrides", }, } - cli.BindOptions(cmd, opts) +} + +func buildLauncherCommand(l *Launcher, cmd *cobra.Command) { + cli.BindOptions(cmd, launcherOpts(l)) cmd.AddCommand(inspect.NewCommand()) }