diff options
author | Martin Polden <mpolden@mpolden.no> | 2022-02-02 11:14:02 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2022-02-02 11:14:02 +0100 |
commit | b5012f2bdedd1da6c8ba2be7c7ea30881af9e389 (patch) | |
tree | fea0eae9450e8a0f1878283912ac66c5d80177b8 /client | |
parent | 1ee1b5eef080267e4724ef6f0374bff6bc2b55e8 (diff) |
Preserve permissions of extracted files
Diffstat (limited to 'client')
-rw-r--r-- | client/go/cmd/clone.go | 58 | ||||
-rw-r--r-- | client/go/cmd/clone_test.go | 8 | ||||
-rw-r--r-- | client/go/cmd/testdata/sample-apps-master.zip | bin | 4253469 -> 4653209 bytes |
3 files changed, 34 insertions, 32 deletions
diff --git a/client/go/cmd/clone.go b/client/go/cmd/clone.go index 42751334523..1ba95668d29 100644 --- a/client/go/cmd/clone.go +++ b/client/go/cmd/clone.go @@ -65,41 +65,40 @@ variable.`, }, } -func cloneApplication(source string, name string) { +func cloneApplication(applicationName string, applicationDir string) { zipFile := getSampleAppsZip() defer zipFile.Close() - zipReader, zipOpenError := zip.OpenReader(zipFile.Name()) - if zipOpenError != nil { - fatalErr(zipOpenError, "Could not open sample apps zip '", color.Cyan(zipFile.Name()), "'") + r, err := zip.OpenReader(zipFile.Name()) + if err != nil { + fatalErr(err, "Could not open sample apps zip '", color.Cyan(zipFile.Name()), "'") return } - defer zipReader.Close() + defer r.Close() found := false - for _, f := range zipReader.File { - zipEntryPrefix := "sample-apps-master/" + source + "/" - if strings.HasPrefix(f.Name, zipEntryPrefix) { + for _, f := range r.File { + dirPrefix := "sample-apps-master/" + applicationName + "/" + if strings.HasPrefix(f.Name, dirPrefix) { if !found { // Create destination directory lazily when source is found - createErr := os.Mkdir(name, 0755) + createErr := os.Mkdir(applicationDir, 0755) if createErr != nil { - fatalErr(createErr, "Could not create directory '", color.Cyan(name), "'") + fatalErr(createErr, "Could not create directory '", color.Cyan(applicationDir), "'") return } } found = true - copyError := copy(f, name, zipEntryPrefix) - if copyError != nil { - fatalErr(copyError, "Could not copy zip entry '", color.Cyan(f.Name), "' to ", color.Cyan(name)) + if err := copy(f, applicationDir, dirPrefix); err != nil { + fatalErr(err, "Could not copy zip entry '", color.Cyan(f.Name), "' to ", color.Cyan(applicationDir)) return } } } if !found { - fatalErrHint(fmt.Errorf("Could not find source application '%s'", color.Cyan(source)), "Use -f to ignore the cache") + fatalErrHint(fmt.Errorf("Could not find source application '%s'", color.Cyan(applicationName)), "Use -f to ignore the cache") } else { - log.Print("Created ", color.Cyan(name)) + log.Print("Created ", color.Cyan(applicationDir)) } } @@ -176,26 +175,25 @@ func copy(f *zip.File, destinationDir string, zipEntryPrefix string) error { destinationPath := filepath.Join(destinationDir, filepath.FromSlash(strings.TrimPrefix(f.Name, zipEntryPrefix))) if strings.HasSuffix(f.Name, "/") { if f.Name != zipEntryPrefix { // root is already created - createError := os.Mkdir(destinationPath, 0755) - if createError != nil { - return createError + if err := os.Mkdir(destinationPath, 0755); err != nil { + return err } } } else { - zipEntry, zipEntryOpenError := f.Open() - if zipEntryOpenError != nil { - return zipEntryOpenError + r, err := f.Open() + if err != nil { + return err } - defer zipEntry.Close() - - destination, createError := os.Create(destinationPath) - if createError != nil { - return createError + defer r.Close() + destination, err := os.Create(destinationPath) + if err != nil { + return err } - - _, copyError := io.Copy(destination, zipEntry) - if copyError != nil { - return copyError + if _, err := io.Copy(destination, r); err != nil { + return err + } + if err := os.Chmod(destinationPath, f.Mode()); err != nil { + return err } } return nil diff --git a/client/go/cmd/clone_test.go b/client/go/cmd/clone_test.go index 054dc7b21fb..5b69672495c 100644 --- a/client/go/cmd/clone_test.go +++ b/client/go/cmd/clone_test.go @@ -5,6 +5,7 @@ package cmd import ( + "io/fs" "os" "path/filepath" "testing" @@ -15,7 +16,7 @@ import ( ) func TestClone(t *testing.T) { - assertCreated("album-recommendation-selfhosted", "mytestapp", t) + assertCreated("text-search", "mytestapp", t) } func assertCreated(sampleAppName string, app string, t *testing.T) { @@ -32,6 +33,9 @@ func assertCreated(sampleAppName string, app string, t *testing.T) { assert.True(t, util.IsDirectory(filepath.Join(app, "src", "main", "application"))) servicesStat, _ := os.Stat(filepath.Join(app, "src", "main", "application", "services.xml")) - servicesSize := int64(2474) + servicesSize := int64(1772) assert.Equal(t, servicesSize, servicesStat.Size()) + + scriptStat, _ := os.Stat(filepath.Join(app, "bin", "convert-msmarco.sh")) + assert.Equal(t, fs.FileMode(0755), scriptStat.Mode()) } diff --git a/client/go/cmd/testdata/sample-apps-master.zip b/client/go/cmd/testdata/sample-apps-master.zip Binary files differindex 6ad49361072..c8fb40af713 100644 --- a/client/go/cmd/testdata/sample-apps-master.zip +++ b/client/go/cmd/testdata/sample-apps-master.zip |