Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add parameterize support to generic package #333

Merged
merged 1 commit into from
Jan 29, 2023
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
7 changes: 7 additions & 0 deletions pkg/os/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package fake

import (
"fmt"

"github.com/linuxsuren/http-downloader/pkg/os/core"
)

// Installer only for test purpose
type Installer struct {
hasError bool
support bool
data map[string]string
}

// NewFakeInstaller returns a Installer
Expand Down Expand Up @@ -64,3 +66,8 @@ func (d *Installer) Stop() (err error) {
}
return
}

// SetURLReplace is a fake method
func (d *Installer) SetURLReplace(data map[string]string) {
d.data = data
}
13 changes: 12 additions & 1 deletion pkg/os/fake/fake_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package fake

import (
"testing"

"github.com/linuxsuren/http-downloader/pkg/os/core"
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewFakeInstaller(t *testing.T) {
Expand All @@ -18,4 +19,14 @@ func TestNewFakeInstaller(t *testing.T) {
ok, err := installer.WaitForStart()
assert.True(t, ok)
assert.NotNil(t, err)

proxyAbleInstaller := &Installer{}
proxyAbleInstaller.SetURLReplace(map[string]string{
"key": "value",
})
assert.Equal(t, map[string]string{
"key": "value",
}, proxyAbleInstaller.data)

var _ core.ProxyAble = &Installer{}
}
36 changes: 30 additions & 6 deletions pkg/os/generic_installer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package os

import (
"bytes"
"fmt"
"github.com/linuxsuren/http-downloader/pkg"
"os"
"strings"
"text/template"

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

"github.com/linuxsuren/http-downloader/pkg/os/apk"
"github.com/linuxsuren/http-downloader/pkg/os/dnf"
Expand Down Expand Up @@ -47,6 +50,7 @@ type genericPackage struct {

// inner fields
proxyMap map[string]string
env map[string]string
execer exec.Execer
}

Expand All @@ -71,15 +75,16 @@ func parseGenericPackages(configFile string, genericPackages *genericPackages) (

// GenericInstallerRegistry registries a generic installer
func GenericInstallerRegistry(configFile string, registry core.InstallerRegistry) (err error) {
defaultExecer := exec.DefaultExecer{}
genericPackages := &genericPackages{}
if err = parseGenericPackages(configFile, genericPackages); err != nil {
return
}
defaultExecer := exec.DefaultExecer{}

// registry all the packages
for i := range genericPackages.Packages {
genericPackage := genericPackages.Packages[i]
genericPackage.execer = defaultExecer

switch genericPackage.PackageManager {
case "apt-get":
Expand Down Expand Up @@ -148,6 +153,7 @@ func (i *genericPackage) Available() (ok bool) {
return
}
func (i *genericPackage) Install() (err error) {
i.loadEnv()
for index := range i.PreInstall {
preInstall := i.PreInstall[index]

Expand All @@ -167,6 +173,7 @@ func (i *genericPackage) Install() (err error) {

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

if err = i.execer.RunCommand(preInstall.Cmd.Cmd, preInstall.Cmd.Args...); err != nil {
return
Expand Down Expand Up @@ -212,16 +219,19 @@ func (i *genericPackage) SetURLReplace(data map[string]string) {
func (i *genericPackage) sliceReplace(args []string) []string {
for index, arg := range args {
if result := i.urlReplace(arg); result != arg {
args[index] = result
args[index] = strings.TrimSpace(result)
}
}
return args
}
func (i *genericPackage) urlReplace(old string) string {
if i.proxyMap == nil {
return old
}
if tpl, err := template.New("env").Parse(old); err == nil {
buf := bytes.NewBuffer([]byte{})

if err = tpl.Execute(buf, i.env); err == nil {
old = buf.String()
}
}
for k, v := range i.proxyMap {
if !strings.Contains(old, k) {
continue
Expand All @@ -230,3 +240,17 @@ func (i *genericPackage) urlReplace(old string) string {
}
return old
}
func (i *genericPackage) loadEnv() {
if i.env == nil {
i.env = map[string]string{}
}
if i.execer.OS() == exec.OSLinux {
if data, readErr := os.ReadFile("/etc/os-release"); readErr == nil {
for _, line := range strings.Split(string(data), "\n") {
if pair := strings.Split(line, "="); len(pair) == 2 {
i.env[fmt.Sprintf("OS_%s", pair[0])] = strings.TrimPrefix(strings.TrimSuffix(pair[1], `"`), `"`)
}
}
}
}
}
80 changes: 80 additions & 0 deletions pkg/os/generic_installer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package os

import (
"errors"
"testing"

"github.com/linuxsuren/http-downloader/pkg/exec"
"github.com/linuxsuren/http-downloader/pkg/os/fake"
"github.com/stretchr/testify/assert"
)

func TestURLReplace(t *testing.T) {
genericPkg := &genericPackage{
env: map[string]string{
"key": "value",
},
execer: exec.FakeExecer{ExpectOS: exec.OSLinux},
}
genericPkg.SetURLReplace(map[string]string{
"github": "ghproxy",
})
genericPkg.loadEnv()
assert.Equal(t, "ghproxy-value", genericPkg.urlReplace("github-{{.key}}"))
assert.Equal(t, "value", genericPkg.urlReplace("{{.key}}"))
assert.Equal(t, []string{"value"}, genericPkg.sliceReplace([]string{"{{.key}}"}))

emptyGenericPkg := &genericPackage{
execer: exec.FakeExecer{ExpectOS: exec.OSLinux},
}
emptyGenericPkg.loadEnv()
assert.NotNil(t, emptyGenericPkg.env)
assert.Nil(t, emptyGenericPkg.Start())
assert.Nil(t, emptyGenericPkg.Stop())
assert.False(t, emptyGenericPkg.IsService())
assert.False(t, emptyGenericPkg.Available())
assert.NotNil(t, emptyGenericPkg.Install())
assert.NotNil(t, emptyGenericPkg.Uninstall())
ok, err := emptyGenericPkg.WaitForStart()
assert.True(t, ok)
assert.Nil(t, err)

withPreInstall := &genericPackage{
execer: exec.FakeExecer{
ExpectOS: exec.OSLinux,
},
PreInstall: []preInstall{{
Cmd: CmdWithArgs{
Cmd: "ls",
},
}, {
IssuePrefix: "good",
Cmd: CmdWithArgs{
Cmd: "ls",
},
}, {
IssuePrefix: "Ubuntu",
Cmd: CmdWithArgs{
Cmd: "ls",
},
}},
CommonInstaller: fake.NewFakeInstaller(true, false),
}
assert.Nil(t, withPreInstall.Install())

withErrorPreInstall := &genericPackage{
execer: exec.FakeExecer{
ExpectOS: exec.OSLinux,
ExpectError: errors.New("error"),
},
PreInstall: []preInstall{{
Cmd: CmdWithArgs{
Cmd: "ls",
},
}},
CommonInstaller: fake.NewFakeInstaller(true, true),
}
assert.NotNil(t, withErrorPreInstall.Install())
assert.NotNil(t, withErrorPreInstall.Uninstall())
assert.True(t, withErrorPreInstall.Available())
}