commands.go 2.8 KB

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