forked from xplshn/dbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
157 lines (133 loc) · 4.36 KB
/
run.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"time"
"github.com/urfave/cli/v3"
)
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(ctx context.Context, c *cli.Command) error {
if c.NArg() == 0 {
return fmt.Errorf("no binary name provided for run command")
}
config, err := loadConfig()
if err != nil {
return err
}
bEntry := stringToBinaryEntry(c.Args().First())
return runFromCache(config, bEntry, c.Args().Tail(), c.Bool("transparent"), getVerbosityLevel(c))
},
}
}
func runFromCache(config *Config, bEntry binaryEntry, args []string, transparentMode bool, verbosityLevel Verbosity) error {
// Try running from PATH if transparent mode is enabled
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, verbosityLevel)
}
}
// Check if the binary exists in cache and matches the requested version
baseName := filepath.Base(bEntry.Name)
cachedFile := filepath.Join(config.CacheDir, baseName)
if fileExists(cachedFile) && isExecutable(cachedFile) {
trackedBEntry, err := readEmbeddedBEntry(cachedFile)
if err == nil && (trackedBEntry.PkgId == bEntry.PkgId || bEntry.PkgId == "") {
if verbosityLevel >= normalVerbosity {
fmt.Printf("Running '%s' from cache...\n", bEntry.Name)
}
if err := runBinary(cachedFile, args, verbosityLevel); err != nil {
return err
}
return cleanCache(config.CacheDir, verbosityLevel)
}
if verbosityLevel >= normalVerbosity {
fmt.Printf("Cached binary '%s' does not match requested binary '%s'. Fetching a new one...\n",
parseBinaryEntry(trackedBEntry, false), parseBinaryEntry(bEntry, false))
}
} else if verbosityLevel >= normalVerbosity {
fmt.Printf("Couldn't find '%s' in the cache. Fetching a new one...\n", bEntry.Name)
}
// Fetch and install the binary
cacheConfig := *config
cacheConfig.UseIntegrationHooks = false
cacheConfig.InstallDir = config.CacheDir
uRepoIndex := fetchRepoIndex(&cacheConfig)
if err := installBinaries(context.Background(), &cacheConfig, []binaryEntry{bEntry}, silentVerbosityWithErrors, uRepoIndex); err != nil {
return err
}
if err := runBinary(cachedFile, args, verbosityLevel); err != nil {
return err
}
return cleanCache(config.CacheDir, verbosityLevel)
}
func runBinary(binaryPath string, args []string, verbosityLevel Verbosity) error {
cmd := exec.Command(binaryPath, args...)
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 err
}
func cleanCache(cacheDir string, verbosityLevel Verbosity) error {
files, err := os.ReadDir(cacheDir)
if err != nil {
return fmt.Errorf("error reading cache directory, cannot proceed with cleanup: %v", err)
}
if len(files) <= maxCacheSize {
return nil
}
type fileWithAtime struct {
info os.DirEntry
atime time.Time
}
var filesWithAtime []fileWithAtime
for _, entry := range files {
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
}