Skip to content

Commit 32e00b2

Browse files
cfergeaupraveenkumar
authored andcommitted
constants: Add ResolveHelperPath() helper
At the moment, helper files (vfkit, crc-admin-helper, crc-driver-libvirt) are expected to all be found in BinDir(). BinDir() is either the installation directory if we made a release build, or ~/.crc/bin if built from git. However, this is not flexible enough for homebrew where vfkit and crc-admin-helper will be in 2 different directories. This commit adds a constants.ResolveHelperPath() function which is very similar to `BinDir()` except that we can add more logic at a later point. It's possible to return helper-specific paths as it's passed as an argument. This commit removes the use of BinDir() and reexports CrcBinDir for the code downloading helpers from the net. This code targets ~/.crc/bin and is not run in the 'IsInstaller()' case, so it does not need BinDir().
1 parent 275925c commit 32e00b2

File tree

4 files changed

+13
-19
lines changed

4 files changed

+13
-19
lines changed

pkg/crc/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (c *Cache) cacheExecutable() error {
134134

135135
// Copy the requested asset into its final destination
136136
for _, extractedFilePath := range extractedFiles {
137-
finalExecutablePath := filepath.Join(constants.BinDir(), filepath.Base(extractedFilePath))
137+
finalExecutablePath := filepath.Join(constants.CrcBinDir, filepath.Base(extractedFilePath))
138138
// If the file exists then remove it (ignore error) first before copy because with `0500` permission
139139
// it is not possible to overwrite the file.
140140
os.Remove(finalExecutablePath)

pkg/crc/constants/constants.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ func GetAdminHelperExecutableForOs(os string) string {
7575
return adminHelperExecutableForOs[os]
7676
}
7777

78-
func GetAdminHelperExecutable() string {
79-
return GetAdminHelperExecutableForOs(runtime.GOOS)
80-
}
81-
8278
func GetAdminHelperURLForOs(os string) string {
8379
return fmt.Sprintf(DefaultAdminHelperURLBase, version.GetAdminHelperVersion(), GetAdminHelperExecutableForOs(os))
8480
}
@@ -118,9 +114,9 @@ func GetDefaultBundle(preset crcpreset.Preset) string {
118114

119115
var (
120116
CrcBaseDir = filepath.Join(GetHomeDir(), ".crc")
121-
crcBinDir = filepath.Join(CrcBaseDir, "bin")
122-
CrcOcBinDir = filepath.Join(crcBinDir, "oc")
123-
CrcSymlinkPath = filepath.Join(crcBinDir, "crc")
117+
CrcBinDir = filepath.Join(CrcBaseDir, "bin")
118+
CrcOcBinDir = filepath.Join(CrcBinDir, "oc")
119+
CrcSymlinkPath = filepath.Join(CrcBinDir, "crc")
124120
ConfigPath = filepath.Join(CrcBaseDir, ConfigFile)
125121
LogFilePath = filepath.Join(CrcBaseDir, LogFile)
126122
DaemonLogFilePath = filepath.Join(CrcBaseDir, DaemonLogFile)
@@ -135,15 +131,15 @@ func GetDefaultBundlePath(preset crcpreset.Preset) string {
135131
return filepath.Join(MachineCacheDir, GetDefaultBundle(preset))
136132
}
137133

138-
func BinDir() string {
134+
func ResolveHelperPath(executableName string) string {
139135
if version.IsInstaller() {
140-
return version.InstallPath()
136+
return filepath.Join(version.InstallPath(), executableName)
141137
}
142-
return crcBinDir
138+
return filepath.Join(CrcBinDir, executableName)
143139
}
144140

145141
func AdminHelperPath() string {
146-
return filepath.Join(BinDir(), GetAdminHelperExecutable())
142+
return ResolveHelperPath(GetAdminHelperExecutableForOs(runtime.GOOS))
147143
}
148144

149145
// GetHomeDir returns the home directory for the current user
@@ -157,7 +153,7 @@ func GetHomeDir() string {
157153

158154
// EnsureBaseDirectoriesExist creates ~/.crc, ~/.crc/bin and ~/.crc/cache directories if it is not present
159155
func EnsureBaseDirectoriesExist() error {
160-
baseDirectories := []string{CrcBaseDir, MachineCacheDir, crcBinDir}
156+
baseDirectories := []string{CrcBaseDir, MachineCacheDir, CrcBinDir}
161157
for _, baseDir := range baseDirectories {
162158
err := os.MkdirAll(baseDir, 0750)
163159
if err != nil {

pkg/crc/machine/libvirt/constants.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package libvirt
55

66
import (
77
"fmt"
8-
"path/filepath"
98

109
"github.com/crc-org/crc/pkg/crc/constants"
1110
)
@@ -21,14 +20,14 @@ const (
2120
)
2221

2322
const (
24-
MachineDriverCommand = "crc-driver-libvirt"
23+
machineDriverCommand = "crc-driver-libvirt"
2524
MachineDriverVersion = "0.13.5"
2625
)
2726

2827
var (
29-
MachineDriverDownloadURL = fmt.Sprintf("https://github.com/crc-org/machine-driver-libvirt/releases/download/%s/%s", MachineDriverVersion, MachineDriverCommand)
28+
MachineDriverDownloadURL = fmt.Sprintf("https://github.com/crc-org/machine-driver-libvirt/releases/download/%s/%s", MachineDriverVersion, machineDriverCommand)
3029
)
3130

3231
func MachineDriverPath() string {
33-
return filepath.Join(constants.BinDir(), MachineDriverCommand)
32+
return constants.ResolveHelperPath(machineDriverCommand)
3433
}

pkg/crc/machine/vfkit/constants.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package vfkit
55

66
import (
77
"fmt"
8-
"path/filepath"
98

109
"github.com/crc-org/crc/pkg/crc/constants"
1110
)
@@ -21,5 +20,5 @@ var (
2120
)
2221

2322
func ExecutablePath() string {
24-
return filepath.Join(constants.BinDir(), vfkitCommand)
23+
return constants.ResolveHelperPath(vfkitCommand)
2524
}

0 commit comments

Comments
 (0)