Skip to content

Commit

Permalink
add artifacts command to harbor - scan, list, delete,info
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 23, 2024
1 parent 381ec2d commit fc8436f
Show file tree
Hide file tree
Showing 16 changed files with 483 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cmd/harbor/root/artifact/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package artifact

import "github.com/spf13/cobra"

func Artifact() *cobra.Command {

cmd := &cobra.Command{
Use: "artifact",
Short: "Manage artifacts",
Long: `Manage artifacts in Harbor Repository`,
Example: ` harbor artifact list`,
}

cmd.AddCommand(
ListArtifactCommand(),
InfoArtifactCommmand(),
DeleteArtifactCommand(),
ScanArtifactCommand(),
T
)

return cmd
}
55 changes: 55 additions & 0 deletions cmd/harbor/root/artifact/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package artifact

import (
"context"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact"
"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",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error

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

if err != nil {
log.Errorf("failed to delete an artifact: %v", err)
}
},
}

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
}
57 changes: 57 additions & 0 deletions cmd/harbor/root/artifact/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package artifact

import (
"context"

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

func InfoArtifactCommmand() *cobra.Command {

cmd := &cobra.Command{
Use: "info",
Short: "Get info of an artifact",
Long: `Get info of an artifact`,
Example: `harbor artifact info <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 = runInfoArtifact(projectName, repoName, reference)
} else {
projectName := utils.GetProjectNameFromUser()
repoName := utils.GetRepoNameFromUser(projectName)
reference := utils.GetReferenceFromUser(repoName, projectName)
err = runInfoArtifact(projectName, repoName, reference)
}

if err != nil {
log.Errorf("failed to get info of an artifact: %v", err)
}

},
}

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
}
56 changes: 56 additions & 0 deletions cmd/harbor/root/artifact/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package artifact

import (
"context"
"fmt"

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

func ListArtifactCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "list artifacts within a repository",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error

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

if err != nil {
log.Errorf("failed to list artifacts: %v", err)
}

},
}

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

}
114 changes: 114 additions & 0 deletions cmd/harbor/root/artifact/scan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package artifact

import (
"context"

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

func ScanArtifactCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "scan",
Short: "Scan an artifact",
Long: `Scan an artifact in Harbor Repository`,
Example: `harbor artifact scan start <project>/<repository>/<reference>`,
}

cmd.AddCommand(
StartScanArtifactCommand(),
StopScanArtifactCommand(),
// LogScanArtifactCommand(),
)

return cmd
}

func StartScanArtifactCommand() *cobra.Command {
cmd := &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>`,
Run: func(cmd *cobra.Command, args []string) {
var err error

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

if err != nil {
log.Errorf("failed to start a scan of an artifact: %v", err)
}
},
}
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",
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>`,
Run: func(cmd *cobra.Command, args []string) {
var err error

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

if err != nil {
log.Errorf("failed to stop a scan of an artifact: %v", err)
}
},
}
return cmd
}
4 changes: 4 additions & 0 deletions cmd/harbor/root/artifact/tags/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tags



1 change: 1 addition & 0 deletions cmd/harbor/root/artifact/tags/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package tags
1 change: 1 addition & 0 deletions cmd/harbor/root/artifact/tags/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package tags
1 change: 1 addition & 0 deletions cmd/harbor/root/artifact/tags/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package tags
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"

"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
Expand Down Expand Up @@ -104,6 +105,7 @@ harbor help
registry.Registry(),
repositry.Repository(),
user.User(),
artifact.Artifact(),
)

return root
Expand Down
1 change: 1 addition & 0 deletions cmd/harbor/root/labels/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package labels
13 changes: 13 additions & 0 deletions cmd/harbor/root/labels/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package labels

import "github.com/spf13/cobra"


func Labels() *cobra.Command {
cmd := &cobra.Command{
Use: "label",
Short: "Manage labels in Harbor",
}

return cmd
}
1 change: 1 addition & 0 deletions cmd/harbor/root/labels/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package labels
18 changes: 18 additions & 0 deletions cmd/harbor/root/replication/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package replication

import (
"github.com/spf13/cobra"
)

func Replication() *cobra.Command {
// replicationCmd represents the replication command.
var replicationCmd = &cobra.Command{
Use: "replication",
Aliases: []string{"repl"},
Short: "",
Long: ``,
}
replicationCmd.AddCommand()

return replicationCmd
}
Loading

0 comments on commit fc8436f

Please sign in to comment.