| 50 | } |
| 51 | |
| 52 | func (a *ArtifactDownload) Run(downloadPath, outputFile, digest string) error { |
| 53 | h, err := crv1.NewHash(digest) |
| 54 | if err != nil { |
| 55 | return fmt.Errorf("invalid digest: %w", err) |
| 56 | } |
| 57 | |
| 58 | client := casclient.New(a.artifactsCASConn) |
| 59 | ctx := context.Background() |
| 60 | info, err := client.Describe(ctx, h.String()) |
| 61 | if err != nil { |
| 62 | return fmt.Errorf("artifact with digest %s not found", h) |
| 63 | } |
| 64 | |
| 65 | if downloadPath != "" && outputFile != "" { |
| 66 | return errors.New("both downloadPath and outputFile cannot be set at the same time") |
| 67 | } |
| 68 | |
| 69 | if downloadPath == "" { |
| 70 | downloadPath, err = os.Getwd() |
| 71 | if err != nil { |
| 72 | return fmt.Errorf("getting current dir: %w", err) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Determine output destination |
| 77 | outputPath := path.Join(downloadPath, info.Filename) |
| 78 | if outputFile != "" && outputFile != "-" { |
| 79 | outputPath = outputFile |
| 80 | } |
| 81 | |
| 82 | // Open output file |
| 83 | var f io.Writer |
| 84 | if outputFile == "-" { |
| 85 | f = a.stdout |
| 86 | } else { |
| 87 | f, err = os.Create(outputPath) |
| 88 | if err != nil { |
| 89 | return fmt.Errorf("creating destination file: %w", err) |
| 90 | } |
| 91 | defer f.(*os.File).Close() |
| 92 | |
| 93 | a.Logger.Info().Str("name", outputFile).Str("to", outputPath).Msg("downloading file") |
| 94 | } |
| 95 | |
| 96 | // Calculate the checksum as we write it to a file |
| 97 | hash := sha256.New() |
| 98 | w := io.MultiWriter(f, hash) |
| 99 | |
| 100 | // render progress bar |
| 101 | go renderOperationStatus(ctx, client.ProgressStatus, info.Size) |
| 102 | defer close(client.ProgressStatus) |
| 103 | |
| 104 | err = client.Download(ctx, w, h.String()) |
| 105 | if err != nil { |
| 106 | a.Logger.Debug().Err(err).Msg("problem downloading file") |
| 107 | return errors.New("problem downloading file") |
| 108 | } |
| 109 | |