Skip to content

Commit

Permalink
changes in artifact tags list cmd
Browse files Browse the repository at this point in the history
Signed-off-by: ALTHAF <[email protected]>
  • Loading branch information
Althaf66 committed Dec 17, 2024
1 parent 3c16b0c commit c5a7fd1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 53 deletions.
6 changes: 3 additions & 3 deletions cmd/harbor/root/artifact/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <project>/<repository>/<reference>`,
Example: `harbor artifact scan start <project>/<repository>@<reference>`,
}

cmd.AddCommand(
Expand All @@ -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 <project>/<repository>/<reference>`,
Example: `harbor artifact scan start <project>/<repository>@<reference>`,
Run: func(cmd *cobra.Command, args []string) {
var err error

Expand All @@ -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 <project>/<repository>/<reference>`,
Example: `harbor artifact scan stop <project>/<repository>@<reference>`,
Run: func(cmd *cobra.Command, args []string) {
var err error

Expand Down
44 changes: 18 additions & 26 deletions cmd/harbor/root/artifact/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <project>/<repository>/<reference>`,
Example: `harbor artifact tags list <project>/<repository>@<reference>`,
}

cmd.AddCommand(
Expand All @@ -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 <project>/<repository>/<reference> <tag>`,
Example: `harbor artifact tags create <project>/<repository>@<reference> <tag>`,
Run: func(cmd *cobra.Command, args []string) {
var err error

Expand All @@ -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 <project>/<repository>/<reference>`,
Example: `harbor artifact tags list <project>/<repository>@<reference>`,
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
}
Expand All @@ -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 <project>/<repository>/<reference> <tag>`,
Example: `harbor artifact tags delete <project>/<repository>@<reference> <tag>`,
Run: func(cmd *cobra.Command, args []string) {
var err error

Expand Down
25 changes: 12 additions & 13 deletions pkg/api/artifact_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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.
Expand All @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions pkg/views/artifact/tags/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package list
import (
"fmt"
"os"
"strconv"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -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,
Expand Down

0 comments on commit c5a7fd1

Please sign in to comment.