Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,15 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
}
return
}
err = os.Install(args[0])

var proxy map[string]string
if o.ProxyGitHub != "" {
proxy = map[string]string{
"raw.githubusercontent.com": fmt.Sprintf("%s/https://raw.githubusercontent.com", o.ProxyGitHub),
"github.com": fmt.Sprintf("%s/https://github.com", o.ProxyGitHub),
}
}
err = os.InstallWithProxy(args[0], proxy)
return
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/os/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ type AdvanceInstaller interface {
type InstallerRegistry interface {
Registry(string, Installer)
}

// ProxyAble define the proxy support feature
type ProxyAble interface {
SetURLReplace(map[string]string)
}
44 changes: 41 additions & 3 deletions pkg/os/generic/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package generic

import (
"fmt"
"github.com/linuxsuren/http-downloader/pkg/exec"
"os"
"runtime"
"strings"
"syscall"

"github.com/linuxsuren/http-downloader/pkg/exec"
)

// CommonInstaller is the installer of a common bash
Expand All @@ -14,6 +16,9 @@ type CommonInstaller struct {
OS string
InstallCmd CmdWithArgs
UninstallCmd CmdWithArgs

// inner fields
proxyMap map[string]string
}

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

// Run runs the current command
func (c CmdWithArgs) Run() (err error) {
fmt.Println(c.SystemCall)
execer := exec.DefaultExecer{}

if c.SystemCall {
var targetBinary string
if targetBinary, err = exec.LookPath(c.Cmd); err != nil {
if targetBinary, err = execer.LookPath(c.Cmd); err != nil {
err = fmt.Errorf("cannot find %s", c.Cmd)
} else {
sysCallArgs := []string{c.Cmd}
sysCallArgs = append(sysCallArgs, c.Args...)
fmt.Println(c.Cmd, strings.Join(sysCallArgs, " "))
err = syscall.Exec(targetBinary, sysCallArgs, os.Environ())
}
} else {
fmt.Println(c.Cmd, strings.Join(c.Args, " "))
err = exec.RunCommand(c.Cmd, c.Args...)
}
return
}

// SetURLReplace set the URL replace map
func (d *CommonInstaller) SetURLReplace(data map[string]string) {
d.proxyMap = data
}

func (d *CommonInstaller) sliceReplace(args []string) []string {
for i, arg := range args {
if result := d.urlReplace(arg); result != arg {
args[i] = result
}
}
return args
}

func (d *CommonInstaller) urlReplace(old string) string {
if d.proxyMap == nil {
return old
}

for k, v := range d.proxyMap {
if !strings.Contains(old, k) {
continue
}
old = strings.ReplaceAll(old, k, v)
}
return old
}

// Available check if support current platform
func (d *CommonInstaller) Available() (ok bool) {
ok = d.OS == "" || runtime.GOOS == d.OS
Expand All @@ -49,12 +85,14 @@ func (d *CommonInstaller) Available() (ok bool) {

// Install installs the target package
func (d *CommonInstaller) Install() (err error) {
d.InstallCmd.Args = d.sliceReplace(d.InstallCmd.Args)
err = d.InstallCmd.Run()
return
}

// Uninstall uninstalls the target package
func (d *CommonInstaller) Uninstall() (err error) {
d.InstallCmd.Args = d.sliceReplace(d.InstallCmd.Args)
err = d.UninstallCmd.Run()
return
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/os/generic/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package generic

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSliceReplace(t *testing.T) {
installer := &CommonInstaller{}
installer.SetURLReplace(map[string]string{
"https://raw.githubusercontent.com": "https://ghproxy.com/https://raw.githubusercontent.com",
})

// a normal case
result := installer.sliceReplace([]string{
"abc",
"https://github.com/raw/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh",
})
assert.Equal(t, []string{"abc",
"https://ghproxy.com/https://github.com/raw/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"}, result)

// an empty slice
noProxyInstaller := &CommonInstaller{}
assert.Equal(t, []string{"abc"}, noProxyInstaller.sliceReplace([]string{"abc"}))
}
41 changes: 38 additions & 3 deletions pkg/os/generic_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package os

import (
"fmt"
"io/ioutil"
"runtime"
"strings"

"github.com/linuxsuren/http-downloader/pkg/os/apk"
"github.com/linuxsuren/http-downloader/pkg/os/dnf"
"github.com/linuxsuren/http-downloader/pkg/os/npm"
"github.com/linuxsuren/http-downloader/pkg/os/snap"
"io/ioutil"
"runtime"
"strings"

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

CommonInstaller core.Installer

// inner fields
proxyMap map[string]string
}

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

if needInstall {
preInstall.Cmd.Args = i.sliceReplace(preInstall.Cmd.Args)

if err = exec.RunCommand(preInstall.Cmd.Cmd, preInstall.Cmd.Args...); err != nil {
return
}
}
}

if i.CommonInstaller != nil {
if proxyAble, ok := i.CommonInstaller.(core.ProxyAble); ok {
proxyAble.SetURLReplace(i.proxyMap)
}
err = i.CommonInstaller.Install()
} else {
err = fmt.Errorf("not support yet")
Expand All @@ -179,3 +188,29 @@ func (i *genericPackage) Start() error {
func (i *genericPackage) Stop() error {
return nil
}

// SetURLReplace set the URL replace map
func (d *genericPackage) SetURLReplace(data map[string]string) {
d.proxyMap = data
}
func (d *genericPackage) sliceReplace(args []string) []string {
for i, arg := range args {
if result := d.urlReplace(arg); result != arg {
args[i] = result
}
}
return args
}
func (d *genericPackage) urlReplace(old string) string {
if d.proxyMap == nil {
return old
}

for k, v := range d.proxyMap {
if !strings.Contains(old, k) {
continue
}
old = strings.ReplaceAll(old, k, v)
}
return old
}
17 changes: 13 additions & 4 deletions pkg/os/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package os

import (
"fmt"
"path"
"path/filepath"

"github.com/linuxsuren/http-downloader/pkg/os/apt"
"github.com/linuxsuren/http-downloader/pkg/os/core"
"github.com/linuxsuren/http-downloader/pkg/os/docker"
"github.com/linuxsuren/http-downloader/pkg/os/yum"
"github.com/mitchellh/go-homedir"
"path"
"path/filepath"
)

// DefaultInstallerRegistry is the default installer registry
Expand Down Expand Up @@ -64,12 +65,20 @@ func HasPackage(name string) bool {
return false
}

// Install installs a package with name
func Install(name string) (err error) {
func Install(name string) error {
return InstallWithProxy(name, nil)
}

// InstallWithProxy installs a package with name
func InstallWithProxy(name string, proxy map[string]string) (err error) {
var installer core.Installer
if installers, ok := GetInstallers(name); ok {
for _, installer = range installers {
if installer.Available() {
if proxyAble, ok := installer.(core.ProxyAble); ok {
proxyAble.SetURLReplace(proxy)
}

err = installer.Install()
break
}
Expand Down