@@ -2,10 +2,12 @@ package generic
2
2
3
3
import (
4
4
"fmt"
5
- "github.com/linuxsuren/http-downloader/pkg/exec"
6
5
"os"
7
6
"runtime"
7
+ "strings"
8
8
"syscall"
9
+
10
+ "github.com/linuxsuren/http-downloader/pkg/exec"
9
11
)
10
12
11
13
// CommonInstaller is the installer of a common bash
@@ -14,6 +16,9 @@ type CommonInstaller struct {
14
16
OS string
15
17
InstallCmd CmdWithArgs
16
18
UninstallCmd CmdWithArgs
19
+
20
+ // inner fields
21
+ proxyMap map [string ]string
17
22
}
18
23
19
24
// CmdWithArgs is a command and with args
@@ -25,22 +30,53 @@ type CmdWithArgs struct {
25
30
26
31
// Run runs the current command
27
32
func (c CmdWithArgs ) Run () (err error ) {
28
- fmt .Println (c .SystemCall )
33
+ execer := exec.DefaultExecer {}
34
+
29
35
if c .SystemCall {
30
36
var targetBinary string
31
- if targetBinary , err = exec .LookPath (c .Cmd ); err != nil {
37
+ if targetBinary , err = execer .LookPath (c .Cmd ); err != nil {
32
38
err = fmt .Errorf ("cannot find %s" , c .Cmd )
33
39
} else {
34
40
sysCallArgs := []string {c .Cmd }
35
41
sysCallArgs = append (sysCallArgs , c .Args ... )
42
+ fmt .Println (c .Cmd , strings .Join (sysCallArgs , " " ))
36
43
err = syscall .Exec (targetBinary , sysCallArgs , os .Environ ())
37
44
}
38
45
} else {
46
+ fmt .Println (c .Cmd , strings .Join (c .Args , " " ))
39
47
err = exec .RunCommand (c .Cmd , c .Args ... )
40
48
}
41
49
return
42
50
}
43
51
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
+
44
80
// Available check if support current platform
45
81
func (d * CommonInstaller ) Available () (ok bool ) {
46
82
ok = d .OS == "" || runtime .GOOS == d .OS
@@ -49,12 +85,14 @@ func (d *CommonInstaller) Available() (ok bool) {
49
85
50
86
// Install installs the target package
51
87
func (d * CommonInstaller ) Install () (err error ) {
88
+ d .InstallCmd .Args = d .sliceReplace (d .InstallCmd .Args )
52
89
err = d .InstallCmd .Run ()
53
90
return
54
91
}
55
92
56
93
// Uninstall uninstalls the target package
57
94
func (d * CommonInstaller ) Uninstall () (err error ) {
95
+ d .InstallCmd .Args = d .sliceReplace (d .InstallCmd .Args )
58
96
err = d .UninstallCmd .Run ()
59
97
return
60
98
}
0 commit comments