-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrun.go
More file actions
187 lines (156 loc) · 4.64 KB
/
run.go
File metadata and controls
187 lines (156 loc) · 4.64 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"time"
"github.com/urfave/cli/v3"
"github.com/zeebo/errs"
)
var (
errRunFailed = errs.Class("run failed")
)
func runCommand() *cli.Command {
return &cli.Command{
Name: "run",
Usage: "Run a specified binary from cache",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "transparent",
Usage: "Run the binary from PATH if found",
},
},
SkipFlagParsing: true,
Action: func(_ context.Context, c *cli.Command) error {
if c.NArg() == 0 {
return errRunFailed.New("no binary name provided for run command")
}
config, err := loadConfig()
if err != nil {
return errRunFailed.Wrap(err)
}
bEntry := stringToBinaryEntry(c.Args().First())
return runFromCache(config, bEntry, c.Args().Tail(), c.Bool("transparent"), nil)
},
}
}
func runFromCache(config *config, bEntry binaryEntry, args []string, transparentMode bool, env []string) error {
if transparentMode {
binaryPath, err := exec.LookPath(bEntry.Name)
if err == nil {
if verbosityLevel >= normalVerbosity {
fmt.Printf("Running '%s' from PATH...\n", bEntry.Name)
}
return runBinary(binaryPath, args, env)
}
}
cachedFile, err := isCached(config, bEntry)
if err == nil {
if verbosityLevel >= normalVerbosity {
fmt.Printf("Running '%s' from cache...\n", parseBinaryEntry(bEntry, true))
}
if err := runBinary(cachedFile, args, env); err != nil {
return errRunFailed.Wrap(err)
}
return cleanRunCache(config.CacheDir)
}
if verbosityLevel >= normalVerbosity {
fmt.Printf("Couldn't find '%s' in the cache. Fetching a new one...\n", parseBinaryEntry(bEntry, true))
}
cacheConfig := *config
cacheConfig.UseIntegrationHooks = false
cacheConfig.InstallDir = config.CacheDir
uRepoIndex, err := fetchRepoIndex(&cacheConfig)
if err != nil {
return errRunFailed.Wrap(err)
}
verbosityLevel = silentVerbosityWithErrors
if err := installBinaries(context.Background(), &cacheConfig, []binaryEntry{bEntry}, uRepoIndex); err != nil {
return errRunFailed.Wrap(err)
}
cachedFile, err = isCached(config, bEntry)
if err != nil {
return errRunFailed.New("failed to find binary after installation: %v", err)
}
if err := runBinary(cachedFile, args, env); err != nil {
return errRunFailed.Wrap(err)
}
return cleanRunCache(config.CacheDir)
}
func isCached(config *config, bEntry binaryEntry) (string, error) {
cachedFile := filepath.Join(config.CacheDir, filepath.Base(bEntry.Name))
if fileExists(cachedFile) && isExecutable(cachedFile) {
trackedBEntry, err := readEmbeddedBEntry(cachedFile)
if err == nil && (trackedBEntry.PkgID == bEntry.PkgID || bEntry.PkgID == "") {
return cachedFile, nil
}
fmt.Println(trackedBEntry)
}
return "", errRunFailed.New("binary '%s' not found in cache or does not match the requested version", bEntry.Name)
}
func runBinary(binaryPath string, args []string, env []string) error {
cmd := exec.Command(binaryPath, args...)
if env == nil {
cmd.Env = os.Environ()
} else {
cmd.Env = env
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil && verbosityLevel == extraVerbose {
fmt.Printf("The program (%s) errored out with a non-zero exit code (%d).\n", binaryPath, cmd.ProcessState.ExitCode())
}
return errRunFailed.Wrap(err)
}
func cleanRunCache(cacheDir string) error {
files, err := os.ReadDir(cacheDir)
if err != nil {
return errRunFailed.Wrap(err)
}
if len(files) <= maxCacheSize {
return nil
}
type fileWithAtime struct {
info os.DirEntry
atime time.Time
}
var filesWithAtime []fileWithAtime
for _, entry := range files {
// Skip files that start with "."
if strings.HasPrefix(entry.Name(), ".") {
continue
}
filePath := filepath.Join(cacheDir, entry.Name())
if !isExecutable(filePath) {
continue
}
fileInfo, err := os.Stat(filePath)
if err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "failed to read file info: %v\n", err)
}
continue
}
filesWithAtime = append(filesWithAtime, fileWithAtime{info: entry, atime: fileInfo.ModTime()})
}
sort.Slice(filesWithAtime, func(i, j int) bool {
return filesWithAtime[i].atime.Before(filesWithAtime[j].atime)
})
for i := 0; i < binariesToDelete && i < len(filesWithAtime); i++ {
filePath := filepath.Join(cacheDir, filesWithAtime[i].info.Name())
if err := os.Remove(filePath); err != nil {
if verbosityLevel >= silentVerbosityWithErrors {
fmt.Fprintf(os.Stderr, "error removing old cached binary: %v\n", err)
}
} else if verbosityLevel >= extraVerbose {
fmt.Printf("Removed old cached binary: %s\n", filePath)
}
}
return nil
}