commands.go 2.9 KB

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