|
| 1 | +// Copyright Project Harbor Authors |
| 2 | +// |
| 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 | +package label |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + |
| 19 | + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact" |
| 20 | + "github.com/goharbor/harbor-cli/pkg/api" |
| 21 | + "github.com/goharbor/harbor-cli/pkg/prompt" |
| 22 | + "github.com/goharbor/harbor-cli/pkg/utils" |
| 23 | + "github.com/goharbor/harbor-cli/pkg/views/label/list" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + "github.com/spf13/viper" |
| 26 | +) |
| 27 | + |
| 28 | +// DelLabelArtifactCommmand delete label command to artifact |
| 29 | +func ListLabelArtifactCommmand() *cobra.Command { |
| 30 | + cmd := &cobra.Command{ |
| 31 | + Use: "list", |
| 32 | + Short: "Display labels attached to a specific artifact", |
| 33 | + Long: `This command lists all labels currently associated with a specific artifact in a Harbor project repository. |
| 34 | +You can provide the artifact reference in the format <project>/<repository>:<reference> (where reference is either a tag or a digest). |
| 35 | +If the reference is not provided as an argument, the command will prompt you to select the project, repository, and artifact. |
| 36 | +
|
| 37 | +Supports output formatting such as JSON or YAML using the --output (-o) flag.`, |
| 38 | + Example: ` # List labels for a tagged artifact |
| 39 | + harbor artifact label list library/nginx:latest |
| 40 | +
|
| 41 | + # List labels for an artifact by digest |
| 42 | + harbor artifact label list myproject/myrepo@sha256:abc123... |
| 43 | +
|
| 44 | + # Prompt-based interactive selection of artifact |
| 45 | + harbor artifact label list |
| 46 | +
|
| 47 | + # Output in JSON format |
| 48 | + harbor artifact label list library/nginx:1.21 -o json`, |
| 49 | + Args: cobra.MaximumNArgs(1), |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + var err error |
| 52 | + var projectName, repoName, reference string |
| 53 | + var artifact *artifact.GetArtifactOK |
| 54 | + getLabel := true |
| 55 | + if len(args) > 0 { |
| 56 | + projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("failed to parse project/repo/reference: %v", err) |
| 59 | + } |
| 60 | + } else { |
| 61 | + projectName, err = prompt.GetProjectNameFromUser() |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) |
| 64 | + } |
| 65 | + repoName = prompt.GetRepoNameFromUser(projectName) |
| 66 | + reference = prompt.GetReferenceFromUser(repoName, projectName) |
| 67 | + } |
| 68 | + |
| 69 | + if reference == "" { |
| 70 | + if len(args) > 0 { |
| 71 | + return fmt.Errorf("Invalid artifact reference format: %s", args[0]) |
| 72 | + } else { |
| 73 | + return fmt.Errorf("Invalid artifact reference format: no arguments provided") |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + artifact, err = api.ViewArtifact(projectName, repoName, reference, getLabel) |
| 78 | + |
| 79 | + if err != nil || artifact == nil { |
| 80 | + return fmt.Errorf("failed to get info of an artifact: %v", utils.ParseHarborErrorMsg(err)) |
| 81 | + } |
| 82 | + labelList := artifact.Payload.Labels |
| 83 | + if len(labelList) == 0 { |
| 84 | + fmt.Printf("No labels found for artifact %s/%s@%s", projectName, repoName, reference) |
| 85 | + return nil |
| 86 | + } |
| 87 | + formatFlag := viper.GetString("output-format") |
| 88 | + if formatFlag != "" { |
| 89 | + err = utils.PrintFormat(labelList, formatFlag) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + } else { |
| 94 | + list.ListLabels(labelList) |
| 95 | + } |
| 96 | + return nil |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + return cmd |
| 101 | +} |
0 commit comments