|
|
|
@ -57,6 +57,8 @@ func installCommand(c CommandLine) error { |
|
|
|
return InstallPlugin(pluginToInstall, version, c) |
|
|
|
return InstallPlugin(pluginToInstall, version, c) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// InstallPlugin downloads the plugin code as a zip file from the Grafana.com API
|
|
|
|
|
|
|
|
// and then extracts the zip into the plugins directory.
|
|
|
|
func InstallPlugin(pluginName, version string, c CommandLine) error { |
|
|
|
func InstallPlugin(pluginName, version string, c CommandLine) error { |
|
|
|
pluginFolder := c.PluginDirectory() |
|
|
|
pluginFolder := c.PluginDirectory() |
|
|
|
downloadURL := c.PluginURL() |
|
|
|
downloadURL := c.PluginURL() |
|
|
|
@ -152,6 +154,10 @@ func downloadFile(pluginName, filePath, url string) (err error) { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return extractFiles(body, pluginName, filePath) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func extractFiles(body []byte, pluginName string, filePath string) error { |
|
|
|
r, err := zip.NewReader(bytes.NewReader(body), int64(len(body))) |
|
|
|
r, err := zip.NewReader(bytes.NewReader(body), int64(len(body))) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
@ -161,12 +167,18 @@ func downloadFile(pluginName, filePath, url string) (err error) { |
|
|
|
|
|
|
|
|
|
|
|
if zf.FileInfo().IsDir() { |
|
|
|
if zf.FileInfo().IsDir() { |
|
|
|
err := os.Mkdir(newFile, 0777) |
|
|
|
err := os.Mkdir(newFile, 0777) |
|
|
|
if PermissionsError(err) { |
|
|
|
if permissionsError(err) { |
|
|
|
return fmt.Errorf(permissionsDeniedMessage, newFile) |
|
|
|
return fmt.Errorf(permissionsDeniedMessage, newFile) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
dst, err := os.Create(newFile) |
|
|
|
fileMode := zf.Mode() |
|
|
|
if PermissionsError(err) { |
|
|
|
|
|
|
|
|
|
|
|
if strings.HasSuffix(newFile, "_linux_amd64") || strings.HasSuffix(newFile, "_darwin_amd64") { |
|
|
|
|
|
|
|
fileMode = os.FileMode(0755) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dst, err := os.OpenFile(newFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode) |
|
|
|
|
|
|
|
if permissionsError(err) { |
|
|
|
return fmt.Errorf(permissionsDeniedMessage, newFile) |
|
|
|
return fmt.Errorf(permissionsDeniedMessage, newFile) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -184,6 +196,6 @@ func downloadFile(pluginName, filePath, url string) (err error) { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func PermissionsError(err error) bool { |
|
|
|
func permissionsError(err error) bool { |
|
|
|
return err != nil && strings.Contains(err.Error(), "permission denied") |
|
|
|
return err != nil && strings.Contains(err.Error(), "permission denied") |
|
|
|
} |
|
|
|
} |
|
|
|
|