Example #1
0
func runCommand(command func(commandLine CommandLine) error) func(context *cli.Context) {
	return func(context *cli.Context) {

		cmd := &contextCommandLine{context}
		if err := command(cmd); err != nil {
			logger.Errorf("\n%s: ", color.RedString("Error"))
			logger.Errorf("%s\n\n", err)

			cmd.ShowHelp()
			os.Exit(1)
		} else {
			logger.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\n\n")
		}
	}
}
Example #2
0
func downloadFile(pluginName, filePath, url string) (err error) {
	defer func() {
		if r := recover(); r != nil {
			retryCount++
			if retryCount < 3 {
				fmt.Println("Failed downloading. Will retry once.")
				err = downloadFile(pluginName, filePath, url)
			} else {
				failure := fmt.Sprintf("%v", r)
				if failure == "runtime error: makeslice: len out of range" {
					err = fmt.Errorf("Corrupt http response from source. Please try again.\n")
				} else {
					panic(r)
				}
			}
		}
	}()

	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	r, err := zip.NewReader(bytes.NewReader(body), resp.ContentLength)
	if err != nil {
		return err
	}
	for _, zf := range r.File {
		newFile := path.Join(filePath, RemoveGitBuildFromName(pluginName, zf.Name))

		if zf.FileInfo().IsDir() {
			err := os.Mkdir(newFile, 0777)
			if PermissionsError(err) {
				return fmt.Errorf(permissionsDeniedMessage, newFile)
			}
		} else {
			dst, err := os.Create(newFile)
			if PermissionsError(err) {
				return fmt.Errorf(permissionsDeniedMessage, newFile)
			}

			src, err := zf.Open()
			if err != nil {
				logger.Errorf("Failed to extract file: %v", err)
			}

			io.Copy(dst, src)
			dst.Close()
			src.Close()
		}
	}

	return nil
}
Example #3
0
func main() {
	setupLogging()

	app := cli.NewApp()
	app.Name = "Grafana cli"
	app.Usage = ""
	app.Author = "Grafana Project"
	app.Email = "https://github.com/grafana/grafana"
	app.Version = version
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:   "pluginsDir",
			Usage:  "path to the grafana plugin directory",
			Value:  getGrafanaPluginDir(),
			EnvVar: "GF_PLUGIN_DIR",
		},
		cli.StringFlag{
			Name:   "repo",
			Usage:  "url to the plugin repository",
			Value:  "https://grafana.net/api/plugins",
			EnvVar: "GF_PLUGIN_REPO",
		},
		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)
	}
}
Example #4
0
func runDbCommand(command func(commandLine CommandLine) error) func(context *cli.Context) {
	return func(context *cli.Context) {

		flag.Parse()
		setting.NewConfigContext(&setting.CommandLineArgs{
			Config:   *configFile,
			HomePath: *homePath,
			Args:     flag.Args(),
		})

		sqlstore.NewEngine()

		cmd := &contextCommandLine{context}
		if err := command(cmd); err != nil {
			logger.Errorf("\n%s: ", color.RedString("Error"))
			logger.Errorf("%s\n\n", err)

			cmd.ShowHelp()
			os.Exit(1)
		} else {
			logger.Info("\n\n")
		}
	}
}