main.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "strings"
  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 "C:\\opt\\grafana\\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 grafana-cli is executed from the cmd folder we can assume
  30. // that its in development environment.
  31. return strings.HasSuffix(pwd, "/pkg/cmd/grafana-cli")
  32. }
  33. func main() {
  34. SetupLogging()
  35. app := cli.NewApp()
  36. app.Name = "Grafana cli"
  37. app.Usage = ""
  38. app.Author = "Grafana Project"
  39. app.Email = "https://github.com/grafana/grafana"
  40. app.Version = version
  41. app.Flags = []cli.Flag{
  42. cli.StringFlag{
  43. Name: "pluginsDir",
  44. Usage: "path to the grafana plugin directory",
  45. Value: getGrafanaPluginDir(),
  46. EnvVar: "GF_PLUGIN_DIR",
  47. },
  48. cli.StringFlag{
  49. Name: "repo",
  50. Usage: "url to the plugin repository",
  51. Value: "https://grafana.net/api/plugins",
  52. EnvVar: "GF_PLUGIN_REPO",
  53. },
  54. cli.BoolFlag{
  55. Name: "debug, d",
  56. Usage: "enable debug logging",
  57. },
  58. }
  59. app.Commands = commands.Commands
  60. app.CommandNotFound = cmdNotFound
  61. if err := app.Run(os.Args); err != nil {
  62. log.Errorf("%v", err)
  63. }
  64. }
  65. func SetupLogging() {
  66. for _, f := range os.Args {
  67. if f == "-D" || f == "--debug" || f == "-debug" {
  68. log.SetDebug(true)
  69. }
  70. }
  71. }
  72. func cmdNotFound(c *cli.Context, command string) {
  73. fmt.Printf(
  74. "%s: '%s' is not a %s command. See '%s --help'.\n",
  75. c.App.Name,
  76. command,
  77. c.App.Name,
  78. os.Args[0],
  79. )
  80. os.Exit(1)
  81. }