diff --git a/cmd/harbor/root/artifact/cmd.go b/cmd/harbor/root/artifact/cmd.go index 8c33e05b..9ae56ba1 100644 --- a/cmd/harbor/root/artifact/cmd.go +++ b/cmd/harbor/root/artifact/cmd.go @@ -1,7 +1,6 @@ package artifact import ( - "github.com/goharbor/harbor-cli/cmd/harbor/root/artifact/tags" "github.com/spf13/cobra" ) @@ -19,7 +18,7 @@ func Artifact() *cobra.Command { InfoArtifactCommmand(), DeleteArtifactCommand(), ScanArtifactCommand(), - tags.ArtifactTagsCmd(), + ArtifactTagsCmd(), ) return cmd diff --git a/cmd/harbor/root/artifact/delete.go b/cmd/harbor/root/artifact/delete.go index 439eeb77..014d3f2c 100644 --- a/cmd/harbor/root/artifact/delete.go +++ b/cmd/harbor/root/artifact/delete.go @@ -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", @@ -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 { @@ -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 -} diff --git a/cmd/harbor/root/artifact/info.go b/cmd/harbor/root/artifact/info.go index 2a2c6a79..bf5cf715 100644 --- a/cmd/harbor/root/artifact/info.go +++ b/cmd/harbor/root/artifact/info.go @@ -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 { @@ -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 { @@ -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 -} diff --git a/cmd/harbor/root/artifact/list.go b/cmd/harbor/root/artifact/list.go index 41c37c3a..051ba544 100644 --- a/cmd/harbor/root/artifact/list.go +++ b/cmd/harbor/root/artifact/list.go @@ -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 { @@ -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 { @@ -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 - -} diff --git a/cmd/harbor/root/artifact/scan.go b/cmd/harbor/root/artifact/scan.go index d869a0a9..44d4412b 100644 --- a/cmd/harbor/root/artifact/scan.go +++ b/cmd/harbor/root/artifact/scan.go @@ -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 { @@ -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 { @@ -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", @@ -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 { diff --git a/cmd/harbor/root/artifact/tags.go b/cmd/harbor/root/artifact/tags.go new file mode 100644 index 00000000..8136ce2d --- /dev/null +++ b/cmd/harbor/root/artifact/tags.go @@ -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 //`, + } + + 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 //`, + 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 // `, + 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 +} diff --git a/cmd/harbor/root/artifact/tags/cmd.go b/cmd/harbor/root/artifact/tags/cmd.go deleted file mode 100644 index e68cd611..00000000 --- a/cmd/harbor/root/artifact/tags/cmd.go +++ /dev/null @@ -1,18 +0,0 @@ -package tags - -import "github.com/spf13/cobra" - -func ArtifactTagsCmd() *cobra.Command { - - cmd := &cobra.Command{ - Use: "tags", - Short: "Manage tags of an artifact", - Example: ` harbor artifact tags list //`, - } - - cmd.AddCommand( - ListTagsCommand(), - ) - - return cmd -} diff --git a/cmd/harbor/root/artifact/tags/create.go b/cmd/harbor/root/artifact/tags/create.go deleted file mode 100644 index 2eee9618..00000000 --- a/cmd/harbor/root/artifact/tags/create.go +++ /dev/null @@ -1 +0,0 @@ -package tags diff --git a/cmd/harbor/root/artifact/tags/delete.go b/cmd/harbor/root/artifact/tags/delete.go deleted file mode 100644 index 3f8eed6f..00000000 --- a/cmd/harbor/root/artifact/tags/delete.go +++ /dev/null @@ -1,52 +0,0 @@ -package tags - -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 DeleteTagsCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "delete", - Short: "Delete a tag of an artifact", - Example: `harbor artifact tags delete // `, - 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 = runDeleteTags(projectName, repoName, reference, tag) - } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) - reference := utils.GetReferenceFromUser(repoName, projectName) - // tag := utils.GetTagFromUseer(repoName, projectName) - tag := "sa" - err = runDeleteTags(projectName, repoName, reference, tag) - } - if err != nil { - log.Errorf("failed to delete a tag of an artifact: %v", err) - } - }, - } - - return cmd -} - -func runDeleteTags(projectName, repoName, reference, tag string) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - _, err := client.Artifact.DeleteTag(ctx, &artifact.DeleteTagParams{ProjectName: projectName, RepositoryName: repoName, Reference: reference, TagName: tag}) - if err != nil { - return err - } - log.Infof("Tag deleted successfully") - return nil -} diff --git a/cmd/harbor/root/artifact/tags/list.go b/cmd/harbor/root/artifact/tags/list.go deleted file mode 100644 index f43faa8b..00000000 --- a/cmd/harbor/root/artifact/tags/list.go +++ /dev/null @@ -1,51 +0,0 @@ -package tags - -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 ListTagsCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "list", - Short: "List tags of an artifact", - Example: `harbor artifact tags list //`, - Run: func(cmd *cobra.Command, args []string) { - var err error - if len(args) > 0 { - projectName, repoName, reference := utils.ParseProjectRepoReference(args[0]) - err = runListTags(projectName, repoName, reference) - } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) - reference := utils.GetReferenceFromUser(repoName, projectName) - err = runListTags(projectName, repoName, reference) - } - if err != nil { - log.Errorf("failed to list tags of an artifact: %v", err) - } - - }, - } - - return cmd -} - -func runListTags(projectName, repoName, reference string) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - - resp, err := client.Artifact.ListTags(ctx, &artifact.ListTagsParams{ProjectName: projectName, RepositoryName: repoName, Reference: reference}) - if err != nil { - return err - } - - log.Info(resp.Payload) - return nil -} diff --git a/cmd/harbor/root/project/create.go b/cmd/harbor/root/project/create.go index b8ffb3a4..4a126a99 100644 --- a/cmd/harbor/root/project/create.go +++ b/cmd/harbor/root/project/create.go @@ -1,16 +1,10 @@ package project import ( - "context" - "strconv" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" - "github.com/goharbor/go-client/pkg/sdk/v2.0/models" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/views/project/create" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // CreateProjectCommand creates a new `harbor create project` command @@ -32,7 +26,7 @@ func CreateProjectCommand() *cobra.Command { } if opts.ProjectName != "" && opts.RegistryID != "" && opts.StorageLimit != "" { - err = runCreateProject(opts) + err = api.CreateProject(opts) } else { err = createProjectView(createView) } @@ -66,33 +60,6 @@ func createProjectView(createView *create.CreateView) error { create.CreateProjectView(createView) - return runCreateProject(*createView) - -} - -func runCreateProject(opts create.CreateView) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - registryID := new(int64) - *registryID, _ = strconv.ParseInt(opts.RegistryID, 10, 64) - - if !opts.ProxyCache { - registryID = nil - } - - storageLimit, _ := strconv.ParseInt(opts.StorageLimit, 10, 64) - - public := strconv.FormatBool(opts.Public) - - response, err := client.Project.CreateProject(ctx, &project.CreateProjectParams{Project: &models.ProjectReq{ProjectName: opts.ProjectName, RegistryID: registryID, StorageLimit: &storageLimit, Public: &opts.Public, Metadata: &models.ProjectMetadata{Public: public}}}) + return api.CreateProject(*createView) - if err != nil { - return err - } - - if response != nil { - log.Info("Project created successfully") - } - return nil } diff --git a/cmd/harbor/root/project/delete.go b/cmd/harbor/root/project/delete.go index d42930f7..0d51765a 100644 --- a/cmd/harbor/root/project/delete.go +++ b/cmd/harbor/root/project/delete.go @@ -1,13 +1,10 @@ package project import ( - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" + "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" ) // DeleteProjectCommand creates a new `harbor delete project` command @@ -21,10 +18,10 @@ func DeleteProjectCommand() *cobra.Command { var err error if len(args) > 0 { - err = runDeleteProject(args[0]) + err = api.DeleteProject(args[0]) } else { projectName := utils.GetProjectNameFromUser() - err = runDeleteProject(projectName) + err = api.DeleteProject(projectName) } if err != nil { log.Errorf("failed to delete project: %v", err) @@ -34,17 +31,3 @@ func DeleteProjectCommand() *cobra.Command { return cmd } - -func runDeleteProject(projectName string) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - _, err := client.Project.DeleteProject(ctx, &project.DeleteProjectParams{ProjectNameOrID: projectName}) - - if err != nil { - return err - } - - log.Info("project deleted successfully") - return nil -} diff --git a/cmd/harbor/root/project/list.go b/cmd/harbor/root/project/list.go index e7c5a141..e985ab8a 100644 --- a/cmd/harbor/root/project/list.go +++ b/cmd/harbor/root/project/list.go @@ -1,36 +1,24 @@ package project import ( - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" list "github.com/goharbor/harbor-cli/pkg/views/project/list" + plist "github.com/goharbor/harbor-cli/pkg/views/project/list" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" ) -type listProjectOptions struct { - name string - owner string - page int64 - pageSize int64 - public bool - q string - sort string - withDetail bool -} - // NewListProjectCommand creates a new `harbor list project` command func ListProjectCommand() *cobra.Command { - var opts listProjectOptions + var opts plist.ListProjectOptions cmd := &cobra.Command{ Use: "list", Short: "list project", Run: func(cmd *cobra.Command, args []string) { - projects, err := RunListProject(opts) + projects, err := api.ListProject(opts) if err != nil { log.Fatalf("failed to get projects list: %v", err) } @@ -45,25 +33,14 @@ func ListProjectCommand() *cobra.Command { } flags := cmd.Flags() - flags.StringVarP(&opts.name, "name", "", "", "Name of the project") - flags.StringVarP(&opts.owner, "owner", "", "", "Name of the project owner") - flags.Int64VarP(&opts.page, "page", "", 1, "Page number") - flags.Int64VarP(&opts.pageSize, "page-size", "", 10, "Size of per page") - flags.BoolVarP(&opts.public, "public", "", true, "Project is public or private") - 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") - flags.BoolVarP(&opts.withDetail, "with-detail", "", true, "Bool value indicating whether return detailed information of the project") + flags.StringVarP(&opts.Name, "name", "", "", "Name of the project") + flags.StringVarP(&opts.Owner, "owner", "", "", "Name of the project owner") + flags.Int64VarP(&opts.Page, "page", "", 1, "Page number") + flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page") + flags.BoolVarP(&opts.Public, "public", "", true, "Project is public or private") + 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") + flags.BoolVarP(&opts.WithDetail, "with-detail", "", true, "Bool value indicating whether return detailed information of the project") return cmd } - -func RunListProject(opts listProjectOptions) (*project.ListProjectsOK, error) { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{Name: &opts.name, Owner: &opts.owner, Page: &opts.page, PageSize: &opts.pageSize, Public: &opts.public, Q: &opts.q, Sort: &opts.sort, WithDetail: &opts.withDetail}) - if err != nil { - return nil, err - } - return response, nil -} diff --git a/cmd/harbor/root/project/logs.go b/cmd/harbor/root/project/logs.go index 45e1edc5..001c1a58 100644 --- a/cmd/harbor/root/project/logs.go +++ b/cmd/harbor/root/project/logs.go @@ -1,9 +1,8 @@ package project import ( - "context" - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" auditLog "github.com/goharbor/harbor-cli/pkg/views/project/logs" log "github.com/sirupsen/logrus" @@ -21,10 +20,10 @@ func LogsProjectCommmand() *cobra.Command { var err error var resp *project.GetLogsOK if len(args) > 0 { - resp, err = runLogsProject(args[0]) + resp, err = api.LogsProject(args[0]) } else { projectName := utils.GetProjectNameFromUser() - resp, err = runLogsProject(projectName) + resp, err = api.LogsProject(projectName) } if err != nil { @@ -43,19 +42,3 @@ func LogsProjectCommmand() *cobra.Command { return cmd } - -func runLogsProject(projectName string) (*project.GetLogsOK, error) { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Project.GetLogs(ctx, &project.GetLogsParams{ - ProjectName: projectName, - Context: ctx, - }) - - if err != nil { - return nil, err - } - - return response, nil -} diff --git a/cmd/harbor/root/project/view.go b/cmd/harbor/root/project/view.go index 530947da..de52ef63 100644 --- a/cmd/harbor/root/project/view.go +++ b/cmd/harbor/root/project/view.go @@ -1,45 +1,35 @@ package project import ( - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" + "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" ) -type getProjectOptions struct { - projectNameOrID string -} - // GetProjectCommand creates a new `harbor get project` command func ViewCommand() *cobra.Command { - var opts getProjectOptions cmd := &cobra.Command{ Use: "view [NAME|ID]", Short: "get project by name or id", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - opts.projectNameOrID = args[0] - return runGetProject(opts) - }, - } + Args: cobra.MaximumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + var err error - return cmd -} + if len(args) > 0 { + err = api.GetProject(args[0]) + } else { + projectName := utils.GetProjectNameFromUser() + err = api.GetProject(projectName) + } -func runGetProject(opts getProjectOptions) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Project.GetProject(ctx, &project.GetProjectParams{ProjectNameOrID: opts.projectNameOrID}) + if err != nil { + log.Errorf("failed to get project: %v", err) + } - if err != nil { - return err + }, } - utils.PrintPayloadInJSONFormat(response) - return nil + return cmd } diff --git a/cmd/harbor/root/registry/create.go b/cmd/harbor/root/registry/create.go index babcdba5..7da4484e 100644 --- a/cmd/harbor/root/registry/create.go +++ b/cmd/harbor/root/registry/create.go @@ -1,15 +1,10 @@ package registry import ( - "context" - - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry" - "github.com/goharbor/go-client/pkg/sdk/v2.0/models" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/views/registry/create" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // NewCreateRegistryCommand creates a new `harbor create registry` command @@ -36,7 +31,7 @@ func CreateRegistryCommand() *cobra.Command { } if opts.Name != "" && opts.Type != "" && opts.URL != "" { - err = runCreateRegistry(opts) + err = api.CreateRegistry(opts) } else { err = createRegistryView(createView) } @@ -67,19 +62,5 @@ func createRegistryView(createView *create.CreateView) error { } create.CreateRegistryView(createView) - return runCreateRegistry(*createView) -} - -func runCreateRegistry(opts create.CreateView) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - _, err := client.Registry.CreateRegistry(ctx, ®istry.CreateRegistryParams{Registry: &models.Registry{Credential: &models.RegistryCredential{AccessKey: opts.Credential.AccessKey, AccessSecret: opts.Credential.AccessSecret, Type: opts.Credential.Type}, Description: opts.Description, Insecure: opts.Insecure, Name: opts.Name, Type: opts.Type, URL: opts.URL}}) - - if err != nil { - return err - } - - log.Infof("Registry %s created", opts.Name) - return nil + return api.CreateRegistry(*createView) } diff --git a/cmd/harbor/root/registry/delete.go b/cmd/harbor/root/registry/delete.go index 01f1188d..fee3f96a 100644 --- a/cmd/harbor/root/registry/delete.go +++ b/cmd/harbor/root/registry/delete.go @@ -1,14 +1,12 @@ package registry import ( - "context" "strconv" - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry" + "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" ) // NewDeleteRegistryCommand creates a new `harbor delete registry` command @@ -23,10 +21,10 @@ func DeleteRegistryCommand() *cobra.Command { if len(args) > 0 { registryId, _ := strconv.ParseInt(args[0], 10, 64) - err = runDeleteRegistry(registryId) + err = api.DeleteRegistry(registryId) } else { registryId := utils.GetRegistryNameFromUser() - err = runDeleteRegistry(registryId) + err = api.DeleteRegistry(registryId) } if err != nil { log.Errorf("failed to delete registry: %v", err) @@ -36,18 +34,3 @@ func DeleteRegistryCommand() *cobra.Command { return cmd } - -func runDeleteRegistry(registryName int64) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - _, err := client.Registry.DeleteRegistry(ctx, ®istry.DeleteRegistryParams{ID: registryName}) - - if err != nil { - return err - } - - log.Info("registry deleted successfully") - - return nil -} diff --git a/cmd/harbor/root/registry/info.go b/cmd/harbor/root/registry/info.go index 07ebe130..82898772 100644 --- a/cmd/harbor/root/registry/info.go +++ b/cmd/harbor/root/registry/info.go @@ -1,14 +1,12 @@ package registry import ( - "context" "strconv" - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry" + "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 InfoRegistryCommand() *cobra.Command { @@ -21,10 +19,10 @@ func InfoRegistryCommand() *cobra.Command { if len(args) > 0 { registryId, _ := strconv.ParseInt(args[0], 10, 64) - err = runInfoRegistry(registryId) + err = api.InfoRegistry(registryId) } else { registryId := utils.GetRegistryNameFromUser() - err = runInfoRegistry(registryId) + err = api.InfoRegistry(registryId) } if err != nil { log.Errorf("failed to get registry info: %v", err) @@ -35,17 +33,3 @@ func InfoRegistryCommand() *cobra.Command { return cmd } - -func runInfoRegistry(registryId int64) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Registry.GetRegistry(ctx, ®istry.GetRegistryParams{ID: registryId}) - - if err != nil { - return err - } - - utils.PrintPayloadInJSONFormat(response.Payload) - return nil -} diff --git a/cmd/harbor/root/registry/update.go b/cmd/harbor/root/registry/update.go index 45b3ecdd..c681663c 100644 --- a/cmd/harbor/root/registry/update.go +++ b/cmd/harbor/root/registry/update.go @@ -1,16 +1,13 @@ package registry import ( - "context" "strconv" - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry" - "github.com/goharbor/go-client/pkg/sdk/v2.0/models" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" "github.com/goharbor/harbor-cli/pkg/views/registry/create" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // NewUpdateRegistryCommand creates a new `harbor update registry` command @@ -49,7 +46,7 @@ func UpdateRegistryCommand() *cobra.Command { } if opts.Name != "" && opts.Type != "" && opts.URL != "" { - err = runUpdateRegistry(updateView, registryId) + err = api.UpdateRegistry(updateView, registryId) } else { err = updateRegistryView(updateView, registryId) } @@ -79,30 +76,5 @@ func updateRegistryView(updateView *create.CreateView, projectID int64) error { } create.CreateRegistryView(updateView) - return runUpdateRegistry(updateView, projectID) -} - -func runUpdateRegistry(updateView *create.CreateView, projectID int64) error { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - registryUpdate := &models.RegistryUpdate{ - Name: &updateView.Name, - Description: &updateView.Description, - URL: &updateView.URL, - AccessKey: &updateView.Credential.AccessKey, - AccessSecret: &updateView.Credential.AccessSecret, - CredentialType: &updateView.Credential.Type, - Insecure: &updateView.Insecure, - } - - _, err := client.Registry.UpdateRegistry(ctx, ®istry.UpdateRegistryParams{ID: projectID, Registry: registryUpdate}) - - if err != nil { - return err - } - - log.Info("registry updated successfully") - - return nil + return api.UpdateRegistry(updateView, projectID) } diff --git a/cmd/harbor/root/registry/view.go b/cmd/harbor/root/registry/view.go index dbe8c3be..bf44b991 100644 --- a/cmd/harbor/root/registry/view.go +++ b/cmd/harbor/root/registry/view.go @@ -1,56 +1,39 @@ package registry import ( - "context" - "fmt" "strconv" - "github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry" - "github.com/goharbor/harbor-cli/pkg/constants" + "github.com/goharbor/harbor-cli/pkg/api" "github.com/goharbor/harbor-cli/pkg/utils" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" ) -type getRegistryOptions struct { - id int64 -} - // NewGetRegistryCommand creates a new `harbor get registry` command func ViewCommand() *cobra.Command { - var opts getRegistryOptions - cmd := &cobra.Command{ Use: "view", Short: "get registry by id", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - id, err := strconv.Atoi(args[0]) - if err != nil { - fmt.Printf("Invalid argument: %s. Expected an integer.\n", args[0]) - return err + Args: cobra.MaximumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + var err error + if len(args) > 0 { + registryId, err := strconv.ParseInt(args[0], 10, 64) + if err != nil { + log.Errorf("failed to parse registry id: %v", err) + } + err = api.GetRegistry(registryId) + } else { + registryId := utils.GetRegistryNameFromUser() + err = api.GetRegistry(registryId) } - opts.id = int64(id) - credentialName, err := cmd.Flags().GetString(constants.CredentialNameOption) if err != nil { - return err + log.Errorf("failed to get registry: %v", err) } - return runGetRegistry(opts, credentialName) }, } return cmd } - -func runGetRegistry(opts getRegistryOptions, credentialName string) error { - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Registry.GetRegistry(ctx, ®istry.GetRegistryParams{ID: opts.id}) - - if err != nil { - return err - } - - utils.PrintPayloadInJSONFormat(response.GetPayload()) - return nil -} diff --git a/pkg/api/artifact_handler.go b/pkg/api/artifact_handler.go new file mode 100644 index 00000000..eacdfd8b --- /dev/null +++ b/pkg/api/artifact_handler.go @@ -0,0 +1,155 @@ +package api + +import ( + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact" + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/scan" + "github.com/goharbor/harbor-cli/pkg/utils" + log "github.com/sirupsen/logrus" +) + +// RunDeleteArtifact handles the deletion of an artifact. +func RunDeleteArtifact(projectName, repoName, reference string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + _, err = client.Artifact.DeleteArtifact(ctx, &artifact.DeleteArtifactParams{ + ProjectName: projectName, + RepositoryName: repoName, + Reference: reference, + }) + if err != nil { + log.Errorf("Failed to delete artifact: %v", err) + return err + } + + log.Infof("Artifact deleted successfully: %s/%s@%s", projectName, repoName, reference) + return nil +} + +// RunInfoArtifact retrieves information about a specific artifact. +func RunInfoArtifact(projectName, repoName, reference string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + response, err := client.Artifact.GetArtifact(ctx, &artifact.GetArtifactParams{ + ProjectName: projectName, + RepositoryName: repoName, + Reference: reference, + }) + if err != nil { + log.Errorf("Failed to get artifact info: %v", err) + return err + } + + utils.PrintPayloadInJSONFormat(response.Payload) + return nil +} + +// RunListArtifact lists all artifacts in a repository. +func RunListArtifact(projectName, repoName string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + response, err := client.Artifact.ListArtifacts(ctx, &artifact.ListArtifactsParams{ + ProjectName: projectName, + RepositoryName: repoName, + }) + if err != nil { + log.Errorf("Failed to list artifacts: %v", err) + return err + } + + log.Info(response.Payload) + return nil +} + +// StartScanArtifact initiates a scan on a specific artifact. +func StartScanArtifact(projectName, repoName, reference string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + _, err = client.Scan.ScanArtifact(ctx, &scan.ScanArtifactParams{ + ProjectName: projectName, + RepositoryName: repoName, + Reference: reference, + }) + if err != nil { + log.Errorf("Failed to start scan: %v", err) + return err + } + + log.Infof("Scan started successfully: %s/%s@%s", projectName, repoName, reference) + return nil +} + +// StopScanArtifact stops a scan on a specific artifact. +func StopScanArtifact(projectName, repoName, reference string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + _, err = client.Scan.StopScanArtifact(ctx, &scan.StopScanArtifactParams{ + ProjectName: projectName, + RepositoryName: repoName, + Reference: reference, + }) + if err != nil { + log.Errorf("Failed to stop scan: %v", err) + return err + } + + log.Infof("Scan stopped successfully: %s/%s@%s", projectName, repoName, reference) + return nil +} + +// DeleteTag deletes a tag from a specific artifact. +func DeleteTag(projectName, repoName, reference, tag string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + _, err = client.Artifact.DeleteTag(ctx, &artifact.DeleteTagParams{ + ProjectName: projectName, + RepositoryName: repoName, + Reference: reference, + TagName: tag, + }) + if err != nil { + log.Errorf("Failed to delete tag: %v", err) + return err + } + + log.Infof("Tag deleted successfully: %s/%s@%s:%s", projectName, repoName, reference, tag) + return nil +} + +// ListTags lists all tags of a specific artifact. +func ListTags(projectName, repoName, reference string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + resp, err := client.Artifact.ListTags(ctx, &artifact.ListTagsParams{ + ProjectName: projectName, + RepositoryName: repoName, + Reference: reference, + }) + if err != nil { + log.Errorf("Failed to list tags: %v", err) + return err + } + + log.Info(resp.Payload) + return nil +} diff --git a/pkg/api/project_handler.go b/pkg/api/project_handler.go new file mode 100644 index 00000000..716d7d9c --- /dev/null +++ b/pkg/api/project_handler.go @@ -0,0 +1,102 @@ +package api + +import ( + "strconv" + + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" + "github.com/goharbor/go-client/pkg/sdk/v2.0/models" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/views/project/create" + plist "github.com/goharbor/harbor-cli/pkg/views/project/list" + log "github.com/sirupsen/logrus" +) + +func CreateProject(opts create.CreateView) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + registryID := new(int64) + *registryID, _ = strconv.ParseInt(opts.RegistryID, 10, 64) + + if !opts.ProxyCache { + registryID = nil + } + + storageLimit, _ := strconv.ParseInt(opts.StorageLimit, 10, 64) + + public := strconv.FormatBool(opts.Public) + + response, err := client.Project.CreateProject(ctx, &project.CreateProjectParams{Project: &models.ProjectReq{ProjectName: opts.ProjectName, RegistryID: registryID, StorageLimit: &storageLimit, Public: &opts.Public, Metadata: &models.ProjectMetadata{Public: public}}}) + + if err != nil { + return err + } + + if response != nil { + log.Info("Project created successfully") + } + return nil +} + +func GetProject(projectName string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + response, err := client.Project.GetProject(ctx, &project.GetProjectParams{ProjectNameOrID: projectName}) + + if err != nil { + return err + } + + utils.PrintPayloadInJSONFormat(response) + return nil +} + +func DeleteProject(projectName string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + _, err = client.Project.DeleteProject(ctx, &project.DeleteProjectParams{ProjectNameOrID: projectName}) + + if err != nil { + return err + } + + log.Info("project deleted successfully") + return nil +} + +func ListProject(opts plist.ListProjectOptions) (*project.ListProjectsOK, error) { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return nil, err + } + response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{Name: &opts.Name, Owner: &opts.Owner, Page: &opts.Page, PageSize: &opts.PageSize, Public: &opts.Public, Q: &opts.Q, Sort: &opts.Sort, WithDetail: &opts.WithDetail}) + if err != nil { + return nil, err + } + return response, nil +} + +func LogsProject(projectName string) (*project.GetLogsOK, error) { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return nil, err + } + + response, err := client.Project.GetLogs(ctx, &project.GetLogsParams{ + ProjectName: projectName, + Context: ctx, + }) + + if err != nil { + return nil, err + } + + return response, nil +} diff --git a/pkg/api/registry_handler.go b/pkg/api/registry_handler.go new file mode 100644 index 00000000..8b34447f --- /dev/null +++ b/pkg/api/registry_handler.go @@ -0,0 +1,98 @@ +package api + +import ( + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry" + "github.com/goharbor/go-client/pkg/sdk/v2.0/models" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/views/registry/create" + log "github.com/sirupsen/logrus" +) + +func CreateRegistry(opts create.CreateView) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + _, err = client.Registry.CreateRegistry(ctx, ®istry.CreateRegistryParams{Registry: &models.Registry{Credential: &models.RegistryCredential{AccessKey: opts.Credential.AccessKey, AccessSecret: opts.Credential.AccessSecret, Type: opts.Credential.Type}, Description: opts.Description, Insecure: opts.Insecure, Name: opts.Name, Type: opts.Type, URL: opts.URL}}) + + if err != nil { + return err + } + + log.Infof("Registry %s created", opts.Name) + return nil +} + +func DeleteRegistry(registryName int64) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + _, err = client.Registry.DeleteRegistry(ctx, ®istry.DeleteRegistryParams{ID: registryName}) + + if err != nil { + return err + } + + log.Info("registry deleted successfully") + + return nil +} + +func InfoRegistry(registryId int64) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + + response, err := client.Registry.GetRegistry(ctx, ®istry.GetRegistryParams{ID: registryId}) + + if err != nil { + return err + } + + utils.PrintPayloadInJSONFormat(response.Payload) + return nil +} + +func GetRegistry(registryId int64) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + response, err := client.Registry.GetRegistry(ctx, ®istry.GetRegistryParams{ID: registryId}) + + if err != nil { + return err + } + + utils.PrintPayloadInJSONFormat(response.GetPayload()) + return nil +} + +func UpdateRegistry(updateView *create.CreateView, projectID int64) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + registryUpdate := &models.RegistryUpdate{ + Name: &updateView.Name, + Description: &updateView.Description, + URL: &updateView.URL, + AccessKey: &updateView.Credential.AccessKey, + AccessSecret: &updateView.Credential.AccessSecret, + CredentialType: &updateView.Credential.Type, + Insecure: &updateView.Insecure, + } + + _, err = client.Registry.UpdateRegistry(ctx, ®istry.UpdateRegistryParams{ID: projectID, Registry: registryUpdate}) + + if err != nil { + return err + } + + log.Info("registry updated successfully") + + return nil +} diff --git a/pkg/api/replication_handler.go b/pkg/api/replication_handler.go new file mode 100644 index 00000000..778f64ec --- /dev/null +++ b/pkg/api/replication_handler.go @@ -0,0 +1 @@ +package api diff --git a/pkg/api/repository_handler.go b/pkg/api/repository_handler.go new file mode 100644 index 00000000..778f64ec --- /dev/null +++ b/pkg/api/repository_handler.go @@ -0,0 +1 @@ +package api diff --git a/pkg/api/user_handler.go b/pkg/api/user_handler.go new file mode 100644 index 00000000..778f64ec --- /dev/null +++ b/pkg/api/user_handler.go @@ -0,0 +1 @@ +package api diff --git a/pkg/utils/client.go b/pkg/utils/client.go new file mode 100644 index 00000000..4da1e0aa --- /dev/null +++ b/pkg/utils/client.go @@ -0,0 +1,62 @@ +package utils + +import ( + "context" + "fmt" + "os" + "sync" + + "github.com/goharbor/go-client/pkg/harbor" + v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client" + log "github.com/sirupsen/logrus" + "github.com/spf13/viper" +) + +var ( + clientInstance *v2client.HarborAPI + clientOnce sync.Once + clientErr error +) + +func GetClient() (*v2client.HarborAPI, error) { + clientOnce.Do(func() { + credentialName := viper.GetString("current-credential-name") + clientInstance = GetClientByCredentialName(credentialName) + if clientErr != nil { + log.Errorf("failed to initialize client: %v", clientErr) + } + }) + return clientInstance, clientErr +} + +func ContextWithClient() (context.Context, *v2client.HarborAPI, error) { + client, err := GetClient() + if err != nil { + return nil, nil, err + } + ctx := context.Background() + return ctx, client, nil +} + +func GetClientByConfig(clientConfig *harbor.ClientSetConfig) *v2client.HarborAPI { + cs, err := harbor.NewClientSet(clientConfig) + if err != nil { + panic(err) + } + return cs.V2() +} + +// Returns Harbor v2 client after resolving the credential name +func GetClientByCredentialName(credentialName string) *v2client.HarborAPI { + credential, err := GetCredentials(credentialName) + if err != nil { + fmt.Print(err) + os.Exit(1) + } + clientConfig := &harbor.ClientSetConfig{ + URL: credential.ServerAddress, + Username: credential.Username, + Password: credential.Password, + } + return GetClientByConfig(clientConfig) +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 0827c505..906f78be 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -4,17 +4,15 @@ import ( "context" "encoding/json" "fmt" - "os" "strings" - "github.com/goharbor/go-client/pkg/harbor" - v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client" "github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact" "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" "github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry" "github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository" "github.com/goharbor/go-client/pkg/sdk/v2.0/client/user" aview "github.com/goharbor/harbor-cli/pkg/views/artifact/select" + tview "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/select" pview "github.com/goharbor/harbor-cli/pkg/views/project/select" rview "github.com/goharbor/harbor-cli/pkg/views/registry/select" repoView "github.com/goharbor/harbor-cli/pkg/views/repository/select" @@ -24,28 +22,6 @@ import ( ) // Returns Harbor v2 client for given clientConfig -func GetClientByConfig(clientConfig *harbor.ClientSetConfig) *v2client.HarborAPI { - cs, err := harbor.NewClientSet(clientConfig) - if err != nil { - panic(err) - } - return cs.V2() -} - -// Returns Harbor v2 client after resolving the credential name -func GetClientByCredentialName(credentialName string) *v2client.HarborAPI { - credential, err := GetCredentials(credentialName) - if err != nil { - fmt.Print(err) - os.Exit(1) - } - clientConfig := &harbor.ClientSetConfig{ - URL: credential.ServerAddress, - Username: credential.Username, - Password: credential.Password, - } - return GetClientByConfig(clientConfig) -} func PrintPayloadInJSONFormat(payload any) { if payload == nil { @@ -161,3 +137,18 @@ func GetUserIdFromUser() int64 { return <-userId } + +func GetTagFromUser(repoName, projectName, reference string) string { + tag := make(chan string) + go func() { + credentialName := viper.GetString("current-credential-name") + client := GetClientByCredentialName(credentialName) + ctx := context.Background() + response, err := client.Artifact.ListTags(ctx, &artifact.ListTagsParams{ProjectName: projectName, RepositoryName: repoName, Reference: reference}) + if err != nil { + log.Fatal(err) + } + tview.ListTags(response.Payload, tag) + }() + return <-tag +} diff --git a/pkg/views/artifact/tags/select/view.go b/pkg/views/artifact/tags/select/view.go new file mode 100644 index 00000000..c20bedeb --- /dev/null +++ b/pkg/views/artifact/tags/select/view.go @@ -0,0 +1,33 @@ +package registry + +import ( + "fmt" + "os" + + "github.com/charmbracelet/bubbles/list" + tea "github.com/charmbracelet/bubbletea" + "github.com/goharbor/go-client/pkg/sdk/v2.0/models" + "github.com/goharbor/harbor-cli/pkg/views/base/selection" +) + +func ListTags(tag []*models.Tag, choice chan<- string) { + itemsList := make([]list.Item, len(tag)) + + for i, t := range tag { + itemsList[i] = selection.Item(t.Name) + } + + m := selection.NewModel(itemsList, "Tag") + + p, err := tea.NewProgram(m, tea.WithAltScreen()).Run() + + if err != nil { + fmt.Println("Error running program:", err) + os.Exit(1) + } + + if p, ok := p.(selection.Model); ok { + choice <- p.Choice + } + +} diff --git a/pkg/views/project/list/view.go b/pkg/views/project/list/view.go index 0cf10be9..235b0b3c 100644 --- a/pkg/views/project/list/view.go +++ b/pkg/views/project/list/view.go @@ -12,6 +12,17 @@ import ( "github.com/goharbor/harbor-cli/pkg/views/base/tablelist" ) +type ListProjectOptions struct { + Name string + Owner string + Page int64 + PageSize int64 + Public bool + Q string + Sort string + WithDetail bool +} + var columns = []table.Column{ {Title: "Project Name", Width: 12}, {Title: "Access Level", Width: 12},