mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 18:26:52 +08:00
grafana-cli: Upgrade to urfave/cli v2 (#22402)
* grafana-cli: Upgrade to urfave/cli v2
This commit is contained in:
@ -1,34 +1,33 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/fatih/color"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/datamigrations"
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func runDbCommand(command func(commandLine utils.CommandLine, sqlStore *sqlstore.SqlStore) error) func(context *cli.Context) {
|
||||
return func(context *cli.Context) {
|
||||
func runDbCommand(command func(commandLine utils.CommandLine, sqlStore *sqlstore.SqlStore) error) func(context *cli.Context) error {
|
||||
return func(context *cli.Context) error {
|
||||
cmd := &utils.ContextCommandLine{Context: context}
|
||||
debug := cmd.GlobalBool("debug")
|
||||
debug := cmd.Bool("debug")
|
||||
|
||||
cfg := setting.NewCfg()
|
||||
|
||||
configOptions := strings.Split(cmd.GlobalString("configOverrides"), " ")
|
||||
configOptions := strings.Split(cmd.String("configOverrides"), " ")
|
||||
if err := cfg.Load(&setting.CommandLineArgs{
|
||||
Config: cmd.ConfigFile(),
|
||||
HomePath: cmd.HomePath(),
|
||||
Args: append(configOptions, cmd.Args()...), // tailing arguments have precedence over the options string
|
||||
Args: append(configOptions, cmd.Args().Slice()...), // tailing arguments have precedence over the options string
|
||||
}); err != nil {
|
||||
logger.Errorf("\n%s: Failed to load configuration", color.RedString("Error"))
|
||||
os.Exit(1)
|
||||
return errutil.Wrap("failed to load configuration", err)
|
||||
}
|
||||
|
||||
if debug {
|
||||
@ -39,80 +38,75 @@ func runDbCommand(command func(commandLine utils.CommandLine, sqlStore *sqlstore
|
||||
engine.Cfg = cfg
|
||||
engine.Bus = bus.GetBus()
|
||||
if err := engine.Init(); err != nil {
|
||||
logger.Errorf("\n%s: Failed to initialize SQL engine", color.RedString("Error"))
|
||||
os.Exit(1)
|
||||
return errutil.Wrap("failed to initialize SQL engine", err)
|
||||
}
|
||||
|
||||
if err := command(cmd, engine); err != nil {
|
||||
logger.Errorf("\n%s: ", color.RedString("Error"))
|
||||
logger.Errorf("%s\n\n", err)
|
||||
|
||||
if err := cmd.ShowHelp(); err != nil {
|
||||
logger.Errorf("\n%s: Failed to show help: %s %s\n\n", color.RedString("Error"),
|
||||
color.RedString("✗"), err)
|
||||
}
|
||||
os.Exit(1)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("\n\n")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func runPluginCommand(command func(commandLine utils.CommandLine) error) func(context *cli.Context) {
|
||||
return func(context *cli.Context) {
|
||||
|
||||
func runPluginCommand(command func(commandLine utils.CommandLine) error) func(context *cli.Context) error {
|
||||
return func(context *cli.Context) error {
|
||||
cmd := &utils.ContextCommandLine{Context: context}
|
||||
if err := command(cmd); err != nil {
|
||||
logger.Errorf("\n%s: ", color.RedString("Error"))
|
||||
logger.Errorf("%s %s\n\n", color.RedString("✗"), err)
|
||||
|
||||
if err := cmd.ShowHelp(); err != nil {
|
||||
logger.Errorf("\n%s: Failed to show help: %s %s\n\n", color.RedString("Error"),
|
||||
color.RedString("✗"), err)
|
||||
}
|
||||
os.Exit(1)
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\n\n")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var pluginCommands = []cli.Command{
|
||||
// Command contains command state.
|
||||
type Command struct {
|
||||
Client utils.ApiClient
|
||||
}
|
||||
|
||||
var cmd Command = Command{
|
||||
Client: &services.GrafanaComClient{},
|
||||
}
|
||||
|
||||
var pluginCommands = []*cli.Command{
|
||||
{
|
||||
Name: "install",
|
||||
Usage: "install <plugin id> <plugin version (optional)>",
|
||||
Action: runPluginCommand(installCommand),
|
||||
Action: runPluginCommand(cmd.installCommand),
|
||||
}, {
|
||||
Name: "list-remote",
|
||||
Usage: "list remote available plugins",
|
||||
Action: runPluginCommand(listRemoteCommand),
|
||||
Action: runPluginCommand(cmd.listRemoteCommand),
|
||||
}, {
|
||||
Name: "list-versions",
|
||||
Usage: "list-versions <plugin id>",
|
||||
Action: runPluginCommand(listversionsCommand),
|
||||
Action: runPluginCommand(cmd.listVersionsCommand),
|
||||
}, {
|
||||
Name: "update",
|
||||
Usage: "update <plugin id>",
|
||||
Aliases: []string{"upgrade"},
|
||||
Action: runPluginCommand(upgradeCommand),
|
||||
Action: runPluginCommand(cmd.upgradeCommand),
|
||||
}, {
|
||||
Name: "update-all",
|
||||
Aliases: []string{"upgrade-all"},
|
||||
Usage: "update all your installed plugins",
|
||||
Action: runPluginCommand(upgradeAllCommand),
|
||||
Action: runPluginCommand(cmd.upgradeAllCommand),
|
||||
}, {
|
||||
Name: "ls",
|
||||
Usage: "list all installed plugins",
|
||||
Action: runPluginCommand(lsCommand),
|
||||
Action: runPluginCommand(cmd.lsCommand),
|
||||
}, {
|
||||
Name: "uninstall",
|
||||
Aliases: []string{"remove"},
|
||||
Usage: "uninstall <plugin id>",
|
||||
Action: runPluginCommand(removeCommand),
|
||||
Action: runPluginCommand(cmd.removeCommand),
|
||||
},
|
||||
}
|
||||
|
||||
var adminCommands = []cli.Command{
|
||||
var adminCommands = []*cli.Command{
|
||||
{
|
||||
Name: "reset-admin-password",
|
||||
Usage: "reset-admin-password <new password>",
|
||||
@ -121,7 +115,7 @@ var adminCommands = []cli.Command{
|
||||
{
|
||||
Name: "data-migration",
|
||||
Usage: "Runs a script that migrates or cleanups data in your db",
|
||||
Subcommands: []cli.Command{
|
||||
Subcommands: []*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.",
|
||||
@ -131,7 +125,7 @@ var adminCommands = []cli.Command{
|
||||
},
|
||||
}
|
||||
|
||||
var Commands = []cli.Command{
|
||||
var Commands = []*cli.Command{
|
||||
{
|
||||
Name: "plugins",
|
||||
Usage: "Manage plugins for grafana",
|
||||
|
Reference in New Issue
Block a user