main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "github.com/codegangsta/cli"
  7. "github.com/grafana/grafana/pkg/cmd/grafana-cli/commands"
  8. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  9. "github.com/lunny/log"
  10. )
  11. var version = "master"
  12. func getGrafanaPluginDir() string {
  13. currentOS := runtime.GOOS
  14. defaultNix := "/var/lib/grafana/plugins"
  15. if currentOS == "windows" {
  16. return "../data/plugins"
  17. }
  18. pwd, err := os.Getwd()
  19. if err != nil {
  20. log.Error("Could not get current path. using default")
  21. return defaultNix
  22. }
  23. if isDevenvironment(pwd) {
  24. return "../data/plugins"
  25. }
  26. return defaultNix
  27. }
  28. func isDevenvironment(pwd string) bool {
  29. // if ../conf/defaults.ini exists, grafana is not installed as package
  30. // that its in development environment.
  31. _, err := os.Stat("../conf/defaults.ini")
  32. return err == nil
  33. }
  34. func main() {
  35. setupLogging()
  36. app := cli.NewApp()
  37. app.Name = "Grafana cli"
  38. app.Usage = ""
  39. app.Author = "Grafana Project"
  40. app.Email = "https://github.com/grafana/grafana"
  41. app.Version = version
  42. app.Flags = []cli.Flag{
  43. cli.StringFlag{
  44. Name: "pluginsDir",
  45. Usage: "path to the grafana plugin directory",
  46. Value: getGrafanaPluginDir(),
  47. EnvVar: "GF_PLUGIN_DIR",
  48. },
  49. cli.StringFlag{
  50. Name: "repo",
  51. Usage: "url to the plugin repository",
  52. Value: "https://grafana.net/api/plugins",
  53. EnvVar: "GF_PLUGIN_REPO",
  54. },
  55. cli.BoolFlag{
  56. Name: "debug, d",
  57. Usage: "enable debug logging",
  58. },
  59. }
  60. app.Commands = commands.Commands
  61. app.CommandNotFound = cmdNotFound
  62. if err := app.Run(os.Args); err != nil {
  63. logger.Errorf("%v", err)
  64. }
  65. }
  66. func setupLogging() {
  67. for _, f := range os.Args {
  68. if f == "-D" || f == "--debug" || f == "-debug" {
  69. logger.SetDebug(true)
  70. }
  71. }
  72. }
  73. func cmdNotFound(c *cli.Context, command string) {
  74. fmt.Printf(
  75. "%s: '%s' is not a %s command. See '%s --help'.\n",
  76. c.App.Name,
  77. command,
  78. c.App.Name,
  79. os.Args[0],
  80. )
  81. os.Exit(1)
  82. }