Skip to content

Commit 71c587a

Browse files
authored
feat: support proxy for the generic download (#316)
1 parent 36fd90e commit 71c587a

File tree

6 files changed

+132
-11
lines changed

6 files changed

+132
-11
lines changed

cmd/install.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,15 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
137137
}
138138
return
139139
}
140-
err = os.Install(args[0])
140+
141+
var proxy map[string]string
142+
if o.ProxyGitHub != "" {
143+
proxy = map[string]string{
144+
"raw.githubusercontent.com": fmt.Sprintf("%s/https://raw.githubusercontent.com", o.ProxyGitHub),
145+
"github.com": fmt.Sprintf("%s/https://github.com", o.ProxyGitHub),
146+
}
147+
}
148+
err = os.InstallWithProxy(args[0], proxy)
141149
return
142150
}
143151

pkg/os/core/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ type AdvanceInstaller interface {
2323
type InstallerRegistry interface {
2424
Registry(string, Installer)
2525
}
26+
27+
// ProxyAble define the proxy support feature
28+
type ProxyAble interface {
29+
SetURLReplace(map[string]string)
30+
}

pkg/os/generic/common.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package generic
22

33
import (
44
"fmt"
5-
"github.com/linuxsuren/http-downloader/pkg/exec"
65
"os"
76
"runtime"
7+
"strings"
88
"syscall"
9+
10+
"github.com/linuxsuren/http-downloader/pkg/exec"
911
)
1012

1113
// CommonInstaller is the installer of a common bash
@@ -14,6 +16,9 @@ type CommonInstaller struct {
1416
OS string
1517
InstallCmd CmdWithArgs
1618
UninstallCmd CmdWithArgs
19+
20+
// inner fields
21+
proxyMap map[string]string
1722
}
1823

1924
// CmdWithArgs is a command and with args
@@ -25,22 +30,53 @@ type CmdWithArgs struct {
2530

2631
// Run runs the current command
2732
func (c CmdWithArgs) Run() (err error) {
28-
fmt.Println(c.SystemCall)
33+
execer := exec.DefaultExecer{}
34+
2935
if c.SystemCall {
3036
var targetBinary string
31-
if targetBinary, err = exec.LookPath(c.Cmd); err != nil {
37+
if targetBinary, err = execer.LookPath(c.Cmd); err != nil {
3238
err = fmt.Errorf("cannot find %s", c.Cmd)
3339
} else {
3440
sysCallArgs := []string{c.Cmd}
3541
sysCallArgs = append(sysCallArgs, c.Args...)
42+
fmt.Println(c.Cmd, strings.Join(sysCallArgs, " "))
3643
err = syscall.Exec(targetBinary, sysCallArgs, os.Environ())
3744
}
3845
} else {
46+
fmt.Println(c.Cmd, strings.Join(c.Args, " "))
3947
err = exec.RunCommand(c.Cmd, c.Args...)
4048
}
4149
return
4250
}
4351

52+
// SetURLReplace set the URL replace map
53+
func (d *CommonInstaller) SetURLReplace(data map[string]string) {
54+
d.proxyMap = data
55+
}
56+
57+
func (d *CommonInstaller) sliceReplace(args []string) []string {
58+
for i, arg := range args {
59+
if result := d.urlReplace(arg); result != arg {
60+
args[i] = result
61+
}
62+
}
63+
return args
64+
}
65+
66+
func (d *CommonInstaller) urlReplace(old string) string {
67+
if d.proxyMap == nil {
68+
return old
69+
}
70+
71+
for k, v := range d.proxyMap {
72+
if !strings.Contains(old, k) {
73+
continue
74+
}
75+
old = strings.ReplaceAll(old, k, v)
76+
}
77+
return old
78+
}
79+
4480
// Available check if support current platform
4581
func (d *CommonInstaller) Available() (ok bool) {
4682
ok = d.OS == "" || runtime.GOOS == d.OS
@@ -49,12 +85,14 @@ func (d *CommonInstaller) Available() (ok bool) {
4985

5086
// Install installs the target package
5187
func (d *CommonInstaller) Install() (err error) {
88+
d.InstallCmd.Args = d.sliceReplace(d.InstallCmd.Args)
5289
err = d.InstallCmd.Run()
5390
return
5491
}
5592

5693
// Uninstall uninstalls the target package
5794
func (d *CommonInstaller) Uninstall() (err error) {
95+
d.InstallCmd.Args = d.sliceReplace(d.InstallCmd.Args)
5896
err = d.UninstallCmd.Run()
5997
return
6098
}

pkg/os/generic/common_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package generic
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSliceReplace(t *testing.T) {
10+
installer := &CommonInstaller{}
11+
installer.SetURLReplace(map[string]string{
12+
"https://raw.githubusercontent.com": "https://ghproxy.com/https://raw.githubusercontent.com",
13+
})
14+
15+
// a normal case
16+
result := installer.sliceReplace([]string{
17+
"abc",
18+
"https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh",
19+
})
20+
assert.Equal(t, []string{"abc",
21+
"https://ghproxy.com/https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"}, result)
22+
23+
// an empty slice
24+
noProxyInstaller := &CommonInstaller{}
25+
assert.Equal(t, []string{"abc"}, noProxyInstaller.sliceReplace([]string{"abc"}))
26+
}

pkg/os/generic_installer.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package os
22

33
import (
44
"fmt"
5+
"io/ioutil"
6+
"runtime"
7+
"strings"
8+
59
"github.com/linuxsuren/http-downloader/pkg/os/apk"
610
"github.com/linuxsuren/http-downloader/pkg/os/dnf"
711
"github.com/linuxsuren/http-downloader/pkg/os/npm"
812
"github.com/linuxsuren/http-downloader/pkg/os/snap"
9-
"io/ioutil"
10-
"runtime"
11-
"strings"
1213

1314
"github.com/linuxsuren/http-downloader/pkg/exec"
1415
"github.com/linuxsuren/http-downloader/pkg/os/apt"
@@ -43,6 +44,9 @@ type genericPackage struct {
4344
StopCmd CmdWithArgs `yaml:"stop"`
4445

4546
CommonInstaller core.Installer
47+
48+
// inner fields
49+
proxyMap map[string]string
4650
}
4751

4852
// CmdWithArgs is a command with arguments
@@ -146,13 +150,18 @@ func (i *genericPackage) Install() (err error) {
146150
}
147151

148152
if needInstall {
153+
preInstall.Cmd.Args = i.sliceReplace(preInstall.Cmd.Args)
154+
149155
if err = exec.RunCommand(preInstall.Cmd.Cmd, preInstall.Cmd.Args...); err != nil {
150156
return
151157
}
152158
}
153159
}
154160

155161
if i.CommonInstaller != nil {
162+
if proxyAble, ok := i.CommonInstaller.(core.ProxyAble); ok {
163+
proxyAble.SetURLReplace(i.proxyMap)
164+
}
156165
err = i.CommonInstaller.Install()
157166
} else {
158167
err = fmt.Errorf("not support yet")
@@ -179,3 +188,29 @@ func (i *genericPackage) Start() error {
179188
func (i *genericPackage) Stop() error {
180189
return nil
181190
}
191+
192+
// SetURLReplace set the URL replace map
193+
func (d *genericPackage) SetURLReplace(data map[string]string) {
194+
d.proxyMap = data
195+
}
196+
func (d *genericPackage) sliceReplace(args []string) []string {
197+
for i, arg := range args {
198+
if result := d.urlReplace(arg); result != arg {
199+
args[i] = result
200+
}
201+
}
202+
return args
203+
}
204+
func (d *genericPackage) urlReplace(old string) string {
205+
if d.proxyMap == nil {
206+
return old
207+
}
208+
209+
for k, v := range d.proxyMap {
210+
if !strings.Contains(old, k) {
211+
continue
212+
}
213+
old = strings.ReplaceAll(old, k, v)
214+
}
215+
return old
216+
}

pkg/os/installer.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package os
22

33
import (
44
"fmt"
5+
"path"
6+
"path/filepath"
7+
58
"github.com/linuxsuren/http-downloader/pkg/os/apt"
69
"github.com/linuxsuren/http-downloader/pkg/os/core"
710
"github.com/linuxsuren/http-downloader/pkg/os/docker"
811
"github.com/linuxsuren/http-downloader/pkg/os/yum"
912
"github.com/mitchellh/go-homedir"
10-
"path"
11-
"path/filepath"
1213
)
1314

1415
// DefaultInstallerRegistry is the default installer registry
@@ -64,12 +65,20 @@ func HasPackage(name string) bool {
6465
return false
6566
}
6667

67-
// Install installs a package with name
68-
func Install(name string) (err error) {
68+
func Install(name string) error {
69+
return InstallWithProxy(name, nil)
70+
}
71+
72+
// InstallWithProxy installs a package with name
73+
func InstallWithProxy(name string, proxy map[string]string) (err error) {
6974
var installer core.Installer
7075
if installers, ok := GetInstallers(name); ok {
7176
for _, installer = range installers {
7277
if installer.Available() {
78+
if proxyAble, ok := installer.(core.ProxyAble); ok {
79+
proxyAble.SetURLReplace(proxy)
80+
}
81+
7382
err = installer.Install()
7483
break
7584
}

0 commit comments

Comments
 (0)