@@ -3,10 +3,10 @@ package cmd
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
- "github.com/ghodss/yaml"
7
6
"github.com/linuxsuren/http-downloader/pkg"
8
7
"github.com/mitchellh/go-homedir"
9
8
"github.com/spf13/cobra"
9
+ "gopkg.in/yaml.v2"
10
10
"io/ioutil"
11
11
"net/url"
12
12
"path"
@@ -44,6 +44,8 @@ func NewGetCmd() (cmd *cobra.Command) {
44
44
flags .StringVarP (& opt .Provider , "provider" , "" , ProviderGitHub , "The file provider" )
45
45
flags .StringVarP (& opt .OS , "os" , "" , runtime .GOOS , "The OS of target binary file" )
46
46
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" )
47
49
return
48
50
}
49
51
@@ -61,8 +63,9 @@ type downloadOption struct {
61
63
Arch string
62
64
OS string
63
65
64
- Thread int
65
- KeepPart bool
66
+ Thread int
67
+ KeepPart bool
68
+ PrintSchema bool
66
69
67
70
// inner fields
68
71
name string
@@ -75,6 +78,33 @@ const (
75
78
ProviderGitHub = "github"
76
79
)
77
80
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
+
78
108
func (o * downloadOption ) providerURLParse (path string ) (url string , err error ) {
79
109
url = path
80
110
if o .Provider != ProviderGitHub {
@@ -127,6 +157,10 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
127
157
var data []byte
128
158
if data , err = ioutil .ReadFile (matchedFile ); err == nil {
129
159
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
+ }
130
164
131
165
if err = yaml .Unmarshal (data , & cfg ); err == nil {
132
166
hdPkg := & hdPackage {
@@ -231,22 +265,24 @@ func renderCmdWithArgs(cmd *cmdWithArgs, hdPkg *hdPackage) (err error) {
231
265
}
232
266
233
267
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"`
245
281
}
246
282
247
283
type cmdWithArgs struct {
248
- Cmd string
249
- Args []string
284
+ Cmd string `yaml:"cmd"`
285
+ Args []string `yaml:"args"`
250
286
}
251
287
252
288
type hdPackage struct {
@@ -258,6 +294,11 @@ type hdPackage struct {
258
294
}
259
295
260
296
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
+
261
302
o .Tar = true
262
303
if len (args ) <= 0 {
263
304
return fmt .Errorf ("no URL provided" )
@@ -297,6 +338,20 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
297
338
}
298
339
299
340
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
+
300
355
if o .Thread <= 1 {
301
356
err = pkg .DownloadWithContinue (o .URL , o .Output , o .ContinueAt , - 1 , 0 , o .ShowProgress )
302
357
} else {
0 commit comments