]> git.lizzy.rs Git - micro.git/commitdiff
fix plugin zips which contain a root directory
authorboombuler <boombuler@gmail.com>
Sat, 1 Oct 2016 06:05:05 +0000 (08:05 +0200)
committerboombuler <boombuler@gmail.com>
Sat, 1 Oct 2016 06:05:05 +0000 (08:05 +0200)
cmd/micro/pluginmanager.go

index 837ef65987f9bd50849f42b18ed9c8e307359149..c9d0b7ac0dac246665342680d852917b80ef1806 100644 (file)
@@ -360,8 +360,32 @@ func (pv *PluginVersion) DownloadAndInstall() error {
        if err = os.MkdirAll(targetDir, dirPerm); err != nil {
                return err
        }
+
+       // Check if all files in zip are in the same directory.
+       // this might be the case if the plugin zip contains the whole plugin dir
+       // instead of its content.
+       var prefix string
+       allPrefixed := false
+       for i, f := range z.File {
+               parts := strings.Split(f.Name, "/")
+               if i == 0 {
+                       prefix = parts[0]
+               } else if parts[0] != prefix {
+                       allPrefixed = false
+                       break
+               } else {
+                       // switch to true since we have at least a second file
+                       allPrefixed = true
+               }
+       }
+
        for _, f := range z.File {
-               targetName := filepath.Join(targetDir, filepath.Join(strings.Split(f.Name, "/")...))
+               parts := strings.Split(f.Name, "/")
+               if allPrefixed {
+                       parts = parts[1:]
+               }
+
+               targetName := filepath.Join(targetDir, filepath.Join(parts...))
                if f.FileInfo().IsDir() {
                        if err := os.MkdirAll(targetName, dirPerm); err != nil {
                                return err