Skip to content

Commit 026b4d6

Browse files
authored
Add platform support for hdConfig (#43)
1 parent 7b271cd commit 026b4d6

File tree

3 files changed

+120
-16
lines changed

3 files changed

+120
-16
lines changed

cmd/get.go

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package cmd
33
import (
44
"bytes"
55
"fmt"
6-
"github.com/ghodss/yaml"
76
"github.com/linuxsuren/http-downloader/pkg"
87
"github.com/mitchellh/go-homedir"
98
"github.com/spf13/cobra"
9+
"gopkg.in/yaml.v2"
1010
"io/ioutil"
1111
"net/url"
1212
"path"
@@ -44,6 +44,8 @@ func NewGetCmd() (cmd *cobra.Command) {
4444
flags.StringVarP(&opt.Provider, "provider", "", ProviderGitHub, "The file provider")
4545
flags.StringVarP(&opt.OS, "os", "", runtime.GOOS, "The OS of target binary file")
4646
flags.StringVarP(&opt.Arch, "arch", "", runtime.GOARCH, "The arch of target binary file")
47+
flags.BoolVarP(&opt.PrintSchema, "print-schema", "", false,
48+
"Print the schema of hdConfig if the flag is true without other function")
4749
return
4850
}
4951

@@ -61,8 +63,9 @@ type downloadOption struct {
6163
Arch string
6264
OS string
6365

64-
Thread int
65-
KeepPart bool
66+
Thread int
67+
KeepPart bool
68+
PrintSchema bool
6669

6770
// inner fields
6871
name string
@@ -75,6 +78,33 @@ const (
7578
ProviderGitHub = "github"
7679
)
7780

81+
func (o *downloadOption) isSupport(cfg hdConfig) bool {
82+
var osSupport, archSupport bool
83+
84+
if len(cfg.SupportOS) > 0 {
85+
for _, item := range cfg.SupportOS {
86+
if runtime.GOOS == item {
87+
osSupport = true
88+
break
89+
}
90+
}
91+
} else {
92+
osSupport = true
93+
}
94+
95+
if len(cfg.SupportArch) > 0 {
96+
for _, item := range cfg.SupportArch {
97+
if runtime.GOARCH == item {
98+
archSupport = true
99+
break
100+
}
101+
}
102+
} else {
103+
archSupport = true
104+
}
105+
return osSupport && archSupport
106+
}
107+
78108
func (o *downloadOption) providerURLParse(path string) (url string, err error) {
79109
url = path
80110
if o.Provider != ProviderGitHub {
@@ -127,6 +157,10 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
127157
var data []byte
128158
if data, err = ioutil.ReadFile(matchedFile); err == nil {
129159
cfg := hdConfig{}
160+
if !o.isSupport(cfg) {
161+
err = fmt.Errorf("not support this platform, os: %s, arch: %s", runtime.GOOS, runtime.GOARCH)
162+
return
163+
}
130164

131165
if err = yaml.Unmarshal(data, &cfg); err == nil {
132166
hdPkg := &hdPackage{
@@ -231,22 +265,24 @@ func renderCmdWithArgs(cmd *cmdWithArgs, hdPkg *hdPackage) (err error) {
231265
}
232266

233267
type hdConfig struct {
234-
Name string
235-
Filename string
236-
Binary string
237-
TargetBinary string
238-
URL string `yaml:"url"`
239-
Tar string
240-
Replacements map[string]string
241-
Installation *cmdWithArgs
242-
PreInstall *cmdWithArgs
243-
PostInstall *cmdWithArgs
244-
TestInstall *cmdWithArgs
268+
Name string `yaml:"name"`
269+
Filename string `yaml:"filename"`
270+
Binary string `yaml:"binary"`
271+
TargetBinary string `yaml:"targetBinary"`
272+
URL string `yaml:"url"`
273+
Tar string `yaml:"tar"`
274+
SupportOS []string `yaml:"supportOS"`
275+
SupportArch []string `yaml:"supportArch"`
276+
Replacements map[string]string `yaml:"replacements"`
277+
Installation *cmdWithArgs `yaml:"installation"`
278+
PreInstall *cmdWithArgs `yaml:"preInstall"`
279+
PostInstall *cmdWithArgs `yaml:"postInstall"`
280+
TestInstall *cmdWithArgs `yaml:"testInstall"`
245281
}
246282

247283
type cmdWithArgs struct {
248-
Cmd string
249-
Args []string
284+
Cmd string `yaml:"cmd"`
285+
Args []string `yaml:"args"`
250286
}
251287

252288
type hdPackage struct {
@@ -258,6 +294,11 @@ type hdPackage struct {
258294
}
259295

260296
func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error) {
297+
// this might not be the best way to print schema
298+
if o.PrintSchema {
299+
return
300+
}
301+
261302
o.Tar = true
262303
if len(args) <= 0 {
263304
return fmt.Errorf("no URL provided")
@@ -297,6 +338,20 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
297338
}
298339

299340
func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
341+
// only print the schema for documentation
342+
if o.PrintSchema {
343+
var data []byte
344+
if data, err = yaml.Marshal(hdConfig{
345+
Installation: &cmdWithArgs{},
346+
PreInstall: &cmdWithArgs{},
347+
PostInstall: &cmdWithArgs{},
348+
TestInstall: &cmdWithArgs{},
349+
}); err == nil {
350+
cmd.Print(string(data))
351+
}
352+
return
353+
}
354+
300355
if o.Thread <= 1 {
301356
err = pkg.DownloadWithContinue(o.URL, o.Output, o.ContinueAt, -1, 0, o.ShowProgress)
302357
} else {

cmd/get_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/magiconair/properties/assert"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
func TestIsSupport(t *testing.T) {
11+
table := []struct {
12+
cfg hdConfig
13+
expect bool
14+
message string
15+
}{{
16+
cfg: hdConfig{},
17+
expect: true,
18+
message: "support all os and arch",
19+
}, {
20+
cfg: hdConfig{
21+
SupportOS: []string{runtime.GOOS},
22+
SupportArch: []string{runtime.GOARCH},
23+
},
24+
expect: true,
25+
message: "",
26+
}, {
27+
cfg: hdConfig{
28+
SupportOS: []string{"fake"},
29+
SupportArch: []string{runtime.GOARCH},
30+
},
31+
expect: false,
32+
message: "not support os",
33+
}, {
34+
cfg: hdConfig{
35+
SupportOS: []string{runtime.GOOS},
36+
SupportArch: []string{"fake"},
37+
},
38+
expect: false,
39+
message: "not support arch",
40+
}}
41+
42+
for i, item := range table {
43+
opt := downloadOption{}
44+
45+
result := opt.isSupport(item.cfg)
46+
assert.Equal(t, result, item.expect, fmt.Sprintf("index: %d, %s", i, item.message))
47+
}
48+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ require (
1414
github.com/onsi/gomega v1.10.4
1515
github.com/spf13/cobra v1.1.3
1616
github.com/stretchr/testify v1.7.0
17+
gopkg.in/yaml.v2 v2.4.0
1718
)

0 commit comments

Comments
 (0)