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 { log.Errorf("\n%s: ", color.RedString("Error")) log.Errorf("%s\n\n", err) cmd.ShowHelp() os.Exit(1) } else { log.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\n\n") } } }
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 { log.Errorf("%v", err) } }
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 { log.Errorf("Failed to extract file: %v", err) } io.Copy(dst, src) dst.Close() src.Close() } } return nil }
func main() { SetupLogging() app := cli.NewApp() app.Name = "Grafana cli" app.Author = "raintank" app.Email = "https://github.com/grafana/grafana" app.Version = version app.Flags = []cli.Flag{ cli.StringFlag{ Name: "path", Usage: "path to the grafana installation", Value: getGrafanaPluginPath(), }, cli.StringFlag{ Name: "repo", Usage: "url to the plugin repository", Value: "https://grafana-net.raintank.io/api/plugins", }, cli.BoolFlag{ Name: "debug, d", Usage: "enable debug logging", }, } app.Commands = commands.Commands app.CommandNotFound = cmdNotFound if err := app.Run(os.Args); err != nil { log.Errorf("%v", err) } }
func downloadFile(pluginName, filePath, url string) (err error) { defer func() { if r := recover(); r != nil { retryCount++ if retryCount == 1 { log.Debug("\nFailed downloading. Will retry once.\n") downloadFile(pluginName, filePath, url) } 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() { os.Mkdir(newFile, 0777) } else { dst, err := os.Create(newFile) if err != nil { if strings.Contains(err.Error(), "permission denied") { return fmt.Errorf( "Could not create file %s. permission deined. Make sure you have write access to plugindir", newFile) } } defer dst.Close() src, err := zf.Open() if err != nil { log.Errorf("%v", err) } defer src.Close() io.Copy(dst, src) } } return nil }
func downloadFile(pluginName, filepath, url string) (err error) { 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() { os.Mkdir(newfile, 0777) } else { dst, err := os.Create(newfile) if err != nil { log.Errorf("%v", err) } defer dst.Close() src, err := zf.Open() if err != nil { log.Errorf("%v", err) } defer src.Close() io.Copy(dst, src) } } return nil }