commands.go 2.8 KB

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