| package actions |
| |
| import ( |
| "context" |
| "fmt" |
| "path" |
| |
| "cloud.google.com/go/storage" |
| "cos.googlesource.com/cos/tools.git/src/pkg/cos" |
| "cos.googlesource.com/cos/tools.git/src/pkg/kernel" |
| "github.com/spf13/cobra" |
| "google.golang.org/api/option" |
| ) |
| |
| var downloadDir string |
| |
| func defaultDownloader(ctx context.Context) (*cos.GCSDownloader, error) { |
| if build == "" { |
| return nil, fmt.Errorf("must specify --build") |
| } |
| guessedBuild, err := cos.GuessBuildNumber(build) |
| if err != nil { |
| return nil, fmt.Errorf("could not get build number from build %s: %v", build, err) |
| } |
| build = guessedBuild |
| |
| if board == "" { |
| return nil, fmt.Errorf("must specify --board") |
| } |
| |
| client, err := storage.NewClient(ctx, option.WithoutAuthentication()) |
| if err != nil { |
| return nil, err |
| } |
| |
| prefix := path.Join(build, board) |
| downloader := cos.NewGCSDownloader(client, nil, "", prefix, "", "", useRegional) |
| |
| return downloader, nil |
| } |
| |
| var downloadCmd = &cobra.Command{ |
| Use: "download", |
| Short: "Download COS kernel artifacts", |
| Long: "Download COS kernel artifacts such as toolchains and headers.", |
| } |
| |
| var toolchainCmd = &cobra.Command{ |
| Use: "toolchain", |
| Short: "Download the COS C toolchain", |
| Long: `Determine the correct toolchain location in the cos-tools GCS bucket |
| and download it to the provided output directory, or the local directory |
| if no output dir is provided. |
| |
| When the download is fully complete, a blank toolchain-installed.txt file |
| will be written in the output directory. This ensures that the toolchain |
| download is atomic and can be skipped if the toolchain-installed.txt file |
| is already present.`, |
| RunE: func(cmd *cobra.Command, args []string) error { |
| ctx := context.Background() |
| downloader, err := defaultDownloader(ctx) |
| if err != nil { |
| return fmt.Errorf("failed to create artifact downloader: %v", err) |
| } |
| |
| return kernel.InstallCompilerToolchainAtomic(ctx, downloader, downloadDir) |
| }, |
| } |
| |
| var headersCmd = &cobra.Command{ |
| Use: "headers", |
| Short: "Download the COS kernel headers", |
| Long: `Determine the correct kernel headers location in the cos-tools GCS bucket |
| and download them to the provided output directory, or the local directory |
| if no output dir is provided. |
| |
| When the download is fully complete, a blank kernel-headers-installed.txt file |
| will be written in the output directory. This ensures that the download |
| is atomic and can be skipped if the kernel-headers-installed.txt file is already |
| present.`, |
| RunE: func(cmd *cobra.Command, args []string) error { |
| ctx := context.Background() |
| downloader, err := defaultDownloader(ctx) |
| if err != nil { |
| return fmt.Errorf("failed to create artifact downloader: %v", err) |
| } |
| |
| return kernel.InstallKernelHeadersAtomic(ctx, downloader, downloadDir) |
| }, |
| } |
| |
| func init() { |
| cmds := []*cobra.Command{downloadCmd, toolchainCmd, headersCmd} |
| for _, cmd := range cmds { |
| cmdFlags := cmd.PersistentFlags() |
| cmdFlags.StringVarP(&downloadDir, "out", "o", ".", |
| "the output directory for the downloaded files") |
| |
| addCommonFlags(cmdFlags) |
| } |
| |
| rootCmd.AddCommand(downloadCmd) |
| downloadCmd.AddCommand(toolchainCmd) |
| downloadCmd.AddCommand(headersCmd) |
| } |