11package main
22
33import (
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
1515func 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
0 commit comments