Skip to content

Commit f2fbde4

Browse files
authored
Add snap packagemanger support (#262)
1 parent e2c1da7 commit f2fbde4

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

cmd/install.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ func newInstallCmd(ctx context.Context) (cmd *cobra.Command) {
2424
},
2525
}
2626
cmd = &cobra.Command{
27-
Use: "install",
28-
Short: "Install a package from https://github.com/LinuxSuRen/hd-home",
27+
Use: "install",
28+
Aliases: []string{"i", "add"},
29+
Short: "Install a package from https://github.com/LinuxSuRen/hd-home",
2930
Long: `Install a package from https://github.com/LinuxSuRen/hd-home
3031
Cannot find your desired package? Please run command: hd fetch --reset, then try it again`,
3132
Example: "hd install goget",

cmd/search.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ func newSearchCmd(context.Context) (cmd *cobra.Command) {
1414
opt := &searchOption{}
1515

1616
cmd = &cobra.Command{
17-
Use: "search",
18-
Short: "Search packages from the hd config repo",
19-
Args: cobra.MinimumNArgs(1),
20-
RunE: opt.runE,
17+
Use: "search",
18+
Aliases: []string{"s", "find", "f"},
19+
Short: "Search packages from the hd config repo",
20+
Args: cobra.MinimumNArgs(1),
21+
RunE: opt.runE,
2122
}
2223
opt.addFlags(cmd.Flags())
2324
return

pkg/os/generic_installer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package os
33
import (
44
"fmt"
55
"github.com/linuxsuren/http-downloader/pkg/os/apk"
6+
"github.com/linuxsuren/http-downloader/pkg/os/snap"
67
"io/ioutil"
78
"runtime"
89
"strings"
@@ -82,6 +83,11 @@ func GenericInstallerRegistry(configFile string, registry core.InstallerRegistry
8283
genericPackage.CommonInstaller = &brew.CommonInstaller{Name: genericPackage.Name}
8384
case "apk":
8485
genericPackage.CommonInstaller = &apk.CommonInstaller{Name: genericPackage.Name}
86+
case snap.SnapName:
87+
genericPackage.CommonInstaller = &snap.CommonInstaller{
88+
Name: genericPackage.Name,
89+
Args: genericPackage.InstallCmd.Args,
90+
}
8591
default:
8692
genericPackage.CommonInstaller = &generic.CommonInstaller{
8793
Name: genericPackage.Name,

pkg/os/snap/common.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package snap
2+
3+
import (
4+
"fmt"
5+
"github.com/linuxsuren/http-downloader/pkg/exec"
6+
"runtime"
7+
)
8+
9+
// SnapName is the name of the snap
10+
const SnapName = "snap"
11+
12+
// CommonInstaller is the installer of a common snap
13+
type CommonInstaller struct {
14+
Name string
15+
Args []string `yaml:"args"`
16+
}
17+
18+
// Available check if support current platform
19+
func (d *CommonInstaller) Available() (ok bool) {
20+
if runtime.GOOS == "linux" {
21+
_, err := exec.LookPath(SnapName)
22+
ok = err == nil
23+
}
24+
return
25+
}
26+
27+
// Install installs the target package
28+
func (d *CommonInstaller) Install() (err error) {
29+
args := []string{"install", d.Name}
30+
args = append(args, d.Args...)
31+
if err = exec.RunCommand(SnapName, args...); err != nil {
32+
return
33+
}
34+
return
35+
}
36+
37+
// Uninstall uninstalls the target package
38+
func (d *CommonInstaller) Uninstall() (err error) {
39+
err = exec.RunCommand(SnapName, "remove", d.Name)
40+
return
41+
}
42+
43+
// WaitForStart waits for the service be started
44+
func (d *CommonInstaller) WaitForStart() (ok bool, err error) {
45+
ok = true
46+
return
47+
}
48+
49+
// Start starts the target service
50+
func (d *CommonInstaller) Start() error {
51+
fmt.Println("not supported yet")
52+
return nil
53+
}
54+
55+
// Stop stops the target service
56+
func (d *CommonInstaller) Stop() error {
57+
fmt.Println("not supported yet")
58+
return nil
59+
}

0 commit comments

Comments
 (0)