main.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "runtime"
  7. "github.com/codegangsta/cli"
  8. "github.com/grafana/grafana/pkg/cmd/grafana-cli/commands"
  9. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  10. "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
  11. "github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
  12. )
  13. var version = "master"
  14. func main() {
  15. setupLogging()
  16. flag.Parse()
  17. app := cli.NewApp()
  18. app.Name = "Grafana cli"
  19. app.Usage = ""
  20. app.Author = "Grafana Project"
  21. app.Email = "https://github.com/grafana/grafana"
  22. app.Version = version
  23. app.Flags = []cli.Flag{
  24. cli.StringFlag{
  25. Name: "pluginsDir",
  26. Usage: "path to the grafana plugin directory",
  27. Value: utils.GetGrafanaPluginDir(runtime.GOOS),
  28. EnvVar: "GF_PLUGIN_DIR",
  29. },
  30. cli.StringFlag{
  31. Name: "repo",
  32. Usage: "url to the plugin repository",
  33. Value: "https://grafana.com/api/plugins",
  34. EnvVar: "GF_PLUGIN_REPO",
  35. },
  36. cli.StringFlag{
  37. Name: "pluginUrl",
  38. Usage: "Full url to the plugin zip file instead of downloading the plugin from grafana.com/api",
  39. Value: "",
  40. EnvVar: "GF_PLUGIN_URL",
  41. },
  42. cli.BoolFlag{
  43. Name: "insecure",
  44. Usage: "Skip TLS verification (insecure)",
  45. },
  46. cli.BoolFlag{
  47. Name: "debug, d",
  48. Usage: "enable debug logging",
  49. },
  50. }
  51. app.Before = func(c *cli.Context) error {
  52. services.Init(version, c.GlobalBool("insecure"))
  53. return nil
  54. }
  55. app.Commands = commands.Commands
  56. app.CommandNotFound = cmdNotFound
  57. if err := app.Run(os.Args); err != nil {
  58. logger.Errorf("%v", err)
  59. }
  60. }
  61. func setupLogging() {
  62. for _, f := range os.Args {
  63. if f == "-d" || f == "--debug" || f == "-debug" {
  64. logger.SetDebug(true)
  65. }
  66. }
  67. }
  68. func cmdNotFound(c *cli.Context, command string) {
  69. fmt.Printf(
  70. "%s: '%s' is not a %s command. See '%s --help'.\n",
  71. c.App.Name,
  72. command,
  73. c.App.Name,
  74. os.Args[0],
  75. )
  76. os.Exit(1)
  77. }