/
commandexecutor.go
141 lines (118 loc) · 3.14 KB
/
commandexecutor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package github
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/lucas-ingemar/packtrak/internal/shared"
"github.com/samber/lo"
)
type CommandExecutorFace interface {
ListInstalledPkgs(ctx context.Context, folderPath string) ([]shared.Package, error)
GetManifestPackages(ctx context.Context, packages []string) ([]shared.Package, error)
InstallPkg(ctx context.Context, pkg shared.Package, folderPath, binPath string) error
RemovePkg(ctx context.Context, pkg shared.Package, folderPath, binPath string) error
}
type commandExecutor struct {
GithubHttpFace
}
func (ce commandExecutor) ListInstalledPkgs(ctx context.Context, folderPath string) (packages []shared.Package, err error) {
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
return nil, nil
}
files, err := os.ReadDir(folderPath)
if err != nil {
return
}
for _, e := range files {
if e.IsDir() {
continue
}
pkg := file2Package(e.Name())
if pkg != nil {
packages = append(packages, *pkg)
}
}
return
}
func (ce commandExecutor) GetManifestPackages(ctx context.Context, packages []string) (pkgObjs []shared.Package, err error) {
// FIXME: Add Github Access Token ENV
errs := []error{}
lo.ForEach(packages, func(pkgFullName string, _ int) {
pkgFullName, err = sanitizeGithubUrl(pkgFullName)
if err != nil {
errs = append(errs, err)
return
}
user, repo, filePattern, err := url2pkgComponents(pkgFullName)
if err != nil {
errs = append(errs, err)
return
}
latestVersion, err := ce.GetLatestRelease(ctx, user, repo, filePattern)
if err != nil {
errs = append(errs, err)
return
}
pkgObjs = append(pkgObjs, shared.Package{
Name: fmt.Sprintf("%s/%s", user, repo),
FullName: pkgFullName,
LatestVersion: latestVersion,
})
})
return pkgObjs, errors.Join(errs...)
}
func (ce commandExecutor) InstallPkg(ctx context.Context, pkg shared.Package, folderPath, binPath string) error {
newFilename, err := ce.DownloadLatestRelease(ctx, pkg, folderPath)
if err != nil {
return err
}
err = os.Chmod(newFilename, 0755)
if err != nil {
return err
}
if binPath != "" {
_, symlinkName, _, err := url2pkgComponents(pkg.FullName)
if err != nil {
return err
}
symlinkPath := filepath.Join(binPath, strings.ToLower(symlinkName))
if _, err := os.Lstat(symlinkPath); err == nil {
os.Remove(symlinkPath)
}
if err = os.Symlink(newFilename, symlinkPath); err != nil {
return err
}
}
return nil
}
func (ce commandExecutor) RemovePkg(ctx context.Context, pkg shared.Package, folderPath, binPath string) error {
user, repo, _, err := url2pkgComponents(pkg.FullName)
if err != nil {
return err
}
files, err := filepath.Glob(filepath.Join(folderPath, fmt.Sprintf("%s.%s*", user, repo)))
if err != nil {
return err
}
for _, file := range files {
binary, err := os.Stat(file)
if err != nil {
return err
}
if binary.IsDir() {
return errors.New("not a file")
}
err = os.Remove(file)
if err != nil {
return err
}
}
symlinkPath := filepath.Join(binPath, strings.ToLower(repo))
if _, err := os.Lstat(symlinkPath); err == nil {
os.Remove(symlinkPath)
}
return nil
}