Skip to content

Commit 46bca63

Browse files
authored
Add bz2 compress support (#179)
1 parent 992a572 commit 46bca63

File tree

5 files changed

+100
-16
lines changed

5 files changed

+100
-16
lines changed

cmd/install.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ func (o *installOption) installFromSource() (err error) {
187187
return
188188
}
189189

190-
191-
192190
func (o *installOption) buildGoSource() (binaryPath string, err error) {
193191
gopath := sysos.Getenv("GOPATH")
194192
if gopath == "" {

pkg/compress/bzip2.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package compress
2+
3+
import (
4+
"compress/bzip2"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"os"
9+
"path/filepath"
10+
)
11+
12+
// Bzip2 implements a compress which based is based on bzip2
13+
type Bzip2 struct {
14+
additionBinaries []string
15+
}
16+
17+
// NewBzip2 creates an instance of Bzip2
18+
func NewBzip2(additionBinaries []string) *Bzip2 {
19+
return &Bzip2{additionBinaries: additionBinaries}
20+
}
21+
22+
// make sure Bzip2 implements the interface Compress
23+
var _ Compress = &Bzip2{}
24+
25+
// ExtractFiles extracts files from a target compress file
26+
func (x *Bzip2) ExtractFiles(sourceFile, targetName string) (err error) {
27+
if targetName == "" {
28+
err = errors.New("target filename is empty")
29+
return
30+
}
31+
var f *os.File
32+
if f, err = os.Open(sourceFile); err != nil {
33+
return err
34+
}
35+
defer func() {
36+
_ = f.Close()
37+
}()
38+
39+
// Create a Bzip2 Reader
40+
r := bzip2.NewReader(f)
41+
42+
var targetFile *os.File
43+
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(sourceFile), targetName),
44+
os.O_CREATE|os.O_RDWR, 0744); err != nil {
45+
return
46+
}
47+
if _, err = io.Copy(targetFile, r); err != nil {
48+
return
49+
}
50+
return
51+
}

pkg/compress/types.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ func GetCompressor(extension string, additionBinaries []string) Compress {
3838
return NewXz(additionBinaries)
3939
case ".zip":
4040
return NewZip(additionBinaries)
41-
default:
41+
case ".gz", ".tar.gz":
4242
return NewGZip(additionBinaries)
43+
case ".bz2":
44+
return NewBzip2(additionBinaries)
4345
}
46+
return nil
47+
}
48+
49+
// IsSupport checks if the desired file extension
50+
func IsSupport(extension string) bool {
51+
return GetCompressor(extension, nil) != nil
4452
}

pkg/compress/types_test.go

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

33
import (
4+
"path"
45
"reflect"
56
"testing"
67
)
@@ -17,7 +18,7 @@ func TestGetCompressor(t *testing.T) {
1718
}{{
1819
name: "unknown type",
1920
args: args{extension: ".xdf"},
20-
want: NewGZip(nil),
21+
want: nil,
2122
}, {
2223
name: ".zip",
2324
args: args{extension: ".zip"},
@@ -26,6 +27,10 @@ func TestGetCompressor(t *testing.T) {
2627
name: ".xz",
2728
args: args{extension: ".xz"},
2829
want: NewXz(nil),
30+
}, {
31+
name: ".tar.gz",
32+
args: args{extension: ".tar.gz"},
33+
want: NewGZip(nil),
2934
}}
3035
for _, tt := range tests {
3136
t.Run(tt.name, func(t *testing.T) {
@@ -35,3 +40,33 @@ func TestGetCompressor(t *testing.T) {
3540
})
3641
}
3742
}
43+
44+
func TestIsSupport(t *testing.T) {
45+
type args struct {
46+
extension string
47+
}
48+
tests := []struct {
49+
name string
50+
args args
51+
want bool
52+
}{{
53+
name: "supported extension: .tar.gz",
54+
args: args{
55+
extension: path.Ext("test.tar.gz"),
56+
},
57+
want: true,
58+
}, {
59+
name: "not supported extension: .ab",
60+
args: args{
61+
extension: path.Ext("test.ab"),
62+
},
63+
want: false,
64+
}}
65+
for _, tt := range tests {
66+
t.Run(tt.name, func(t *testing.T) {
67+
if got := IsSupport(tt.args.extension); got != tt.want {
68+
t.Errorf("IsSupport() = %v, want %v", got, tt.want)
69+
}
70+
})
71+
}
72+
}

pkg/installer/check.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/AlecAivazis/survey/v2"
77
"github.com/linuxsuren/http-downloader/pkg"
88
"github.com/linuxsuren/http-downloader/pkg/common"
9+
"github.com/linuxsuren/http-downloader/pkg/compress"
910
"github.com/linuxsuren/http-downloader/pkg/exec"
1011
"github.com/linuxsuren/http-downloader/pkg/net"
1112
"github.com/linuxsuren/http-downloader/pkg/os"
@@ -27,12 +28,6 @@ const (
2728
ProviderGitHub = "github"
2829
)
2930

30-
var supportedPackages = []string{
31-
"tar.gz",
32-
"zip",
33-
"tar.xz",
34-
}
35-
3631
// CheckDepAndInstall checks the desired tools, install the missing packages
3732
func (o *Installer) CheckDepAndInstall(tools map[string]string) (err error) {
3833
for tool, formula := range tools {
@@ -271,6 +266,8 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag
271266
}
272267
o.Name = cfg.Binary
273268
}
269+
} else {
270+
err = fmt.Errorf("failed to parse YAML file: %s, error: %v", matchedFile, err)
274271
}
275272
}
276273
}
@@ -393,10 +390,5 @@ func getPackagingFormat(installer *Installer) string {
393390
}
394391

395392
func hasPackageSuffix(packageURL string) bool {
396-
for i := 0; i < len(supportedPackages); i++ {
397-
if strings.HasSuffix(packageURL, supportedPackages[i]) {
398-
return true
399-
}
400-
}
401-
return false
393+
return compress.IsSupport(path.Ext(packageURL))
402394
}

0 commit comments

Comments
 (0)