-
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.
add artifacts command to harbor - scan, list, delete,info
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
- Loading branch information
Showing
16 changed files
with
483 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
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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
|
||
} |
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,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 | ||
} |
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,4 @@ | ||
package tags | ||
|
||
|
||
|
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 @@ | ||
package tags |
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 @@ | ||
package tags |
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 @@ | ||
package tags |
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 @@ | ||
package labels |
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,13 @@ | ||
package labels | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
|
||
func Labels() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "label", | ||
Short: "Manage labels in Harbor", | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package labels |
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,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 | ||
} |
Oops, something went wrong.