From 7c176cfa22e0170ec230f6966e5072634db925b5 Mon Sep 17 00:00:00 2001 From: gotjosh Date: Mon, 17 Jun 2019 11:27:26 +0100 Subject: [PATCH] cli: grafana-cli should receive flags from the command line (#17606) grafana-cli should allow configuration overrides to be received from the command line. e.g. ``` grafana-cli admin reset-password cfg:default.paths.logs=custom/log/directory/ ``` Seems like we missed the inclusion of `flag.Parse` as we run the command, to be able to consume them. Additionally, it'll be useful for the user to know whenever these are being overriden or not - hence the addition of logging the configuration to be used as we run the command. (cherry picked from commit 6fbca90269a0fe17f4e5ab7dac608b71ddd94db6) --- pkg/cmd/grafana-cli/commands/commands.go | 26 +++++++++++++++--------- pkg/cmd/grafana-cli/main.go | 2 ++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/grafana-cli/commands/commands.go b/pkg/cmd/grafana-cli/commands/commands.go index ebaee557348..2ef9e78af4f 100644 --- a/pkg/cmd/grafana-cli/commands/commands.go +++ b/pkg/cmd/grafana-cli/commands/commands.go @@ -19,12 +19,15 @@ func runDbCommand(command func(commandLine utils.CommandLine, sqlStore *sqlstore cmd := &utils.ContextCommandLine{Context: context} cfg := setting.NewCfg() + cfg.Load(&setting.CommandLineArgs{ Config: cmd.String("config"), HomePath: cmd.String("homepath"), Args: flag.Args(), }) + cfg.LogConfigSources() + engine := &sqlstore.SqlStore{} engine.Cfg = cfg engine.Bus = bus.GetBus() @@ -93,21 +96,23 @@ var pluginCommands = []cli.Command{ }, } +var dbCommandFlags = []cli.Flag{ + cli.StringFlag{ + Name: "homepath", + Usage: "path to grafana install/home path, defaults to working directory", + }, + cli.StringFlag{ + Name: "config", + Usage: "path to config file", + }, +} + var adminCommands = []cli.Command{ { Name: "reset-admin-password", Usage: "reset-admin-password ", Action: runDbCommand(resetPasswordCommand), - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "homepath", - Usage: "path to grafana install/home path, defaults to working directory", - }, - cli.StringFlag{ - Name: "config", - Usage: "path to config file", - }, - }, + Flags: dbCommandFlags, }, { Name: "data-migration", @@ -117,6 +122,7 @@ var adminCommands = []cli.Command{ Name: "encrypt-datasource-passwords", Usage: "Migrates passwords from unsecured fields to secure_json_data field. Return ok unless there is an error. Safe to execute multiple times.", Action: runDbCommand(datamigrations.EncryptDatasourcePaswords), + Flags: dbCommandFlags, }, }, }, diff --git a/pkg/cmd/grafana-cli/main.go b/pkg/cmd/grafana-cli/main.go index a17de5603e0..a83efb3fb76 100644 --- a/pkg/cmd/grafana-cli/main.go +++ b/pkg/cmd/grafana-cli/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "os" "runtime" @@ -17,6 +18,7 @@ var version = "master" func main() { setupLogging() + flag.Parse() app := cli.NewApp() app.Name = "Grafana cli" app.Usage = ""