-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Push command #256
Push command #256
Changes from 12 commits
e7e6d49
f9e017d
bc0d52f
99124e4
cc821ff
2dfe735
521caac
4adbc46
b38c131
424bcd2
1637553
e2588b1
43b5612
7125182
222a19e
c9d72a2
c09427c
1fc058f
605fbaf
c7765d7
99d89c1
6fbcf2f
03cc839
eff71ee
8a2ca96
087e4ce
b94d5c8
589f19b
63d8f57
5fddff0
b67d822
5cfb16f
c1c0406
5cbc41f
a8f78f8
9b85802
e4707f0
e1399aa
88a3fa4
6a62db3
055ae03
90cf57d
23879c1
f5a3ee7
53197ac
0633cb8
0e4accf
845f881
6fbf1c9
5abac45
a543970
418f4de
15f0597
1ff7fd5
931acf8
800a15e
f922f78
f648f41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package content | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"github.com/wttech/aemc/pkg/common/pathx" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func Archive(sourcePath, targetFile string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. zipper should |
||
if !pathx.Exists(sourcePath) { | ||
return fmt.Errorf("cannot archive path '%s' to file '%s' as source path does not exist", sourcePath, targetFile) | ||
} | ||
err := pathx.Ensure(filepath.Dir(targetFile)) | ||
if err != nil { | ||
return err | ||
} | ||
err = compress(sourcePath, targetFile) | ||
if err != nil { | ||
return fmt.Errorf("cannot archive dir '%s' to file '%s': %w", sourcePath, targetFile, err) | ||
} | ||
return nil | ||
} | ||
|
||
func Unarchive(sourceFile string, targetDir string) error { | ||
if !pathx.Exists(sourceFile) { | ||
return fmt.Errorf("cannot unarchive file '%s' to dir '%s' as source file does not exist", sourceFile, targetDir) | ||
} | ||
if err := pathx.Ensure(targetDir); err != nil { | ||
return err | ||
} | ||
if err := extract(sourceFile, targetDir); err != nil { | ||
return fmt.Errorf("cannot unarchive file '%s' to dir '%s': %w", sourceFile, targetDir, err) | ||
} | ||
return nil | ||
} | ||
|
||
func compress(src string, dest string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
destFile, err := os.Create(dest) | ||
if err != nil { | ||
return err | ||
} | ||
defer destFile.Close() | ||
|
||
zipWriter := zip.NewWriter(destFile) | ||
defer zipWriter.Close() | ||
|
||
err = filepath.Walk(src, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
header, err := zip.FileInfoHeader(info) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
header.Name, err = filepath.Rel(src, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
header.Name += "/" | ||
} else { | ||
header.Method = zip.Deflate | ||
} | ||
|
||
writer, err := zipWriter.CreateHeader(header) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !info.IsDir() { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
_, err = io.Copy(writer, file) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func extract(src string, dest string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
zipReader, err := zip.OpenReader(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer zipReader.Close() | ||
|
||
for _, file := range zipReader.File { | ||
filePath := filepath.Join(dest, file.Name) | ||
|
||
if file.FileInfo().IsDir() { | ||
os.MkdirAll(filePath, 0755) | ||
continue | ||
} | ||
|
||
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil { | ||
return err | ||
} | ||
|
||
inFile, err := file.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer inFile.Close() | ||
|
||
outFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
defer outFile.Close() | ||
|
||
_, err = io.Copy(outFile, inFile) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice to explain why extra zipper/archiver is needed here and why mholt cannot be used here.