Skip to content

Commit f2a055b

Browse files
committed
Fix run.go
Signed-off-by: xplshn <xplshn@murena.io>
1 parent edcf846 commit f2a055b

File tree

4 files changed

+45
-91
lines changed

4 files changed

+45
-91
lines changed

config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ type Hooks struct {
3131
}
3232

3333
type HookCommands struct {
34-
IntegrationCommands []string `yaml:"integration_commands"`
35-
DeintegrationCommands []string `yaml:"deintegration_commands"`
36-
IntegrationErrorMsg string `yaml:"integration_error_msg"`
37-
DeintegrationErrorMsg string `yaml:"deintegration_error_msg"`
38-
UseRunFromCache bool `yaml:"use_run_from_cache"`
34+
IntegrationCommands []string `yaml:"integrationCommands"`
35+
DeintegrationCommands []string `yaml:"deintegrationCommands"`
36+
IntegrationErrorMsg string `yaml:"integrationErrorMsg"`
37+
DeintegrationErrorMsg string `yaml:"deintegrationErrorMsg"`
38+
UseRunFromCache bool `yaml:"RunFromCache"`
3939
NoOp bool `yaml:"nop"`
4040
}
4141

@@ -59,7 +59,7 @@ func executeHookCommand(config *Config, cmdTemplate, bEntryPath, extension strin
5959
args := commandParts[1:]
6060

6161
if hookCommands.UseRunFromCache {
62-
return runFromCache(config, stringToBinaryEntry(command), args, true, verbosityLevel, uRepoIndex)
62+
return runFromCache(config, stringToBinaryEntry(command), args, true, verbosityLevel)
6363
}
6464

6565
cmdExec := exec.Command(command, args...)

findURL.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func findURL(config *Config, bEntries []binaryEntry, verbosityLevel Verbosity, u
108108
for _, e := range allErrors {
109109
errorMessages = append(errorMessages, e.Error())
110110
}
111-
return nil, nil, fmt.Errorf("error: no valid download URLs found for any of the requested binaries.\n%s\n", strings.Join(errorMessages, "\n"))
111+
return nil, nil, fmt.Errorf(ternary(len(bEntries) != 1, "error: no valid download URLs found for any of the requested binaries.\n%s\n", "%s\n"), strings.Join(errorMessages, "\n"))
112112
}
113113

114114
return foundURLs, foundB3sum, nil

run.go

Lines changed: 36 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"os/exec"
78
"path/filepath"
89
"sort"
910
"time"
10-
"context"
1111

1212
"github.com/urfave/cli/v3"
1313
)
1414

1515
func runCommand() *cli.Command {
1616
return &cli.Command{
17-
Name: "run",
18-
Usage: "Run a specified binary from cache",
17+
Name: "run",
18+
Usage: "Run a specified binary from cache",
1919
Flags: []cli.Flag{
2020
&cli.BoolFlag{
2121
Name: "transparent",
2222
Usage: "Run the binary from PATH if found",
2323
},
2424
},
25+
SkipFlagParsing: true,
2526
Action: func(ctx context.Context, c *cli.Command) error {
2627
if c.NArg() == 0 {
2728
return fmt.Errorf("no binary name provided for run command")
@@ -31,93 +32,60 @@ func runCommand() *cli.Command {
3132
if err != nil {
3233
return err
3334
}
34-
uRepoIndex := fetchRepoIndex(config)
35-
36-
// The first argument is the binary name
37-
binaryName := c.Args().First()
38-
bEntry := stringToBinaryEntry(binaryName)
39-
40-
// The rest of the arguments are passed to the binary
41-
args := c.Args().Tail()
42-
43-
return runFromCache(config, bEntry, args, c.Bool("transparent"), getVerbosityLevel(c), uRepoIndex)
35+
36+
bEntry := stringToBinaryEntry(c.Args().First())
37+
return runFromCache(config, bEntry, c.Args().Tail(), c.Bool("transparent"), getVerbosityLevel(c))
4438
},
4539
}
4640
}
4741

48-
func returnCachedFile(config *Config, binaryName string) (cachedBinary string, trackedBEntry binaryEntry, err error) {
49-
cachedBinary = filepath.Join(config.CacheDir, filepath.Base(binaryName))
50-
51-
trackedBEntry, err = readEmbeddedBEntry(cachedBinary)
52-
if err != nil {
53-
return "", trackedBEntry, err
54-
}
55-
56-
if !fileExists(cachedBinary) {
57-
return "", trackedBEntry, fmt.Errorf("cached binary not found")
58-
}
59-
60-
return cachedBinary, trackedBEntry, nil
61-
}
62-
63-
func runFromCache(config *Config, bEntry binaryEntry, args []string, transparentMode bool, verbosityLevel Verbosity, uRepoIndex []binaryEntry) error {
64-
binaryPath, err := exec.LookPath(bEntry.Name)
65-
if err == nil && transparentMode {
66-
if verbosityLevel >= normalVerbosity {
67-
fmt.Printf("Running '%s' from PATH...\n", bEntry.Name)
42+
func runFromCache(config *Config, bEntry binaryEntry, args []string, transparentMode bool, verbosityLevel Verbosity) error {
43+
// Try running from PATH if transparent mode is enabled
44+
if transparentMode {
45+
binaryPath, err := exec.LookPath(bEntry.Name)
46+
if err == nil {
47+
if verbosityLevel >= normalVerbosity {
48+
fmt.Printf("Running '%s' from PATH...\n", bEntry.Name)
49+
}
50+
return runBinary(binaryPath, args, verbosityLevel)
6851
}
69-
return runBinary(binaryPath, args, verbosityLevel)
7052
}
7153

54+
// Check if the binary exists in cache and matches the requested version
7255
baseName := filepath.Base(bEntry.Name)
7356
cachedFile := filepath.Join(config.CacheDir, baseName)
57+
7458
if fileExists(cachedFile) && isExecutable(cachedFile) {
7559
trackedBEntry, err := readEmbeddedBEntry(cachedFile)
76-
if err != nil || trackedBEntry.PkgId != bEntry.PkgId {
60+
if err == nil && (trackedBEntry.PkgId == bEntry.PkgId || bEntry.PkgId == "") {
7761
if verbosityLevel >= normalVerbosity {
78-
if trackedBEntry.Name != "" {
79-
fmt.Printf("Cached binary '%s' does not match requested binary '%s'. Fetching a new one...\n", parseBinaryEntry(trackedBEntry, false), parseBinaryEntry(bEntry, false))
80-
}
62+
fmt.Printf("Running '%s' from cache...\n", bEntry.Name)
8163
}
82-
83-
config.UseIntegrationHooks = false
84-
config.InstallDir = config.CacheDir
85-
if err := installBinaries(context.Background(), config, []binaryEntry{bEntry}, silentVerbosityWithErrors, uRepoIndex); err != nil {
86-
if verbosityLevel >= silentVerbosityWithErrors {
87-
fmt.Fprintf(os.Stderr, "Error: could not fetch and cache the binary: %v\n", err)
88-
}
89-
return err
90-
}
91-
92-
if err := runBinary(filepath.Join(config.CacheDir, baseName), args, verbosityLevel); err != nil {
64+
if err := runBinary(cachedFile, args, verbosityLevel); err != nil {
9365
return err
9466
}
9567
return cleanCache(config.CacheDir, verbosityLevel)
9668
}
97-
69+
9870
if verbosityLevel >= normalVerbosity {
99-
fmt.Printf("Running '%s' from cache...\n", bEntry.Name)
71+
fmt.Printf("Cached binary '%s' does not match requested binary '%s'. Fetching a new one...\n",
72+
parseBinaryEntry(trackedBEntry, false), parseBinaryEntry(bEntry, false))
10073
}
101-
if err := runBinary(filepath.Join(config.CacheDir, baseName), args, verbosityLevel); err != nil {
102-
return err
103-
}
104-
return cleanCache(config.CacheDir, verbosityLevel)
105-
}
106-
107-
if verbosityLevel >= normalVerbosity {
74+
} else if verbosityLevel >= normalVerbosity {
10875
fmt.Printf("Couldn't find '%s' in the cache. Fetching a new one...\n", bEntry.Name)
10976
}
11077

111-
config.UseIntegrationHooks = false
112-
config.InstallDir = config.CacheDir
113-
if err := installBinaries(context.Background(), config, []binaryEntry{bEntry}, silentVerbosityWithErrors, uRepoIndex); err != nil {
114-
if verbosityLevel >= silentVerbosityWithErrors {
115-
fmt.Fprintf(os.Stderr, "error: could not cache the binary: %v\n", err)
116-
}
78+
// Fetch and install the binary
79+
cacheConfig := *config
80+
cacheConfig.UseIntegrationHooks = false
81+
cacheConfig.InstallDir = config.CacheDir
82+
83+
uRepoIndex := fetchRepoIndex(&cacheConfig)
84+
if err := installBinaries(context.Background(), &cacheConfig, []binaryEntry{bEntry}, silentVerbosityWithErrors, uRepoIndex); err != nil {
11785
return err
11886
}
11987

120-
if err := runBinary(filepath.Join(config.CacheDir, baseName), args, verbosityLevel); err != nil {
88+
if err := runBinary(cachedFile, args, verbosityLevel); err != nil {
12189
return err
12290
}
12391
return cleanCache(config.CacheDir, verbosityLevel)
@@ -167,9 +135,7 @@ func cleanCache(cacheDir string, verbosityLevel Verbosity) error {
167135
continue
168136
}
169137

170-
atime := fileInfo.ModTime()
171-
172-
filesWithAtime = append(filesWithAtime, fileWithAtime{info: entry, atime: atime})
138+
filesWithAtime = append(filesWithAtime, fileWithAtime{info: entry, atime: fileInfo.ModTime()})
173139
}
174140

175141
sort.Slice(filesWithAtime, func(i, j int) bool {
@@ -182,10 +148,8 @@ func cleanCache(cacheDir string, verbosityLevel Verbosity) error {
182148
if verbosityLevel >= silentVerbosityWithErrors {
183149
fmt.Fprintf(os.Stderr, "error removing old cached binary: %v\n", err)
184150
}
185-
} else {
186-
if verbosityLevel >= extraVerbose {
187-
fmt.Printf("Removed old cached binary: %s\n", filePath)
188-
}
151+
} else if verbosityLevel >= extraVerbose {
152+
fmt.Printf("Removed old cached binary: %s\n", filePath)
189153
}
190154
}
191155

utility.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ func truncateSprintf(indicator, format string, a ...interface{}) string {
208208
return text
209209
}
210210

211-
width := getTerminalWidth() - len(indicator)
211+
width := uint(getTerminalWidth() - len(indicator))
212212
if width <= 0 {
213213
return text
214214
}
215215

216216
var out bytes.Buffer
217-
var visibleCount int
217+
var visibleCount uint
218218
var inEscape bool
219219
var escBuf bytes.Buffer
220220

@@ -418,16 +418,6 @@ func isSymlink(filePath string) bool {
418418
return err == nil && fileInfo.Mode()&os.ModeSymlink != 0
419419
}
420420

421-
func sanitizeString(input string) string {
422-
var sanitized strings.Builder
423-
for _, ch := range input {
424-
if ch >= 32 && ch <= 126 {
425-
sanitized.WriteRune(ch)
426-
}
427-
}
428-
return sanitized.String()
429-
}
430-
431421
func ternary[T any](cond bool, vtrue, vfalse T) T {
432422
if cond {
433423
return vtrue

0 commit comments

Comments
 (0)