diff --git a/cmd/harbor/root/artifact/scan.go b/cmd/harbor/root/artifact/scan.go index 34cea8da..cf8f235f 100644 --- a/cmd/harbor/root/artifact/scan.go +++ b/cmd/harbor/root/artifact/scan.go @@ -13,7 +13,7 @@ func ScanArtifactCommand() *cobra.Command { Use: "scan", Short: "Scan an artifact", Long: `Scan an artifact in Harbor Repository`, - Example: `harbor artifact scan start //`, + Example: `harbor artifact scan start /@`, } cmd.AddCommand( @@ -30,7 +30,7 @@ func StartScanArtifactCommand() *cobra.Command { Use: "start", Short: "Start a scan of an artifact", Long: `Start a scan of an artifact in Harbor Repository`, - Example: `harbor artifact scan start //`, + Example: `harbor artifact scan start /@`, Run: func(cmd *cobra.Command, args []string) { var err error @@ -56,7 +56,7 @@ func StopScanArtifactCommand() *cobra.Command { Use: "stop", Short: "Stop a scan of an artifact", Long: `Stop a scan of an artifact in Harbor Repository`, - Example: `harbor artifact scan stop //`, + Example: `harbor artifact scan stop /@`, Run: func(cmd *cobra.Command, args []string) { var err error diff --git a/cmd/harbor/root/artifact/tags.go b/cmd/harbor/root/artifact/tags.go index a213b3c7..deae4ece 100644 --- a/cmd/harbor/root/artifact/tags.go +++ b/cmd/harbor/root/artifact/tags.go @@ -9,14 +9,13 @@ import ( "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/list" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func ArtifactTagsCmd() *cobra.Command { cmd := &cobra.Command{ Use: "tags", Short: "Manage tags of an artifact", - Example: ` harbor artifact tags list //`, + Example: `harbor artifact tags list /@`, } cmd.AddCommand( @@ -32,7 +31,7 @@ func CreateTagsCmd() *cobra.Command { cmd := &cobra.Command{ Use: "create", Short: "Create a tag of an artifact", - Example: `harbor artifact tags create // `, + Example: `harbor artifact tags create /@ `, Run: func(cmd *cobra.Command, args []string) { var err error @@ -58,43 +57,36 @@ func CreateTagsCmd() *cobra.Command { } func ListTagsCmd() *cobra.Command { + var opts api.ListFlags cmd := &cobra.Command{ Use: "list", Short: "List tags of an artifact", - Example: `harbor artifact tags list //`, + Example: `harbor artifact tags list /@`, Run: func(cmd *cobra.Command, args []string) { var err error - var tags *artifact.ListTagsOK - var projectName, repoName, reference string + var resp artifact.ListTagsOK if len(args) > 0 { - projectName, repoName, reference = utils.ParseProjectRepoReference(args[0]) + projectName, repoName, reference := utils.ParseProjectRepoReference(args[0]) + resp, err = api.ListTags(projectName, repoName, reference, opts) } else { - projectName = prompt.GetProjectNameFromUser() - repoName = prompt.GetRepoNameFromUser(projectName) - reference = prompt.GetReferenceFromUser(repoName, projectName) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + resp, err = api.ListTags(projectName, repoName, reference, opts) } - - tags, err = api.ListTags(projectName, repoName, reference) - if err != nil { log.Errorf("failed to list tags: %v", err) - return - } - - FormatFlag := viper.GetString("output-format") - if FormatFlag != "" { - err = utils.PrintFormat(tags, FormatFlag) - if err != nil { - log.Error(err) - return - } - } else { - list.ListTags(tags.Payload) } + list.ListTagArtifact(resp.Payload) }, } + flags := cmd.Flags() + flags.Int64VarP(&opts.Page, "page", "p", 1, "Page number") + flags.Int64VarP(&opts.PageSize, "page-size", "n", 10, "Size of per page") + flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources") + flags.StringVarP(&opts.Sort, "sort", "", "", "Sort the resource list in ascending or descending order") return cmd } @@ -103,7 +95,7 @@ func DeleteTagsCmd() *cobra.Command { cmd := &cobra.Command{ Use: "delete", Short: "Delete a tag of an artifact", - Example: `harbor artifact tags delete // `, + Example: `harbor artifact tags delete /@ `, Run: func(cmd *cobra.Command, args []string) { var err error diff --git a/pkg/api/artifact_handler.go b/pkg/api/artifact_handler.go index d79eba44..1a52e541 100644 --- a/pkg/api/artifact_handler.go +++ b/pkg/api/artifact_handler.go @@ -21,7 +21,6 @@ func DeleteArtifact(projectName, repoName, reference string) error { Reference: reference, }) if err != nil { - log.Errorf("Failed to delete artifact: %v", err) return err } @@ -44,7 +43,6 @@ func ViewArtifact(projectName, repoName, reference string) (*artifact.GetArtifac }) if err != nil { - log.Errorf("Failed to get artifact info: %v", err) return response, err } @@ -90,7 +88,6 @@ func StartScanArtifact(projectName, repoName, reference string) error { Reference: reference, }) if err != nil { - log.Errorf("Failed to start scan: %v", err) return err } @@ -111,7 +108,6 @@ func StopScanArtifact(projectName, repoName, reference string) error { Reference: reference, }) if err != nil { - log.Errorf("Failed to stop scan: %v", err) return err } @@ -133,7 +129,6 @@ func DeleteTag(projectName, repoName, reference, tag string) error { TagName: tag, }) if err != nil { - log.Errorf("Failed to delete tag: %v", err) return err } @@ -142,24 +137,29 @@ func DeleteTag(projectName, repoName, reference, tag string) error { } // ListTags lists all tags of a specific artifact. -func ListTags(projectName, repoName, reference string) (*artifact.ListTagsOK, error) { +func ListTags(projectName, repoName, reference string, opts ...ListFlags) (artifact.ListTagsOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return &artifact.ListTagsOK{}, err + return artifact.ListTagsOK{}, err + } + var listFlags ListFlags + if len(opts) > 0 { + listFlags = opts[0] } - resp, err := client.Artifact.ListTags(ctx, &artifact.ListTagsParams{ ProjectName: projectName, RepositoryName: repoName, Reference: reference, + Page: &listFlags.Page, + PageSize: &listFlags.PageSize, + Q: &listFlags.Q, + Sort: &listFlags.Sort, }) - if err != nil { - log.Errorf("Failed to list tags: %v", err) - return &artifact.ListTagsOK{}, err + return artifact.ListTagsOK{}, err } - return resp, nil + return *resp, nil } // CreateTag creates a tag for a specific artifact. @@ -177,7 +177,6 @@ func CreateTag(projectName, repoName, reference, tagName string) error { }, }) if err != nil { - log.Errorf("Failed to create tag: %v", err) return err } log.Infof("Tag created successfully: %s/%s@%s:%s", projectName, repoName, reference, tagName) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 5bff7837..88e65635 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -47,11 +47,20 @@ func ParseProjectRepo(projectRepo string) (string, string) { } func ParseProjectRepoReference(projectRepoReference string) (string, string, string) { - split := strings.Split(projectRepoReference, "/") - if len(split) != 3 { - log.Fatalf("invalid project/repository/reference format: %s", projectRepoReference) + var projectname []string + var reponame []string + var referencename []string + indexSlash := strings.Index(projectRepoReference, "/") + indexAt := strings.Index(projectRepoReference, "@") + + if indexSlash != -1 && indexAt != -1 && indexSlash < indexAt { + projectname = strings.Split(projectRepoReference, "/") + reponame = strings.Split(projectname[1], "@") + referencename = strings.Split(projectname[1], "@") + } else { + log.Fatalf("invalid project/repository@reference format: %s", projectRepoReference) } - return split[0], split[1], split[2] + return projectname[0], reponame[0], referencename[1] } func SanitizeServerAddress(server string) string { diff --git a/pkg/views/artifact/tags/list/view.go b/pkg/views/artifact/tags/list/view.go index f20491b5..7e169577 100644 --- a/pkg/views/artifact/tags/list/view.go +++ b/pkg/views/artifact/tags/list/view.go @@ -3,6 +3,7 @@ package list import ( "fmt" "os" + "strconv" "github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" @@ -12,18 +13,19 @@ import ( ) var columns = []table.Column{ - {Title: "Name", Width: 12}, - {Title: "Pull Time", Width: 30}, - {Title: "Push Time", Width: 30}, + {Title: "ID", Width: 6}, + {Title: "Tag Name", Width: 20}, + {Title: "Pull Time", Width: 18}, + {Title: "Push Time", Width: 18}, } -func ListTags(tags []*models.Tag) { +func ListTagArtifact(artifacts []*models.Tag) { var rows []table.Row - for _, tag := range tags { - - pullTime, _ := utils.FormatCreatedTime(tag.PullTime.String()) + for _, tag := range artifacts { pushTime, _ := utils.FormatCreatedTime(tag.PushTime.String()) + pullTime, _ := utils.FormatCreatedTime(tag.PullTime.String()) rows = append(rows, table.Row{ + strconv.FormatInt(int64(tag.ID), 10), tag.Name, pullTime, pushTime,