|
6 | 6 | "fmt"
|
7 | 7 | "log"
|
8 | 8 | "os"
|
| 9 | + "os/exec" |
9 | 10 | "os/signal"
|
10 | 11 | "sort"
|
11 | 12 | "strings"
|
@@ -78,6 +79,40 @@ var artifactsCmd = &cobra.Command{
|
78 | 79 | },
|
79 | 80 | }
|
80 | 81 |
|
| 82 | +var artifactsAllCmd = &cobra.Command{ |
| 83 | + Use: "artifacts-all", |
| 84 | + Short: "Download all the artifacts available in the catalog (Used for testing purposes)", |
| 85 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 86 | + fmt.Println("Downloading all artifacts...") |
| 87 | + |
| 88 | + output := outputFlag |
| 89 | + if output == "" { |
| 90 | + homeDir, err := internal.GetHomeDir() |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("failed to get home directory: %w", err) |
| 93 | + } |
| 94 | + output = homeDir |
| 95 | + } |
| 96 | + for _, component := range internal.Components { |
| 97 | + releaseService, ok := component.(internal.ReleaseService) |
| 98 | + if !ok { |
| 99 | + continue |
| 100 | + } |
| 101 | + location, err := internal.DownloadRelease(output, releaseService.ReleaseArtifact()) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("failed to download release: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + // make sure the artifact is valid to be executed on this platform |
| 107 | + log.Printf("Downloaded %s to %s\n", releaseService.ReleaseArtifact().Name, location) |
| 108 | + if err := isExecutableValid(location); err != nil { |
| 109 | + return fmt.Errorf("failed to check if artifact is valid: %w", err) |
| 110 | + } |
| 111 | + } |
| 112 | + return nil |
| 113 | + }, |
| 114 | +} |
| 115 | + |
81 | 116 | var recipes = []internal.Recipe{
|
82 | 117 | &internal.L1Recipe{},
|
83 | 118 | &internal.OpRecipe{},
|
@@ -110,9 +145,11 @@ func main() {
|
110 | 145 |
|
111 | 146 | // reuse the same output flag for the artifacts command
|
112 | 147 | artifactsCmd.Flags().StringVar(&outputFlag, "output", "", "Output folder for the artifacts")
|
| 148 | + artifactsAllCmd.Flags().StringVar(&outputFlag, "output", "", "Output folder for the artifacts") |
113 | 149 |
|
114 | 150 | rootCmd.AddCommand(cookCmd)
|
115 | 151 | rootCmd.AddCommand(artifactsCmd)
|
| 152 | + rootCmd.AddCommand(artifactsAllCmd) |
116 | 153 |
|
117 | 154 | if err := rootCmd.Execute(); err != nil {
|
118 | 155 | fmt.Println(err)
|
@@ -231,3 +268,26 @@ func runIt(recipe internal.Recipe) error {
|
231 | 268 | }
|
232 | 269 | return nil
|
233 | 270 | }
|
| 271 | + |
| 272 | +func isExecutableValid(path string) error { |
| 273 | + // First check if file exists |
| 274 | + _, err := os.Stat(path) |
| 275 | + if err != nil { |
| 276 | + return fmt.Errorf("file does not exist or is inaccessible: %w", err) |
| 277 | + } |
| 278 | + |
| 279 | + // Try to execute with a harmless flag or in a way that won't run the main program |
| 280 | + cmd := exec.Command(path, "--version") |
| 281 | + // Redirect output to /dev/null |
| 282 | + cmd.Stdout = nil |
| 283 | + cmd.Stderr = nil |
| 284 | + |
| 285 | + if err := cmd.Start(); err != nil { |
| 286 | + return fmt.Errorf("cannot start executable: %w", err) |
| 287 | + } |
| 288 | + |
| 289 | + // Immediately kill the process since we just want to test if it starts |
| 290 | + cmd.Process.Kill() |
| 291 | + |
| 292 | + return nil |
| 293 | +} |
0 commit comments