Skip to content

Commit

Permalink
feat: add handler function for the commands
Browse files Browse the repository at this point in the history
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
  • Loading branch information
amands98 committed May 26, 2024
1 parent a502930 commit 887d550
Show file tree
Hide file tree
Showing 30 changed files with 637 additions and 520 deletions.
3 changes: 1 addition & 2 deletions cmd/harbor/root/artifact/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package artifact

import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact/tags"
"github.com/spf13/cobra"
)

Expand All @@ -19,7 +18,7 @@ func Artifact() *cobra.Command {
InfoArtifactCommmand(),
DeleteArtifactCommand(),
ScanArtifactCommand(),
tags.ArtifactTagsCmd(),
ArtifactTagsCmd(),
)

return cmd
Expand Down
26 changes: 3 additions & 23 deletions cmd/harbor/root/artifact/delete.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package artifact

import (
"context"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func DeleteArtifactCommand() *cobra.Command {

cmd := &cobra.Command{
Use: "delete",
Short: "delete an artifact",
Expand All @@ -21,12 +17,12 @@ func DeleteArtifactCommand() *cobra.Command {

if len(args) > 0 {
projectName, repoName, reference := utils.ParseProjectRepoReference(args[0])
err = runDeleteArtifact(projectName, repoName, reference)
err = api.RunDeleteArtifact(projectName, repoName, reference)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
reference := utils.GetReferenceFromUser(repoName, projectName)
err = runDeleteArtifact(projectName, repoName, reference)
err = api.RunDeleteArtifact(projectName, repoName, reference)
}

if err != nil {
Expand All @@ -37,19 +33,3 @@ func DeleteArtifactCommand() *cobra.Command {

return cmd
}

func runDeleteArtifact(projectName, repoName, reference string) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()

_, err := client.Artifact.DeleteArtifact(ctx, &artifact.DeleteArtifactParams{ProjectName: projectName, RepositoryName: repoName, Reference: reference})

if err != nil {
return err
}

log.Infof("Artifact deleted successfully")

return nil
}
25 changes: 3 additions & 22 deletions cmd/harbor/root/artifact/info.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package artifact

import (
"context"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func InfoArtifactCommmand() *cobra.Command {
Expand All @@ -22,12 +19,12 @@ func InfoArtifactCommmand() *cobra.Command {

if len(args) > 0 {
projectName, repoName, reference := utils.ParseProjectRepoReference(args[0])
err = runInfoArtifact(projectName, repoName, reference)
err = api.RunInfoArtifact(projectName, repoName, reference)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
reference := utils.GetReferenceFromUser(repoName, projectName)
err = runInfoArtifact(projectName, repoName, reference)
err = api.RunInfoArtifact(projectName, repoName, reference)
}

if err != nil {
Expand All @@ -39,19 +36,3 @@ func InfoArtifactCommmand() *cobra.Command {

return cmd
}

func runInfoArtifact(projectName, repoName, reference string) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()

response, err := client.Artifact.GetArtifact(ctx, &artifact.GetArtifactParams{ProjectName: projectName, RepositoryName: repoName, Reference: reference})

if err != nil {
return err
}

utils.PrintPayloadInJSONFormat(response.Payload)

return nil
}
27 changes: 3 additions & 24 deletions cmd/harbor/root/artifact/list.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package artifact

import (
"context"
"fmt"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ListArtifactCommand() *cobra.Command {
Expand All @@ -21,11 +17,11 @@ func ListArtifactCommand() *cobra.Command {

if len(args) > 0 {
projectName, repoName := utils.ParseProjectRepo(args[0])
err = runListArtifact(projectName, repoName)
err = api.RunListArtifact(projectName, repoName)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
err = runListArtifact(projectName, repoName)
err = api.RunListArtifact(projectName, repoName)
}

if err != nil {
Expand All @@ -37,20 +33,3 @@ func ListArtifactCommand() *cobra.Command {

return cmd
}

func runListArtifact(projectName, repoName string) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()

response, err := client.Artifact.ListArtifacts(ctx, &artifact.ListArtifactsParams{ProjectName: projectName, RepositoryName: repoName})

if err != nil {
return err
}

fmt.Println(response.Payload)

return nil

}
45 changes: 5 additions & 40 deletions cmd/harbor/root/artifact/scan.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package artifact

import (
"context"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/scan"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ScanArtifactCommand() *cobra.Command {
Expand Down Expand Up @@ -38,12 +35,12 @@ func StartScanArtifactCommand() *cobra.Command {

if len(args) > 0 {
projectName, repoName, reference := utils.ParseProjectRepoReference(args[0])
err = runStartScanArtifact(projectName, repoName, reference)
err = api.StartScanArtifact(projectName, repoName, reference)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
reference := utils.GetReferenceFromUser(repoName, projectName)
err = runStartScanArtifact(projectName, repoName, reference)
err = api.StartScanArtifact(projectName, repoName, reference)
}

if err != nil {
Expand All @@ -54,38 +51,6 @@ func StartScanArtifactCommand() *cobra.Command {
return cmd
}

func runStartScanArtifact(projectName, repoName, reference string) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()

_, err := client.Scan.ScanArtifact(ctx, &scan.ScanArtifactParams{ProjectName: projectName, RepositoryName: repoName, Reference: reference})

if err != nil {
return err
}

log.Infof("Scan started successfully")

return nil
}

func runStopScanArtifact(projectName, repoName, reference string) error {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()

_, err := client.Scan.StopScanArtifact(ctx, &scan.StopScanArtifactParams{ProjectName: projectName, RepositoryName: repoName, Reference: reference})

if err != nil {
return err
}

log.Infof("Scan stopped successfully")

return nil
}

func StopScanArtifactCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "stop",
Expand All @@ -97,12 +62,12 @@ func StopScanArtifactCommand() *cobra.Command {

if len(args) > 0 {
projectName, repoName, reference := utils.ParseProjectRepoReference(args[0])
err = runStopScanArtifact(projectName, repoName, reference)
err = api.StopScanArtifact(projectName, repoName, reference)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
reference := utils.GetReferenceFromUser(repoName, projectName)
err = runStopScanArtifact(projectName, repoName, reference)
err = api.StopScanArtifact(projectName, repoName, reference)
}

if err != nil {
Expand Down
77 changes: 77 additions & 0 deletions cmd/harbor/root/artifact/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package artifact

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func ArtifactTagsCmd() *cobra.Command {

cmd := &cobra.Command{
Use: "tags",
Short: "Manage tags of an artifact",
Example: ` harbor artifact tags list <project>/<repository>/<reference>`,
}

cmd.AddCommand(
ListTagsCommand(),
DeleteTagsCmd(),
)

return cmd
}

func ListTagsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List tags of an artifact",
Example: `harbor artifact tags list <project>/<repository>/<reference>`,
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
projectName, repoName, reference := utils.ParseProjectRepoReference(args[0])
err = api.ListTags(projectName, repoName, reference)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
reference := utils.GetReferenceFromUser(repoName, projectName)
err = api.ListTags(projectName, repoName, reference)
}
if err != nil {
log.Errorf("failed to list tags of an artifact: %v", err)
}

},
}

return cmd
}

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>`,
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
projectName, repoName, reference := utils.ParseProjectRepoReference(args[0])
tag := args[1]
err = api.DeleteTag(projectName, repoName, reference, tag)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
reference := utils.GetReferenceFromUser(repoName, projectName)
tag := utils.GetTagFromUser(repoName, projectName, reference)
err = api.DeleteTag(projectName, repoName, reference, tag)
}
if err != nil {
log.Errorf("failed to delete a tag of an artifact: %v", err)
}
},
}

return cmd
}
18 changes: 0 additions & 18 deletions cmd/harbor/root/artifact/tags/cmd.go

This file was deleted.

1 change: 0 additions & 1 deletion cmd/harbor/root/artifact/tags/create.go

This file was deleted.

Loading

0 comments on commit 887d550

Please sign in to comment.