|
| 1 | +/* |
| 2 | +Copyright The ORAS Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package text |
| 17 | + |
| 18 | +import ( |
| 19 | + "strings" |
| 20 | + "time" |
| 21 | + |
| 22 | + "oras.land/oras/cmd/oras/internal/display/status/progress/humanize" |
| 23 | + "oras.land/oras/cmd/oras/internal/output" |
| 24 | +) |
| 25 | + |
| 26 | +// RestoreHandler handles text metadata output for restore command. |
| 27 | +type RestoreHandler struct { |
| 28 | + printer *output.Printer |
| 29 | + dryRun bool |
| 30 | +} |
| 31 | + |
| 32 | +// NewRestoreHandler creates a new RestoreHandler. |
| 33 | +func NewRestoreHandler(printer *output.Printer, dryRun bool) *RestoreHandler { |
| 34 | + return &RestoreHandler{ |
| 35 | + printer: printer, |
| 36 | + dryRun: dryRun, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// OnTarLoaded implements metadata.RestoreHandler. |
| 41 | +func (rh *RestoreHandler) OnTarLoaded(path string, size int64) error { |
| 42 | + return rh.printer.Printf("Loaded backup archive: %s (%s)\n", path, humanize.ToBytes(size)) |
| 43 | +} |
| 44 | + |
| 45 | +// OnTagsFound implements metadata.RestoreHandler. |
| 46 | +func (rh *RestoreHandler) OnTagsFound(tags []string) error { |
| 47 | + if len(tags) == 0 { |
| 48 | + return rh.printer.Printf("No tags found in the backup\n") |
| 49 | + } |
| 50 | + if len(tags) <= 5 { |
| 51 | + // print small number of tags in one line |
| 52 | + return rh.printer.Printf("Found %d tag(s) in the backup: %s\n", len(tags), strings.Join(tags, ", ")) |
| 53 | + } |
| 54 | + // print large number of tags line by line |
| 55 | + if err := rh.printer.Printf("Found %d tag(s) in the backup:\n", len(tags)); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + for _, tag := range tags { |
| 59 | + if err := rh.printer.Println(tag); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// OnArtifactPushed implements metadata.RestoreHandler. |
| 67 | +func (rh *RestoreHandler) OnArtifactPushed(tag string, referrerCount int) error { |
| 68 | + if rh.dryRun { |
| 69 | + return rh.printer.Printf("Dry run: would push tag %s with %d referrer(s)\n", tag, referrerCount) |
| 70 | + } |
| 71 | + return rh.printer.Printf("Pushed tag %s with %d referrer(s)\n", tag, referrerCount) |
| 72 | +} |
| 73 | + |
| 74 | +// OnRestoreCompleted implements metadata.RestoreHandler. |
| 75 | +func (rh *RestoreHandler) OnRestoreCompleted(tagsCount int, repo string, duration time.Duration) error { |
| 76 | + if rh.dryRun { |
| 77 | + return rh.printer.Printf("Dry run complete: %d tag(s) would be restored to %q (no data pushed)\n", tagsCount, repo) |
| 78 | + } |
| 79 | + return rh.printer.Printf("Successfully restored %d tag(s) to %q in %s\n", tagsCount, repo, humanize.FormatDuration(duration)) |
| 80 | +} |
| 81 | + |
| 82 | +// Render implements metadata.RestoreHandler. |
| 83 | +func (rh *RestoreHandler) Render() error { |
| 84 | + return nil |
| 85 | +} |
0 commit comments