-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support labels add/remove on artifact
- Loading branch information
1 parent
afd5683
commit 2b48067
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package artifact | ||
|
||
import ( | ||
"github.com/goharbor/harbor-cli/pkg/api" | ||
"github.com/goharbor/harbor-cli/pkg/prompt" | ||
"github.com/goharbor/harbor-cli/pkg/utils" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func ArtifactLabelsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "labels", | ||
Short: "Manage labels of an artifact", | ||
} | ||
|
||
cmd.AddCommand( | ||
AddLabelsCmd(), | ||
RemoveLabelsCmd(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func AddLabelsCmd() *cobra.Command { | ||
var opts api.ListFlags | ||
cmd := &cobra.Command{ | ||
Use: "add", | ||
Short: "Add a label of an artifact", | ||
Example: `harbor artifact labels add <project>/<repository>/<reference> <labelName|labelID>`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var err error | ||
var projectName, repoName, reference string | ||
var labelID int64 | ||
|
||
if len(args) > 0 { | ||
projectName, repoName, reference = utils.ParseProjectRepoReference(args[0]) | ||
labelID, err = api.GetLabelIdByName(args[1]) | ||
if err != nil { | ||
logrus.Errorf("Failed to get this lable: %s", args[1]) | ||
return | ||
} | ||
} else { | ||
projectName = prompt.GetProjectNameFromUser() | ||
repoName = prompt.GetRepoNameFromUser(projectName) | ||
reference = prompt.GetReferenceFromUser(repoName, projectName) | ||
labelID = prompt.GetLabelIdFromUser(opts) | ||
} | ||
err = api.AddLabel(projectName, repoName, reference, labelID) | ||
|
||
if err != nil { | ||
logrus.Errorf("Failed to add label %s/%s@%s", projectName, repoName, reference) | ||
return | ||
} | ||
|
||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func RemoveLabelsCmd() *cobra.Command { | ||
var opts api.ListFlags | ||
|
||
cmd := &cobra.Command{ | ||
Use: "remove", | ||
Short: "Remove a label of an artifact", | ||
Example: `harbor artifact labels remove <project>/<repository>/<reference> <labelName|labelID>`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var err error | ||
var projectName, repoName, reference string | ||
var labelID int64 | ||
|
||
if len(args) > 0 { | ||
projectName, repoName, reference = utils.ParseProjectRepoReference(args[0]) | ||
labelID, err = api.GetLabelIdByName(args[1]) | ||
if err != nil { | ||
logrus.Errorf("Failed to get this lable: %s", args[1]) | ||
return | ||
} | ||
} else { | ||
projectName = prompt.GetProjectNameFromUser() | ||
repoName = prompt.GetRepoNameFromUser(projectName) | ||
reference = prompt.GetReferenceFromUser(repoName, projectName) | ||
labelID = prompt.GetLabelIdFromUser(opts) | ||
} | ||
err = api.RemoveLabel(projectName, repoName, reference, labelID) | ||
|
||
if err != nil { | ||
logrus.Errorf("Failed to remove label %s/%s@%s", projectName, repoName, reference) | ||
return | ||
} | ||
|
||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters