Skip to content

Commit 7a7592a

Browse files
anjannathpraveenkumar
authored andcommitted
Add flag --components to crc-embedder tool
this is needed when we want to select what data files to download/embed current crc-embedder downloads all the data files needed for a platform
1 parent 3907835 commit 7a7592a

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

cmd/crc-embedder/cmd/download.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"runtime"
6+
"strings"
57

68
"github.com/spf13/cobra"
79
)
810

911
func init() {
1012
downloadCmd.Flags().StringVar(&goos, "goos", runtime.GOOS, "Target platform (darwin, linux or windows)")
13+
downloadCmd.Flags().StringSliceVar(&components, "components", []string{}, fmt.Sprintf("List of component(s) to download (%s)", strings.Join(getAllComponentNames(goos), ", ")))
1114
rootCmd.AddCommand(downloadCmd)
1215
}
1316

@@ -21,6 +24,6 @@ var downloadCmd = &cobra.Command{
2124
}
2225

2326
func runDownload(args []string) error {
24-
_, err := downloadDataFiles(goos, args[0])
27+
_, err := downloadDataFiles(goos, components, args[0])
2528
return err
2629
}

cmd/crc-embedder/cmd/embed.go

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path"
77
"path/filepath"
88
"runtime"
9+
"strings"
910

1011
"github.com/crc-org/crc/pkg/crc/constants"
1112
"github.com/crc-org/crc/pkg/crc/logging"
@@ -21,13 +22,23 @@ import (
2122
var (
2223
goos string
2324
cacheDir string
25+
components []string
2426
noDownload bool
2527
)
2628

29+
const (
30+
vfkitDriver = "vfkit-driver"
31+
vfkitEntitlement = "vfkit-entitlement"
32+
libvirtDriver = "libvirt-driver"
33+
adminHelper = "admin-helper"
34+
tray = "tray"
35+
)
36+
2737
func init() {
2838
embedCmd.Flags().StringVar(&goos, "goos", runtime.GOOS, "Target platform (darwin, linux or windows)")
2939
embedCmd.Flags().StringVar(&cacheDir, "cache-dir", "", "Destination directory for the downloaded files")
3040
embedCmd.Flags().BoolVar(&noDownload, "no-download", false, "Only embed files, don't download")
41+
embedCmd.Flags().StringSliceVar(&components, "components", []string{}, fmt.Sprintf("List of component(s) to download (%s)", strings.Join(getAllComponentNames(goos), ", ")))
3142
rootCmd.AddCommand(embedCmd)
3243
}
3344

@@ -55,7 +66,7 @@ func runEmbed(args []string) {
5566
if noDownload {
5667
embedFileList = getEmbedFileList(goos, cacheDir)
5768
} else {
58-
embedFileList, err = downloadDataFiles(goos, cacheDir)
69+
embedFileList, err = downloadDataFiles(goos, components, cacheDir)
5970
if err != nil {
6071
logging.Fatalf("Failed to download data files: %v", err)
6172
}
@@ -95,20 +106,20 @@ type remoteFileInfo struct {
95106
}
96107

97108
var (
98-
dataFileUrls = map[string][]remoteFileInfo{
109+
dataFileUrls = map[string]map[string]remoteFileInfo{
99110
"darwin": {
100-
{vfkit.VfkitDownloadURL, 0755},
101-
{vfkit.VfkitEntitlementsURL, 0644},
102-
{constants.GetCRCMacTrayDownloadURL(), 0644},
103-
{constants.GetAdminHelperURLForOs("darwin"), 0755},
111+
vfkitDriver: {vfkit.VfkitDownloadURL, 0755},
112+
vfkitEntitlement: {vfkit.VfkitEntitlementsURL, 0644},
113+
tray: {constants.GetCRCMacTrayDownloadURL(), 0644},
114+
adminHelper: {constants.GetAdminHelperURLForOs("darwin"), 0755},
104115
},
105116
"linux": {
106-
{libvirt.MachineDriverDownloadURL, 0755},
107-
{constants.GetAdminHelperURLForOs("linux"), 0755},
117+
libvirtDriver: {libvirt.MachineDriverDownloadURL, 0755},
118+
adminHelper: {constants.GetAdminHelperURLForOs("linux"), 0755},
108119
},
109120
"windows": {
110-
{constants.GetAdminHelperURLForOs("windows"), 0755},
111-
{constants.GetCRCWindowsTrayDownloadURL(), 0644},
121+
adminHelper: {constants.GetAdminHelperURLForOs("windows"), 0755},
122+
tray: {constants.GetCRCWindowsTrayDownloadURL(), 0644},
112123
},
113124
}
114125
)
@@ -124,16 +135,44 @@ func getEmbedFileList(goos string, destDir string) []string {
124135
return fileList
125136
}
126137

127-
func downloadDataFiles(goos string, destDir string) ([]string, error) {
138+
func getAllComponentNames(goos string) []string {
139+
var components []string
140+
for component := range dataFileUrls[goos] {
141+
components = append(components, component)
142+
}
143+
return components
144+
}
145+
146+
func shouldDownload(components []string, component string) bool {
147+
if len(components) == 0 {
148+
return true
149+
}
150+
for _, v := range components {
151+
if v == component {
152+
return true
153+
}
154+
}
155+
return false
156+
}
157+
158+
func downloadDataFiles(goos string, components []string, destDir string) ([]string, error) {
128159
downloadedFiles := []string{}
129160
downloads := dataFileUrls[goos]
130-
for _, dl := range downloads {
161+
162+
for componentName, dl := range downloads {
163+
if !shouldDownload(components, componentName) {
164+
continue
165+
}
131166
filename, err := download.Download(dl.url, destDir, dl.permissions, nil)
132167
if err != nil {
133168
return nil, err
134169
}
135170
downloadedFiles = append(downloadedFiles, filename)
136171
}
137172

173+
if len(components) != 0 && len(components) != len(downloadedFiles) {
174+
logging.Warnf("invalid components requested, supported components are: %s", strings.Join(getAllComponentNames(goos), ", "))
175+
}
176+
138177
return downloadedFiles, nil
139178
}

0 commit comments

Comments
 (0)