1
1
package cmd
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"fmt"
6
7
"net/http"
@@ -9,6 +10,7 @@ import (
9
10
"path"
10
11
"runtime"
11
12
"strings"
13
+ "sync"
12
14
13
15
"github.com/linuxsuren/http-downloader/pkg"
14
16
"github.com/linuxsuren/http-downloader/pkg/installer"
@@ -20,9 +22,7 @@ import (
20
22
21
23
// newGetCmd return the get command
22
24
func newGetCmd (ctx context.Context ) (cmd * cobra.Command ) {
23
- opt := & downloadOption {
24
- RoundTripper : getRoundTripper (ctx ),
25
- }
25
+ opt := newDownloadOption (ctx )
26
26
cmd = & cobra.Command {
27
27
Use : "get" ,
28
28
Short : "download the file" ,
@@ -68,8 +68,18 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
68
68
return
69
69
}
70
70
71
+ func newDownloadOption (ctx context.Context ) * downloadOption {
72
+ return & downloadOption {
73
+ RoundTripper : getRoundTripper (ctx ),
74
+ fetcher : & installer.DefaultFetcher {},
75
+ wait : & sync.WaitGroup {},
76
+ }
77
+ }
78
+
71
79
type downloadOption struct {
72
80
searchOption
81
+ cancel context.CancelFunc
82
+ wait * sync.WaitGroup
73
83
74
84
URL string
75
85
Category string
@@ -94,12 +104,13 @@ type downloadOption struct {
94
104
PrintCategories bool
95
105
96
106
// inner fields
97
- name string
98
- Tar bool
99
- Package * installer.HDConfig
100
- org string
101
- repo string
102
- fetcher installer.Fetcher
107
+ name string
108
+ Tar bool
109
+ Package * installer.HDConfig
110
+ org string
111
+ repo string
112
+ fetcher installer.Fetcher
113
+ ExpectVersion string // should be like >v1.1.0
103
114
}
104
115
105
116
const (
@@ -111,14 +122,19 @@ const (
111
122
112
123
func (o * downloadOption ) fetch () (err error ) {
113
124
if ! o .Fetch {
125
+ o .wait .Add (1 )
126
+ go func () {
127
+ // no need to handle the error due to this is a background task
128
+ if o .fetcher != nil {
129
+ err = o .fetcher .FetchLatestRepo (o .Provider , installer .ConfigBranch , bytes .NewBuffer (nil ))
130
+ }
131
+ o .wait .Done ()
132
+ }()
114
133
return
115
134
}
116
135
117
136
// fetch the latest config
118
137
fmt .Println ("start to fetch the config" )
119
- if o .fetcher == nil {
120
- o .fetcher = & installer.DefaultFetcher {}
121
- }
122
138
if err = o .fetcher .FetchLatestRepo (o .Provider , installer .ConfigBranch , sysos .Stdout ); err != nil {
123
139
err = fmt .Errorf ("unable to fetch the latest config, error: %v" , err )
124
140
return
@@ -133,6 +149,11 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
133
149
return
134
150
}
135
151
152
+ if cmd .Context () != nil {
153
+ ctx , cancel := context .WithCancel (cmd .Context ())
154
+ o .cancel = cancel
155
+ o .fetcher .SetContext (ctx )
156
+ }
136
157
if err = o .fetch (); err != nil {
137
158
return
138
159
}
@@ -147,6 +168,7 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
147
168
}
148
169
149
170
targetURL := args [0 ]
171
+ o .Package = & installer.HDConfig {}
150
172
if ! strings .HasPrefix (targetURL , "http://" ) && ! strings .HasPrefix (targetURL , "https://" ) {
151
173
ins := & installer.Installer {
152
174
Provider : o .Provider ,
@@ -186,6 +208,13 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
186
208
}
187
209
188
210
func (o * downloadOption ) runE (cmd * cobra.Command , args []string ) (err error ) {
211
+ defer func () {
212
+ if o .cancel != nil {
213
+ o .cancel ()
214
+ o .wait .Wait ()
215
+ }
216
+ }()
217
+
189
218
// only print the schema for documentation
190
219
if o .PrintSchema {
191
220
var data []byte
0 commit comments