Skip to content

Commit

Permalink
feat: support to install packages via scoop (#368)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Mar 2, 2023
1 parent 885acae commit eb7be78
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 32 deletions.
2 changes: 2 additions & 0 deletions pkg/exec/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
OSLinux = "linux"
// OSDarwin is the alias of Darwin
OSDarwin = "darwin"
// OSWindows is the alias of Windows
OSWindows = "windows"
)

// DefaultExecer is a wrapper for the OS exec
Expand Down
14 changes: 10 additions & 4 deletions pkg/os/apk/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package apk

import (
"fmt"

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

const (
// Tool is the tool name of apk
Tool = "apk"
)

// CommonInstaller is the installer of a common apk
type CommonInstaller struct {
Name string
Expand All @@ -13,22 +19,22 @@ type CommonInstaller struct {

// Available check if support current platform
func (d *CommonInstaller) Available() (ok bool) {
if d.Execer.OS() == "linux" {
_, err := d.Execer.LookPath("apk")
if d.Execer.OS() == exec.OSLinux {
_, err := d.Execer.LookPath(Tool)
ok = err == nil
}
return
}

// Install installs the target package
func (d *CommonInstaller) Install() (err error) {
err = d.Execer.RunCommand("apk", "add", d.Name)
err = d.Execer.RunCommand(Tool, "add", d.Name)
return
}

// Uninstall uninstalls the target package
func (d *CommonInstaller) Uninstall() (err error) {
err = d.Execer.RunCommand("apk", "del", d.Name)
err = d.Execer.RunCommand(Tool, "del", d.Name)
return
}

Expand Down
16 changes: 11 additions & 5 deletions pkg/os/apt/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package apt

import (
"fmt"

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

const (
// Tool is the tool name of apt-get
Tool = "apt-get"
)

// CommonInstaller is the installer of Conntrack in CentOS
type CommonInstaller struct {
Name string
Expand All @@ -13,24 +19,24 @@ type CommonInstaller struct {

// Available check if support current platform
func (d *CommonInstaller) Available() (ok bool) {
if d.Execer.OS() == "linux" {
_, err := d.Execer.LookPath("apt-get")
if d.Execer.OS() == exec.OSLinux {
_, err := d.Execer.LookPath(Tool)
ok = err == nil
}
return
}

// Install installs the Conntrack
func (d *CommonInstaller) Install() (err error) {
if err = d.Execer.RunCommand("apt-get", "update", "-y"); err == nil {
err = d.Execer.RunCommand("apt-get", "install", "-y", d.Name)
if err = d.Execer.RunCommand(Tool, "update", "-y"); err == nil {
err = d.Execer.RunCommand(Tool, "install", "-y", d.Name)
}
return
}

// Uninstall uninstalls the Conntrack
func (d *CommonInstaller) Uninstall() (err error) {
err = d.Execer.RunCommand("apt-get", "remove", "-y", d.Name)
err = d.Execer.RunCommand(Tool, "remove", "-y", d.Name)
return
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/os/brew/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package brew

import (
"fmt"

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

const (
// Tool is the tool name of brew
Tool = "brew"
)

// CommonInstaller is the installer of a common brew
type CommonInstaller struct {
Name string
Expand All @@ -13,24 +19,24 @@ type CommonInstaller struct {

// Available check if support current platform
func (d *CommonInstaller) Available() (ok bool) {
if d.Execer.OS() == "darwin" {
_, err := d.Execer.LookPath("brew")
if d.Execer.OS() == exec.OSDarwin || d.Execer.OS() == exec.OSLinux {
_, err := d.Execer.LookPath(Tool)
ok = err == nil
}
return
}

// Install installs the target package
func (d *CommonInstaller) Install() (err error) {
if err = d.Execer.RunCommand("brew", "install", d.Name); err != nil {
if err = d.Execer.RunCommand(Tool, "install", d.Name); err != nil {
return
}
return
}

// Uninstall uninstalls the Conntrack
func (d *CommonInstaller) Uninstall() (err error) {
err = d.Execer.RunCommand("brew", "remove", d.Name)
err = d.Execer.RunCommand(Tool, "remove", d.Name)
return
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/os/brew/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package brew

import (
"errors"
"testing"

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

func TestCommon(t *testing.T) {
Expand All @@ -28,7 +29,7 @@ func TestCommon(t *testing.T) {
}, {
name: "not is darwin",
installer: CommonInstaller{
Execer: exec.FakeExecer{ExpectOS: "linux"},
Execer: exec.FakeExecer{ExpectOS: exec.OSWindows},
},
expectAvailable: false,
hasErr: false,
Expand Down
9 changes: 5 additions & 4 deletions pkg/os/dnf/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dnf

import (
"fmt"

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

Expand All @@ -16,24 +17,24 @@ type CommonInstaller struct {

// Available check if support current platform
func (d *CommonInstaller) Available() (ok bool) {
if d.Execer.OS() == "linux" {
_, err := d.Execer.LookPath("dnf")
if d.Execer.OS() == exec.OSLinux {
_, err := d.Execer.LookPath(DNFName)
ok = err == nil
}
return
}

// Install installs the target package
func (d *CommonInstaller) Install() (err error) {
if err = d.Execer.RunCommand("dnf", "install", d.Name, "-y"); err != nil {
if err = d.Execer.RunCommand(DNFName, "install", d.Name, "-y"); err != nil {
return
}
return
}

// Uninstall uninstalls the target
func (d *CommonInstaller) Uninstall() (err error) {
err = d.Execer.RunCommand("dnf", "remove", d.Name, "-y")
err = d.Execer.RunCommand(DNFName, "remove", d.Name, "-y")
return
}

Expand Down
20 changes: 16 additions & 4 deletions pkg/os/generic_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (
"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/scoop"
"github.com/linuxsuren/http-downloader/pkg/os/snap"
"github.com/linuxsuren/http-downloader/pkg/os/winget"

"github.com/linuxsuren/http-downloader/pkg/exec"
"github.com/linuxsuren/http-downloader/pkg/os/apt"
Expand Down Expand Up @@ -157,22 +159,22 @@ func GenericInstallerRegistry(configFile string, registry core.InstallerRegistry
genericPackage.execer = defaultExecer

switch genericPackage.PackageManager {
case "apt-get":
case apt.Tool:
genericPackage.CommonInstaller = &apt.CommonInstaller{
Name: genericPackage.Name,
Execer: defaultExecer,
}
case "yum":
case yum.Tool:
genericPackage.CommonInstaller = &yum.CommonInstaller{
Name: genericPackage.Name,
Execer: defaultExecer,
}
case "brew":
case brew.Tool:
genericPackage.CommonInstaller = &brew.CommonInstaller{
Name: genericPackage.Name,
Execer: defaultExecer,
}
case "apk":
case apk.Tool:
genericPackage.CommonInstaller = &apk.CommonInstaller{
Name: genericPackage.Name,
Execer: defaultExecer,
Expand All @@ -193,6 +195,16 @@ func GenericInstallerRegistry(configFile string, registry core.InstallerRegistry
Name: genericPackage.Name,
Execer: defaultExecer,
}
case winget.Tool:
genericPackage.CommonInstaller = &winget.CommonInstaller{
Name: genericPackage.Name,
Execer: defaultExecer,
}
case scoop.Tool:
genericPackage.CommonInstaller = &scoop.CommonInstaller{
Name: genericPackage.Name,
Execer: defaultExecer,
}
default:
genericPackage.CommonInstaller = &generic.CommonInstaller{
Name: genericPackage.Name,
Expand Down
6 changes: 3 additions & 3 deletions pkg/os/npm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ type CommonInstaller struct {

// Available check if support current platform
func (d *CommonInstaller) Available() (ok bool) {
_, err := d.Execer.LookPath("npm")
_, err := d.Execer.LookPath(NPMName)
ok = err == nil
return
}

// Install installs the target package
func (d *CommonInstaller) Install() (err error) {
err = d.Execer.RunCommand("npm", "i", "-g", d.Name)
err = d.Execer.RunCommand(NPMName, "i", "-g", d.Name)
return
}

// Uninstall uninstalls the target package
func (d *CommonInstaller) Uninstall() (err error) {
err = d.Execer.RunCommand("npm", "uninstall", "-g", d.Name)
err = d.Execer.RunCommand(NPMName, "uninstall", "-g", d.Name)
return
}

Expand Down
57 changes: 57 additions & 0 deletions pkg/os/scoop/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package scoop

import (
"fmt"

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

// Tool is the tool name of this intergration
const Tool = "scoop"

// CommonInstaller is the installer of Conntrack in CentOS
type CommonInstaller struct {
Name string
Execer exec.Execer
}

// Available check if support current platform
func (d *CommonInstaller) Available() (ok bool) {
if d.Execer.OS() == exec.OSWindows {
_, err := d.Execer.LookPath(Tool)
ok = err == nil
}
return
}

// Install installs the Conntrack
func (d *CommonInstaller) Install() (err error) {
if err = d.Execer.RunCommand(Tool, "bucket", "add", "extras"); err == nil {
err = d.Execer.RunCommand(Tool, "install", d.Name)
}
return
}

// Uninstall uninstalls the Conntrack
func (d *CommonInstaller) Uninstall() (err error) {
err = d.Execer.RunCommand(Tool, "uninstall", d.Name)
return
}

// WaitForStart waits for the service be started
func (d *CommonInstaller) WaitForStart() (ok bool, err error) {
ok = true
return
}

// Start starts the Conntrack service
func (d *CommonInstaller) Start() error {
fmt.Println("not supported yet")
return nil
}

// Stop stops the Conntrack service
func (d *CommonInstaller) Stop() error {
fmt.Println("not supported yet")
return nil
}
2 changes: 2 additions & 0 deletions pkg/os/scoop/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package scoop implements the scoop integration
package scoop
3 changes: 2 additions & 1 deletion pkg/os/snap/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package snap

import (
"fmt"

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

Expand All @@ -17,7 +18,7 @@ type CommonInstaller struct {

// Available check if support current platform
func (d *CommonInstaller) Available() (ok bool) {
if d.Execer.OS() == "linux" {
if d.Execer.OS() == exec.OSLinux {
_, err := d.Execer.LookPath(SnapName)
ok = err == nil
}
Expand Down
Loading

0 comments on commit eb7be78

Please sign in to comment.