main.go 1.8 KB

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