grafana/pkg/cmd/grafana-cli/main.go

98 lines
1.9 KiB
Go
Raw Normal View History

2016-02-15 07:09:34 -06:00
package main
import (
"fmt"
2016-03-28 14:42:26 -05:00
"os"
"runtime"
2016-02-15 07:09:34 -06:00
"github.com/codegangsta/cli"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
2016-02-15 07:09:34 -06:00
)
2016-02-15 09:11:37 -06:00
var version = "master"
2016-03-28 14:42:26 -05:00
func getGrafanaPluginDir() string {
currentOS := runtime.GOOS
defaultNix := "/var/lib/grafana/plugins"
if currentOS == "windows" {
return "../data/plugins"
2016-02-15 07:09:34 -06:00
}
pwd, err := os.Getwd()
if err != nil {
2016-06-03 06:40:48 -05:00
logger.Error("Could not get current path. using default")
return defaultNix
}
if isDevenvironment(pwd) {
return "../data/plugins"
}
return defaultNix
}
func isDevenvironment(pwd string) bool {
// if ../conf/defaults.ini exists, grafana is not installed as package
// that its in development environment.
_, err := os.Stat("../conf/defaults.ini")
return err == nil
2016-02-15 07:09:34 -06:00
}
func main() {
setupLogging()
2016-02-15 07:09:34 -06:00
app := cli.NewApp()
app.Name = "Grafana cli"
2016-03-28 14:42:26 -05:00
app.Usage = ""
app.Author = "Grafana Project"
2016-02-15 07:09:34 -06:00
app.Email = "https://github.com/grafana/grafana"
2016-02-15 09:11:37 -06:00
app.Version = version
2016-02-15 07:09:34 -06:00
app.Flags = []cli.Flag{
cli.StringFlag{
2016-03-28 14:42:26 -05:00
Name: "pluginsDir",
Usage: "path to the grafana plugin directory",
Value: getGrafanaPluginDir(),
2016-03-21 04:05:23 -05:00
EnvVar: "GF_PLUGIN_DIR",
2016-02-15 07:09:34 -06:00
},
cli.StringFlag{
2016-03-21 04:05:23 -05:00
Name: "repo",
Usage: "url to the plugin repository",
2016-03-28 10:36:27 -05:00
Value: "https://grafana.net/api/plugins",
2016-03-21 04:05:23 -05:00
EnvVar: "GF_PLUGIN_REPO",
},
2016-02-15 07:09:34 -06:00
cli.BoolFlag{
Name: "debug, d",
Usage: "enable debug logging",
},
}
app.Commands = commands.Commands
app.CommandNotFound = cmdNotFound
if err := app.Run(os.Args); err != nil {
logger.Errorf("%v", err)
2016-02-15 07:09:34 -06:00
}
}
func setupLogging() {
2016-02-15 07:09:34 -06:00
for _, f := range os.Args {
if f == "-D" || f == "--debug" || f == "-debug" {
logger.SetDebug(true)
2016-02-15 07:09:34 -06:00
}
}
}
func cmdNotFound(c *cli.Context, command string) {
fmt.Printf(
"%s: '%s' is not a %s command. See '%s --help'.\n",
c.App.Name,
command,
c.App.Name,
os.Args[0],
)
os.Exit(1)
}