|
| 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 display |
| 17 | + |
| 18 | +import ( |
| 19 | + "os" |
| 20 | + |
| 21 | + "oras.land/oras/cmd/oras/internal/display/metadata" |
| 22 | + "oras.land/oras/cmd/oras/internal/display/metadata/json" |
| 23 | + "oras.land/oras/cmd/oras/internal/display/metadata/template" |
| 24 | + "oras.land/oras/cmd/oras/internal/display/metadata/text" |
| 25 | + "oras.land/oras/cmd/oras/internal/display/status" |
| 26 | +) |
| 27 | + |
| 28 | +// NewPushHandler returns status and metadata handlers for push command. |
| 29 | +func NewPushHandler(format string, tty *os.File, verbose bool) (status.PushHandler, metadata.PushHandler) { |
| 30 | + var statusHandler status.PushHandler |
| 31 | + if tty != nil { |
| 32 | + statusHandler = status.NewTTYPushHandler(tty) |
| 33 | + } else if format == "" { |
| 34 | + statusHandler = status.NewTextPushHandler(verbose) |
| 35 | + } else { |
| 36 | + statusHandler = status.NewDiscardHandler() |
| 37 | + } |
| 38 | + |
| 39 | + var metadataHandler metadata.PushHandler |
| 40 | + switch format { |
| 41 | + case "": |
| 42 | + metadataHandler = text.NewPushHandler() |
| 43 | + case "json": |
| 44 | + metadataHandler = json.NewPushHandler() |
| 45 | + default: |
| 46 | + metadataHandler = template.NewPushHandler(format) |
| 47 | + } |
| 48 | + |
| 49 | + return statusHandler, metadataHandler |
| 50 | +} |
| 51 | + |
| 52 | +// NewAttachHandler returns status and metadata handlers for attach command. |
| 53 | +func NewAttachHandler(format string, tty *os.File, verbose bool) (status.AttachHandler, metadata.AttachHandler) { |
| 54 | + var statusHandler status.AttachHandler |
| 55 | + if tty != nil { |
| 56 | + statusHandler = status.NewTTYAttachHandler(tty) |
| 57 | + } else if format == "" { |
| 58 | + statusHandler = status.NewTextAttachHandler(verbose) |
| 59 | + } else { |
| 60 | + statusHandler = status.NewDiscardHandler() |
| 61 | + } |
| 62 | + |
| 63 | + var metadataHandler metadata.AttachHandler |
| 64 | + switch format { |
| 65 | + case "": |
| 66 | + metadataHandler = text.NewAttachHandler() |
| 67 | + case "json": |
| 68 | + metadataHandler = json.NewAttachHandler() |
| 69 | + default: |
| 70 | + metadataHandler = template.NewAttachHandler(format) |
| 71 | + } |
| 72 | + |
| 73 | + return statusHandler, metadataHandler |
| 74 | +} |
0 commit comments