commands.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package commands
  2. import (
  3. "flag"
  4. "os"
  5. "github.com/codegangsta/cli"
  6. "github.com/fatih/color"
  7. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  8. "github.com/grafana/grafana/pkg/services/sqlstore"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. func runDbCommand(command func(commandLine CommandLine) error) func(context *cli.Context) {
  12. return func(context *cli.Context) {
  13. cmd := &contextCommandLine{context}
  14. cfg := setting.NewCfg()
  15. cfg.Load(&setting.CommandLineArgs{
  16. Config: cmd.String("config"),
  17. HomePath: cmd.String("homepath"),
  18. Args: flag.Args(),
  19. })
  20. sqlstore.NewEngine()
  21. if err := command(cmd); err != nil {
  22. logger.Errorf("\n%s: ", color.RedString("Error"))
  23. logger.Errorf("%s\n\n", err)
  24. cmd.ShowHelp()
  25. os.Exit(1)
  26. } else {
  27. logger.Info("\n\n")
  28. }
  29. }
  30. }
  31. func runPluginCommand(command func(commandLine CommandLine) error) func(context *cli.Context) {
  32. return func(context *cli.Context) {
  33. cmd := &contextCommandLine{context}
  34. if err := command(cmd); err != nil {
  35. logger.Errorf("\n%s: ", color.RedString("Error"))
  36. logger.Errorf("%s %s\n\n", color.RedString("✗"), err)
  37. cmd.ShowHelp()
  38. os.Exit(1)
  39. } else {
  40. logger.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\n\n")
  41. }
  42. }
  43. }
  44. var pluginCommands = []cli.Command{
  45. {
  46. Name: "install",
  47. Usage: "install <plugin id> <plugin version (optional)>",
  48. Action: runPluginCommand(installCommand),
  49. }, {
  50. Name: "list-remote",
  51. Usage: "list remote available plugins",
  52. Action: runPluginCommand(listremoteCommand),
  53. }, {
  54. Name: "list-versions",
  55. Usage: "list-versions <plugin id>",
  56. Action: runPluginCommand(listversionsCommand),
  57. }, {
  58. Name: "update",
  59. Usage: "update <plugin id>",
  60. Aliases: []string{"upgrade"},
  61. Action: runPluginCommand(upgradeCommand),
  62. }, {
  63. Name: "update-all",
  64. Aliases: []string{"upgrade-all"},
  65. Usage: "update all your installed plugins",
  66. Action: runPluginCommand(upgradeAllCommand),
  67. }, {
  68. Name: "ls",
  69. Usage: "list all installed plugins",
  70. Action: runPluginCommand(lsCommand),
  71. }, {
  72. Name: "uninstall",
  73. Aliases: []string{"remove"},
  74. Usage: "uninstall <plugin id>",
  75. Action: runPluginCommand(removeCommand),
  76. },
  77. }
  78. var adminCommands = []cli.Command{
  79. {
  80. Name: "reset-admin-password",
  81. Usage: "reset-admin-password <new password>",
  82. Action: runDbCommand(resetPasswordCommand),
  83. Flags: []cli.Flag{
  84. cli.StringFlag{
  85. Name: "homepath",
  86. Usage: "path to grafana install/home path, defaults to working directory",
  87. },
  88. cli.StringFlag{
  89. Name: "config",
  90. Usage: "path to config file",
  91. },
  92. },
  93. },
  94. }
  95. var Commands = []cli.Command{
  96. {
  97. Name: "plugins",
  98. Usage: "Manage plugins for grafana",
  99. Subcommands: pluginCommands,
  100. },
  101. {
  102. Name: "admin",
  103. Usage: "Grafana admin commands",
  104. Subcommands: adminCommands,
  105. },
  106. }