Skip to content

Commit 20a02e6

Browse files
cfergeaupraveenkumar
authored andcommitted
cache: Pass full path to Cache.New()
The Cache package is a bit messy at the moment, as it handles both the installer use-case, where we don't want to download anything, and the 'built from git' use-case, where it will automatically download files. The difference between the 2 can be found in `EnsureIsCached()` which will always return an error if IsInstaller() is true, and which will download the binary if not. The 'New()' interface is influenced by the 'download missing binary' use-case, as we pass it an executable name, and then the code will turn it into a full path by prepending constants.BinDir() when needed. This in particular means that all the binaries must be in the same directory. This does not have to be true when using an installer. This commit reworks this by allowing to pass a full path instead of just the binary name.
1 parent e96b3bb commit 20a02e6

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

pkg/crc/cache/cache.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
type Cache struct {
20-
executableName string
20+
executablePath string
2121
archiveURL string
2222
destDir string
2323
version string
@@ -34,16 +34,16 @@ func (e *VersionMismatchError) Error() string {
3434
return fmt.Sprintf("%s version mismatch: %s expected but %s found in the cache", e.ExecutableName, e.ExpectedVersion, e.CurrentVersion)
3535
}
3636

37-
func New(executableName string, archiveURL string, version string, getVersion func(string) (string, error)) *Cache {
38-
return &Cache{executableName: executableName, archiveURL: archiveURL, destDir: constants.BinDir(), version: version, getVersion: getVersion}
37+
func New(executablePath string, archiveURL string, version string, getVersion func(string) (string, error)) *Cache {
38+
return &Cache{executablePath: executablePath, archiveURL: archiveURL, destDir: constants.BinDir(), version: version, getVersion: getVersion}
3939
}
4040

4141
func (c *Cache) GetExecutablePath() string {
42-
return filepath.Join(c.destDir, c.executableName)
42+
return c.executablePath
4343
}
4444

4545
func (c *Cache) GetExecutableName() string {
46-
return c.executableName
46+
return filepath.Base(c.executablePath)
4747
}
4848

4949
/* getVersionGeneric runs the cached executable with 'args', and assumes the version string
@@ -69,7 +69,7 @@ func getVersionGeneric(executablePath string, args ...string) (string, error) {
6969
func NewAdminHelperCache() *Cache {
7070
url := constants.GetAdminHelperURL()
7171
version := version.GetAdminHelperVersion()
72-
return New(constants.GetAdminHelperExecutable(),
72+
return New(constants.AdminHelperPath(),
7373
url,
7474
version,
7575
func(executable string) (string, error) {
@@ -122,14 +122,14 @@ func (c *Cache) CacheExecutable() error {
122122
if IsTarball(assetTmpFile) {
123123
// Extract the tarball and put it the cache directory.
124124
extractedFiles, err = extract.UncompressWithFilter(assetTmpFile, tmpDir, false,
125-
func(filename string) bool { return filepath.Base(filename) == c.executableName })
125+
func(filename string) bool { return filepath.Base(filename) == c.GetExecutableName() })
126126
if err != nil {
127127
return errors.Wrapf(err, "Cannot uncompress '%s'", assetTmpFile)
128128
}
129129
} else {
130130
extractedFiles = append(extractedFiles, assetTmpFile)
131-
if filepath.Base(assetTmpFile) != c.executableName {
132-
logging.Warnf("Executable name is %s but extracted file name is %s", c.executableName, filepath.Base(assetTmpFile))
131+
if filepath.Base(assetTmpFile) != c.GetExecutableName() {
132+
logging.Warnf("Executable name is %s but extracted file name is %s", c.GetExecutableName(), filepath.Base(assetTmpFile))
133133
}
134134
}
135135

@@ -153,7 +153,7 @@ func (c *Cache) CacheExecutable() error {
153153
}
154154

155155
func (c *Cache) getExecutable(destDir string) (string, error) {
156-
logging.Debugf("Trying to extract %s from crc executable", c.executableName)
156+
logging.Debugf("Trying to extract %s from crc executable", c.GetExecutableName())
157157
archiveName := filepath.Base(c.archiveURL)
158158
destPath := filepath.Join(destDir, archiveName)
159159
err := embed.Extract(archiveName, destPath)
@@ -175,14 +175,14 @@ func (c *Cache) CheckVersion() error {
175175
}
176176
if currentVersion != c.version {
177177
err := &VersionMismatchError{
178-
ExecutableName: c.executableName,
178+
ExecutableName: c.GetExecutableName(),
179179
CurrentVersion: currentVersion,
180180
ExpectedVersion: c.version,
181181
}
182182
logging.Debugf("%s", err.Error())
183183
return err
184184
}
185-
logging.Debugf("Found %s version %s", c.executableName, c.version)
185+
logging.Debugf("Found %s version %s", c.GetExecutableName(), c.version)
186186
return nil
187187
}
188188

pkg/crc/cache/cache_darwin.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package cache
22

33
import (
4-
"path/filepath"
5-
64
"github.com/crc-org/crc/pkg/crc/machine/vfkit"
75
)
86

97
func NewVfkitCache() *Cache {
10-
return New(filepath.Base(vfkit.ExecutablePath()), vfkit.VfkitDownloadURL, vfkit.VfkitVersion, getVfkitVersion)
8+
return New(vfkit.ExecutablePath(), vfkit.VfkitDownloadURL, vfkit.VfkitVersion, getVfkitVersion)
119
}
1210

1311
func getVfkitVersion(executablePath string) (string, error) {

pkg/crc/cache/cache_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
func NewMachineDriverLibvirtCache() *Cache {
8-
return New(libvirt.MachineDriverCommand, libvirt.MachineDriverDownloadURL, libvirt.MachineDriverVersion, getCurrentLibvirtDriverVersion)
8+
return New(libvirt.MachineDriverPath(), libvirt.MachineDriverDownloadURL, libvirt.MachineDriverVersion, getCurrentLibvirtDriverVersion)
99
}
1010

1111
func getCurrentLibvirtDriverVersion(executablePath string) (string, error) {

0 commit comments

Comments
 (0)