Skip to content

Commit 982a776

Browse files
authored
Merge pull request #532 from roots/migrate-archiver-package
Migrate to mholt/archives
2 parents 589defb + 2574258 commit 982a776

File tree

4 files changed

+309
-40
lines changed

4 files changed

+309
-40
lines changed

github/main.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -10,7 +11,7 @@ import (
1011
"strings"
1112
"time"
1213

13-
"github.com/mholt/archiver"
14+
"github.com/mholt/archives"
1415
)
1516

1617
var (
@@ -53,7 +54,19 @@ func DownloadRelease(repo string, version string, path string, dest string) (rel
5354
return nil, err
5455
}
5556

56-
if err := archiver.Unarchive(archivePath, path); err != nil {
57+
ctx := context.TODO()
58+
59+
archiveFile, err := os.Open(archivePath)
60+
if err != nil {
61+
return nil, fmt.Errorf("Error opening release archive: %v", err)
62+
}
63+
defer archiveFile.Close()
64+
65+
var format archives.Zip
66+
67+
if err := format.Extract(ctx, archiveFile, func(ctx context.Context, fi archives.FileInfo) error {
68+
return extractToDisk(fi, path)
69+
}); err != nil {
5770
return nil, fmt.Errorf("Error extracting the release archive: %v", err)
5871
}
5972

@@ -135,3 +148,28 @@ func DownloadFile(filepath string, url string, client *http.Client) error {
135148

136149
return nil
137150
}
151+
152+
func extractToDisk(fi archives.FileInfo, dest string) error {
153+
destPath := filepath.Join(dest, fi.NameInArchive)
154+
if fi.IsDir() {
155+
return os.MkdirAll(destPath, os.ModePerm)
156+
}
157+
158+
src, err := fi.Open()
159+
if err != nil {
160+
return err
161+
}
162+
defer src.Close()
163+
164+
dst, err := os.Create(destPath)
165+
if err != nil {
166+
return err
167+
}
168+
169+
_, err = io.Copy(dst, src)
170+
if err != nil {
171+
return err
172+
}
173+
174+
return os.Chmod(destPath, fi.Mode().Perm())
175+
}

github/main_test.go

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"net/http"
@@ -10,7 +11,7 @@ import (
1011
"testing"
1112

1213
"github.com/google/go-cmp/cmp"
13-
"github.com/mholt/archiver"
14+
"github.com/mholt/archives"
1415
)
1516

1617
func TestNewReleaseFromVersion(t *testing.T) {
@@ -37,11 +38,16 @@ func TestDownloadRelease(t *testing.T) {
3738
t.Error(err)
3839
}
3940

41+
_, err = os.Create(filepath.Join(dir, "test_file"))
42+
if err != nil {
43+
t.Error(err)
44+
}
45+
4046
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
4147
rw.Header().Set("Content-type", "application/octet-stream")
4248
rw.Header().Set("Content-Disposition", "attachment; filename='release.zip'")
4349

44-
err = createZipFile([]string{dir}, rw)
50+
err = createZipFile(filepath.Join(tmpDir, dir), rw)
4551
if err != nil {
4652
t.Error(err)
4753
}
@@ -54,7 +60,13 @@ func TestDownloadRelease(t *testing.T) {
5460

5561
const expectedVersion = "1.0.0"
5662

57-
release, err := DownloadRelease("roots/trellis", expectedVersion, tmpDir, filepath.Join(tmpDir, "test_release_dir"))
63+
destPath := filepath.Join(tmpDir, "test_release_dir")
64+
release, err := DownloadRelease("roots/trellis", expectedVersion, tmpDir, destPath)
65+
66+
expectedPath := filepath.Join(tmpDir, "test_release_dir", "test_file")
67+
if _, err := os.Stat(expectedPath); os.IsNotExist(err) {
68+
t.Errorf("expected extracted file %s to exist", expectedPath)
69+
}
5870

5971
if expectedVersion != release.Version {
6072
t.Errorf("expected version %s but got %s", expectedVersion, release.Version)
@@ -111,44 +123,21 @@ func TestFetechLatestRelease(t *testing.T) {
111123
}
112124
}
113125

114-
func createZipFile(files []string, writer io.Writer) error {
115-
zip := archiver.NewZip()
126+
func createZipFile(dir string, writer io.Writer) error {
127+
var format archives.Zip
128+
ctx := context.Background()
129+
130+
files, err := archives.FilesFromDisk(ctx, nil, map[string]string{
131+
dir: filepath.Base(dir),
132+
})
116133

117-
err := zip.Create(writer)
118134
if err != nil {
119135
return err
120136
}
121137

122-
defer zip.Close()
123-
124-
for _, fname := range files {
125-
info, err := os.Stat(fname)
126-
if err != nil {
127-
return err
128-
}
129-
130-
internalName, err := archiver.NameInArchive(info, fname, fname)
131-
if err != nil {
132-
return err
133-
}
134-
135-
file, err := os.Open(fname)
136-
if err != nil {
137-
return err
138-
}
139-
140-
err = zip.Write(archiver.File{
141-
FileInfo: archiver.FileInfo{
142-
FileInfo: info,
143-
CustomName: internalName,
144-
},
145-
ReadCloser: file,
146-
})
147-
148-
if err != nil {
149-
return err
150-
}
138+
err = format.Archive(ctx, writer, files)
139+
if err != nil {
140+
return err
151141
}
152-
153142
return nil
154143
}

go.mod

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,47 @@ require (
2727
github.com/Masterminds/goutils v1.1.1 // indirect
2828
github.com/Masterminds/semver/v3 v3.1.1 // indirect
2929
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
30+
github.com/STARRY-S/zip v0.2.1 // indirect
3031
github.com/alessio/shellescape v1.4.1 // indirect
32+
github.com/andybalholm/brotli v1.1.1 // indirect
3133
github.com/armon/go-radix v1.0.0 // indirect
3234
github.com/bgentry/speakeasy v0.1.0 // indirect
35+
github.com/bodgit/plumbing v1.3.0 // indirect
36+
github.com/bodgit/sevenzip v1.6.0 // indirect
37+
github.com/bodgit/windows v1.0.1 // indirect
3338
github.com/chzyer/readline v1.5.0 // indirect
3439
github.com/chzyer/test v1.0.0 // indirect
35-
github.com/dsnet/compress v0.0.1 // indirect
40+
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect
3641
github.com/golang/snappy v0.0.4 // indirect
3742
github.com/google/go-querystring v1.1.0 // indirect
3843
github.com/google/uuid v1.3.0 // indirect
3944
github.com/hashicorp/errwrap v1.1.0 // indirect
4045
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
4146
github.com/hashicorp/go-multierror v1.1.1 // indirect
4247
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
48+
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
4349
github.com/huandu/xstrings v1.3.2 // indirect
4450
github.com/imdario/mergo v0.3.13 // indirect
51+
github.com/klauspost/compress v1.17.11 // indirect
52+
github.com/klauspost/pgzip v1.2.6 // indirect
4553
github.com/mattn/go-colorable v0.1.13 // indirect
4654
github.com/mattn/go-runewidth v0.0.13 // indirect
55+
github.com/mholt/archives v0.1.0 // indirect
4756
github.com/mitchellh/copystructure v1.2.0 // indirect
4857
github.com/mitchellh/reflectwalk v1.0.2 // indirect
4958
github.com/nwaples/rardecode v1.1.3 // indirect
59+
github.com/nwaples/rardecode/v2 v2.0.0-beta.4.0.20241112120701-034e449c6e78 // indirect
5060
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
61+
github.com/pierrec/lz4/v4 v4.1.21 // indirect
5162
github.com/rivo/uniseg v0.2.0 // indirect
5263
github.com/rogpeppe/go-internal v1.8.1 // indirect
5364
github.com/shopspring/decimal v1.3.1 // indirect
65+
github.com/sorairolake/lzip-go v0.3.5 // indirect
5466
github.com/spf13/cast v1.5.0 // indirect
55-
github.com/ulikunitz/xz v0.5.10 // indirect
67+
github.com/therootcompany/xz v1.0.1 // indirect
68+
github.com/ulikunitz/xz v0.5.12 // indirect
5669
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
70+
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
5771
golang.org/x/net v0.27.0 // indirect
5872
golang.org/x/sys v0.29.0 // indirect
5973
golang.org/x/text v0.21.0 // indirect

0 commit comments

Comments
 (0)