From a97d08492e0132c2d4a3111721a44e9f09139ce3 Mon Sep 17 00:00:00 2001 From: amands98 Date: Wed, 29 May 2024 08:40:48 -0400 Subject: [PATCH] feat: manage prompts separately Signed-off-by: amands98 --- cmd/harbor/root/artifact/delete.go | 11 +-- cmd/harbor/root/artifact/info.go | 11 +-- cmd/harbor/root/artifact/list.go | 13 ++- cmd/harbor/root/artifact/scan.go | 30 +++---- cmd/harbor/root/artifact/tags.go | 67 ++++++++++----- cmd/harbor/root/project/delete.go | 4 +- cmd/harbor/root/project/list.go | 7 +- cmd/harbor/root/project/logs.go | 3 +- cmd/harbor/root/project/view.go | 4 +- cmd/harbor/root/registry/delete.go | 4 +- cmd/harbor/root/registry/info.go | 4 +- cmd/harbor/root/registry/list.go | 36 ++------ cmd/harbor/root/registry/update.go | 4 +- cmd/harbor/root/registry/view.go | 4 +- cmd/harbor/root/repository/delete.go | 5 +- cmd/harbor/root/repository/info.go | 5 +- cmd/harbor/root/repository/list.go | 15 +++- cmd/harbor/root/user/delete.go | 4 +- cmd/harbor/root/user/elevate.go | 4 +- pkg/api/artifact_handler.go | 48 ++++++++--- pkg/api/project_handler.go | 15 ++-- pkg/api/registry_handler.go | 27 ++++++ pkg/api/repository_handler.go | 12 ++- pkg/api/types.go | 10 +++ pkg/prompt/prompt.go | 91 ++++++++++++++++++++ pkg/utils/utils.go | 114 ------------------------- pkg/views/artifact/tags/create/view.go | 30 +++++++ pkg/views/project/create/view.go | 7 +- pkg/views/project/list/view.go | 11 --- 29 files changed, 333 insertions(+), 267 deletions(-) create mode 100644 pkg/api/types.go create mode 100644 pkg/prompt/prompt.go create mode 100644 pkg/views/artifact/tags/create/view.go diff --git a/cmd/harbor/root/artifact/delete.go b/cmd/harbor/root/artifact/delete.go index 014d3f2c..d2dc82ea 100644 --- a/cmd/harbor/root/artifact/delete.go +++ b/cmd/harbor/root/artifact/delete.go @@ -2,6 +2,7 @@ package artifact import ( "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -17,12 +18,12 @@ func DeleteArtifactCommand() *cobra.Command { if len(args) > 0 { projectName, repoName, reference := utils.ParseProjectRepoReference(args[0]) - err = api.RunDeleteArtifact(projectName, repoName, reference) + err = api.DeleteArtifact(projectName, repoName, reference) } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) - reference := utils.GetReferenceFromUser(repoName, projectName) - err = api.RunDeleteArtifact(projectName, repoName, reference) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + err = api.DeleteArtifact(projectName, repoName, reference) } if err != nil { diff --git a/cmd/harbor/root/artifact/info.go b/cmd/harbor/root/artifact/info.go index bf5cf715..b215d91a 100644 --- a/cmd/harbor/root/artifact/info.go +++ b/cmd/harbor/root/artifact/info.go @@ -2,6 +2,7 @@ package artifact import ( "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -19,12 +20,12 @@ func InfoArtifactCommmand() *cobra.Command { if len(args) > 0 { projectName, repoName, reference := utils.ParseProjectRepoReference(args[0]) - err = api.RunInfoArtifact(projectName, repoName, reference) + err = api.InfoArtifact(projectName, repoName, reference) } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) - reference := utils.GetReferenceFromUser(repoName, projectName) - err = api.RunInfoArtifact(projectName, repoName, reference) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + err = api.InfoArtifact(projectName, repoName, reference) } if err != nil { diff --git a/cmd/harbor/root/artifact/list.go b/cmd/harbor/root/artifact/list.go index 051ba544..37e928fa 100644 --- a/cmd/harbor/root/artifact/list.go +++ b/cmd/harbor/root/artifact/list.go @@ -1,7 +1,9 @@ package artifact import ( + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact" "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -14,20 +16,23 @@ func ListArtifactCommand() *cobra.Command { Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { var err error + var resp artifact.ListArtifactsOK if len(args) > 0 { projectName, repoName := utils.ParseProjectRepo(args[0]) - err = api.RunListArtifact(projectName, repoName) + resp, err = api.ListArtifact(projectName, repoName) } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) - err = api.RunListArtifact(projectName, repoName) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + resp, err = api.ListArtifact(projectName, repoName) } if err != nil { log.Errorf("failed to list artifacts: %v", err) } + log.Infof("Artifacts: %v", resp) + }, } diff --git a/cmd/harbor/root/artifact/scan.go b/cmd/harbor/root/artifact/scan.go index 44d4412b..8fc34e75 100644 --- a/cmd/harbor/root/artifact/scan.go +++ b/cmd/harbor/root/artifact/scan.go @@ -2,8 +2,8 @@ package artifact import ( "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/utils" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -31,21 +31,17 @@ func StartScanArtifactCommand() *cobra.Command { Long: `Start a scan of an artifact in Harbor Repository`, Example: `harbor artifact scan start //`, Run: func(cmd *cobra.Command, args []string) { - var err error if len(args) > 0 { projectName, repoName, reference := utils.ParseProjectRepoReference(args[0]) - err = api.StartScanArtifact(projectName, repoName, reference) + api.StartScanArtifact(projectName, repoName, reference) } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) - reference := utils.GetReferenceFromUser(repoName, projectName) - err = api.StartScanArtifact(projectName, repoName, reference) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + api.StartScanArtifact(projectName, repoName, reference) } - if err != nil { - log.Errorf("failed to start a scan of an artifact: %v", err) - } }, } return cmd @@ -58,21 +54,17 @@ func StopScanArtifactCommand() *cobra.Command { Long: `Stop a scan of an artifact in Harbor Repository`, Example: `harbor artifact scan stop //`, Run: func(cmd *cobra.Command, args []string) { - var err error if len(args) > 0 { projectName, repoName, reference := utils.ParseProjectRepoReference(args[0]) - err = api.StopScanArtifact(projectName, repoName, reference) + api.StopScanArtifact(projectName, repoName, reference) } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) - reference := utils.GetReferenceFromUser(repoName, projectName) - err = api.StopScanArtifact(projectName, repoName, reference) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + api.StopScanArtifact(projectName, repoName, reference) } - if err != nil { - log.Errorf("failed to stop a scan of an artifact: %v", err) - } }, } return cmd diff --git a/cmd/harbor/root/artifact/tags.go b/cmd/harbor/root/artifact/tags.go index 8136ce2d..b42fe5c3 100644 --- a/cmd/harbor/root/artifact/tags.go +++ b/cmd/harbor/root/artifact/tags.go @@ -1,8 +1,11 @@ package artifact import ( + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/artifact" "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/create" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -16,33 +19,59 @@ func ArtifactTagsCmd() *cobra.Command { } cmd.AddCommand( - ListTagsCommand(), + ListTagsCmd(), DeleteTagsCmd(), + CreateTagsCmd(), ) return cmd } -func ListTagsCommand() *cobra.Command { +func CreateTagsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Create a tag of an artifact", + Example: `harbor artifact tags create // `, + Run: func(cmd *cobra.Command, args []string) { + + if len(args) > 0 { + projectName, repoName, reference := utils.ParseProjectRepoReference(args[0]) + tag := args[1] + api.CreateTag(projectName, repoName, reference, tag) + } else { + var tagName string + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + create.CreateTagView(&tagName) + api.CreateTag(projectName, repoName, reference, tagName) + } + }, + } + + return cmd +} + +func ListTagsCmd() *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 + + var resp artifact.ListTagsOK if len(args) > 0 { projectName, repoName, reference := utils.ParseProjectRepoReference(args[0]) - err = api.ListTags(projectName, repoName, reference) + resp, _ = 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) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + resp, _ = api.ListTags(projectName, repoName, reference) } + log.Info(resp.Payload) + }, } @@ -55,20 +84,16 @@ func DeleteTagsCmd() *cobra.Command { 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) + 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) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) + reference := prompt.GetReferenceFromUser(repoName, projectName) + tag := prompt.GetTagFromUser(repoName, projectName, reference) + api.DeleteTag(projectName, repoName, reference, tag) } }, } diff --git a/cmd/harbor/root/project/delete.go b/cmd/harbor/root/project/delete.go index 0d51765a..b09447cb 100644 --- a/cmd/harbor/root/project/delete.go +++ b/cmd/harbor/root/project/delete.go @@ -2,7 +2,7 @@ package project import ( "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -20,7 +20,7 @@ func DeleteProjectCommand() *cobra.Command { if len(args) > 0 { err = api.DeleteProject(args[0]) } else { - projectName := utils.GetProjectNameFromUser() + projectName := prompt.GetProjectNameFromUser() err = api.DeleteProject(projectName) } if err != nil { diff --git a/cmd/harbor/root/project/list.go b/cmd/harbor/root/project/list.go index e985ab8a..e8a5594b 100644 --- a/cmd/harbor/root/project/list.go +++ b/cmd/harbor/root/project/list.go @@ -4,7 +4,6 @@ import ( "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" @@ -12,7 +11,7 @@ import ( // NewListProjectCommand creates a new `harbor list project` command func ListProjectCommand() *cobra.Command { - var opts plist.ListProjectOptions + var opts api.ListFlags cmd := &cobra.Command{ Use: "list", @@ -34,13 +33,11 @@ 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.BoolVarP(&opts.Public, "public", "", false, "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 } diff --git a/cmd/harbor/root/project/logs.go b/cmd/harbor/root/project/logs.go index 001c1a58..6ee330db 100644 --- a/cmd/harbor/root/project/logs.go +++ b/cmd/harbor/root/project/logs.go @@ -3,6 +3,7 @@ package project import ( "github.com/goharbor/go-client/pkg/sdk/v2.0/client/project" "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/utils" auditLog "github.com/goharbor/harbor-cli/pkg/views/project/logs" log "github.com/sirupsen/logrus" @@ -22,7 +23,7 @@ func LogsProjectCommmand() *cobra.Command { if len(args) > 0 { resp, err = api.LogsProject(args[0]) } else { - projectName := utils.GetProjectNameFromUser() + projectName := prompt.GetProjectNameFromUser() resp, err = api.LogsProject(projectName) } diff --git a/cmd/harbor/root/project/view.go b/cmd/harbor/root/project/view.go index de52ef63..908d51d8 100644 --- a/cmd/harbor/root/project/view.go +++ b/cmd/harbor/root/project/view.go @@ -2,7 +2,7 @@ package project import ( "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -20,7 +20,7 @@ func ViewCommand() *cobra.Command { if len(args) > 0 { err = api.GetProject(args[0]) } else { - projectName := utils.GetProjectNameFromUser() + projectName := prompt.GetProjectNameFromUser() err = api.GetProject(projectName) } diff --git a/cmd/harbor/root/registry/delete.go b/cmd/harbor/root/registry/delete.go index fee3f96a..ed9d426b 100644 --- a/cmd/harbor/root/registry/delete.go +++ b/cmd/harbor/root/registry/delete.go @@ -4,7 +4,7 @@ import ( "strconv" "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -23,7 +23,7 @@ func DeleteRegistryCommand() *cobra.Command { registryId, _ := strconv.ParseInt(args[0], 10, 64) err = api.DeleteRegistry(registryId) } else { - registryId := utils.GetRegistryNameFromUser() + registryId := prompt.GetRegistryNameFromUser() err = api.DeleteRegistry(registryId) } if err != nil { diff --git a/cmd/harbor/root/registry/info.go b/cmd/harbor/root/registry/info.go index 82898772..17da984c 100644 --- a/cmd/harbor/root/registry/info.go +++ b/cmd/harbor/root/registry/info.go @@ -4,7 +4,7 @@ import ( "strconv" "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -21,7 +21,7 @@ func InfoRegistryCommand() *cobra.Command { registryId, _ := strconv.ParseInt(args[0], 10, 64) err = api.InfoRegistry(registryId) } else { - registryId := utils.GetRegistryNameFromUser() + registryId := prompt.GetRegistryNameFromUser() err = api.InfoRegistry(registryId) } if err != nil { diff --git a/cmd/harbor/root/registry/list.go b/cmd/harbor/root/registry/list.go index 76b8f42c..21862779 100644 --- a/cmd/harbor/root/registry/list.go +++ b/cmd/harbor/root/registry/list.go @@ -1,9 +1,7 @@ package registry import ( - "context" - - "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" "github.com/goharbor/harbor-cli/pkg/views/registry/list" log "github.com/sirupsen/logrus" @@ -11,22 +9,15 @@ import ( "github.com/spf13/viper" ) -type listRegistryOptions struct { - page int64 - pageSize int64 - q string - sort string -} - // NewListRegistryCommand creates a new `harbor list registry` command func ListRegistryCommand() *cobra.Command { - var opts listRegistryOptions + var opts api.ListFlags cmd := &cobra.Command{ Use: "list", Short: "list registry", Run: func(cmd *cobra.Command, args []string) { - registry, err := runListRegistry(opts) + registry, err := api.ListRegistries(opts) if err != nil { log.Fatalf("failed to get projects list: %v", err) @@ -41,23 +32,10 @@ func ListRegistryCommand() *cobra.Command { } flags := cmd.Flags() - flags.Int64VarP(&opts.page, "page", "", 1, "Page number") - flags.Int64VarP(&opts.pageSize, "page-size", "", 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") + flags.Int64VarP(&opts.Page, "page", "", 1, "Page number") + flags.Int64VarP(&opts.PageSize, "page-size", "", 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 } - -func runListRegistry(opts listRegistryOptions) (*registry.ListRegistriesOK, error) { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Registry.ListRegistries(ctx, ®istry.ListRegistriesParams{Page: &opts.page, PageSize: &opts.pageSize, Q: &opts.q, Sort: &opts.sort}) - - if err != nil { - return nil, err - } - - return response, nil -} diff --git a/cmd/harbor/root/registry/update.go b/cmd/harbor/root/registry/update.go index c681663c..b9bbb8e4 100644 --- a/cmd/harbor/root/registry/update.go +++ b/cmd/harbor/root/registry/update.go @@ -4,7 +4,7 @@ import ( "strconv" "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/views/registry/create" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -38,7 +38,7 @@ func UpdateRegistryCommand() *cobra.Command { if len(args) > 0 { registryId, err = strconv.ParseInt(args[0], 10, 64) } else { - registryId = utils.GetRegistryNameFromUser() + registryId = prompt.GetRegistryNameFromUser() } if err != nil { diff --git a/cmd/harbor/root/registry/view.go b/cmd/harbor/root/registry/view.go index 8540e3d6..cd43e992 100644 --- a/cmd/harbor/root/registry/view.go +++ b/cmd/harbor/root/registry/view.go @@ -4,7 +4,7 @@ import ( "strconv" "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -24,7 +24,7 @@ func ViewCommand() *cobra.Command { } api.GetRegistry(registryId) } else { - registryId := utils.GetRegistryNameFromUser() + registryId := prompt.GetRegistryNameFromUser() api.GetRegistry(registryId) } }, diff --git a/cmd/harbor/root/repository/delete.go b/cmd/harbor/root/repository/delete.go index 0d83fb9b..fdb3caeb 100644 --- a/cmd/harbor/root/repository/delete.go +++ b/cmd/harbor/root/repository/delete.go @@ -2,6 +2,7 @@ package repository import ( "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -19,8 +20,8 @@ func RepoDeleteCmd() *cobra.Command { projectName, repoName := utils.ParseProjectRepo(args[0]) err = api.RepoDelete(projectName, repoName) } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) err = api.RepoDelete(projectName, repoName) } if err != nil { diff --git a/cmd/harbor/root/repository/info.go b/cmd/harbor/root/repository/info.go index 9a365196..6a54da71 100644 --- a/cmd/harbor/root/repository/info.go +++ b/cmd/harbor/root/repository/info.go @@ -2,6 +2,7 @@ package repository import ( "github.com/goharbor/harbor-cli/pkg/api" + "github.com/goharbor/harbor-cli/pkg/prompt" "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -19,8 +20,8 @@ func RepoInfoCmd() *cobra.Command { projectName, repoName := utils.ParseProjectRepo(args[0]) err = api.RepoInfo(projectName, repoName) } else { - projectName := utils.GetProjectNameFromUser() - repoName := utils.GetRepoNameFromUser(projectName) + projectName := prompt.GetProjectNameFromUser() + repoName := prompt.GetRepoNameFromUser(projectName) err = api.RepoInfo(projectName, repoName) } if err != nil { diff --git a/cmd/harbor/root/repository/list.go b/cmd/harbor/root/repository/list.go index 428cab0f..e9f1126c 100644 --- a/cmd/harbor/root/repository/list.go +++ b/cmd/harbor/root/repository/list.go @@ -1,8 +1,10 @@ package repository import ( + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository" "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" + "github.com/goharbor/harbor-cli/pkg/views/repository/list" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -14,16 +16,21 @@ func ListRepositoryCommand() *cobra.Command { Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { var err error + var resp repository.ListRepositoriesOK if len(args) > 0 { - err = api.ListRepository(args[0]) + resp, err = api.ListRepository(args[0]) } else { - projectName := utils.GetProjectNameFromUser() - err = api.ListRepository(projectName) + projectName := prompt.GetProjectNameFromUser() + resp, err = api.ListRepository(projectName) } + if err != nil { log.Errorf("failed to list repositories: %v", err) } + + list.ListRepositories(resp.Payload) + }, } diff --git a/cmd/harbor/root/user/delete.go b/cmd/harbor/root/user/delete.go index 5b287ba1..1b5afc8a 100644 --- a/cmd/harbor/root/user/delete.go +++ b/cmd/harbor/root/user/delete.go @@ -4,7 +4,7 @@ import ( "strconv" "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -21,7 +21,7 @@ func UserDeleteCmd() *cobra.Command { err = api.DeleteUser(userId) } else { - userId := utils.GetUserIdFromUser() + userId := prompt.GetUserIdFromUser() err = api.DeleteUser(userId) } diff --git a/cmd/harbor/root/user/elevate.go b/cmd/harbor/root/user/elevate.go index 9e7e495b..9369b9c7 100644 --- a/cmd/harbor/root/user/elevate.go +++ b/cmd/harbor/root/user/elevate.go @@ -4,7 +4,7 @@ import ( "strconv" "github.com/goharbor/harbor-cli/pkg/api" - "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/goharbor/harbor-cli/pkg/prompt" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -22,7 +22,7 @@ func ElevateUserCmd() *cobra.Command { userId, _ = strconv.ParseInt(args[0], 10, 64) } else { - userId = utils.GetUserIdFromUser() + userId = prompt.GetUserIdFromUser() } // Todo : Ask for the confirmation before elevating the user to admin role diff --git a/pkg/api/artifact_handler.go b/pkg/api/artifact_handler.go index eacdfd8b..cdfc6764 100644 --- a/pkg/api/artifact_handler.go +++ b/pkg/api/artifact_handler.go @@ -3,12 +3,13 @@ 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/go-client/pkg/sdk/v2.0/models" "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 { +// DeleteArtifact handles the deletion of an artifact. +func DeleteArtifact(projectName, repoName, reference string) error { ctx, client, err := utils.ContextWithClient() if err != nil { return err @@ -28,8 +29,8 @@ func RunDeleteArtifact(projectName, repoName, reference string) error { return nil } -// RunInfoArtifact retrieves information about a specific artifact. -func RunInfoArtifact(projectName, repoName, reference string) error { +// InfoArtifact retrieves information about a specific artifact. +func InfoArtifact(projectName, repoName, reference string) error { ctx, client, err := utils.ContextWithClient() if err != nil { return err @@ -50,10 +51,10 @@ func RunInfoArtifact(projectName, repoName, reference string) error { } // RunListArtifact lists all artifacts in a repository. -func RunListArtifact(projectName, repoName string) error { +func ListArtifact(projectName, repoName string) (artifact.ListArtifactsOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return err + return artifact.ListArtifactsOK{}, err } response, err := client.Artifact.ListArtifacts(ctx, &artifact.ListArtifactsParams{ @@ -61,12 +62,10 @@ func RunListArtifact(projectName, repoName string) error { RepositoryName: repoName, }) if err != nil { - log.Errorf("Failed to list artifacts: %v", err) - return err + return artifact.ListArtifactsOK{}, err } - log.Info(response.Payload) - return nil + return *response, nil } // StartScanArtifact initiates a scan on a specific artifact. @@ -134,10 +133,10 @@ func DeleteTag(projectName, repoName, reference, tag string) error { } // ListTags lists all tags of a specific artifact. -func ListTags(projectName, repoName, reference string) error { +func ListTags(projectName, repoName, reference string) (artifact.ListTagsOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return err + return artifact.ListTagsOK{}, err } resp, err := client.Artifact.ListTags(ctx, &artifact.ListTagsParams{ @@ -147,9 +146,30 @@ func ListTags(projectName, repoName, reference string) error { }) if err != nil { log.Errorf("Failed to list tags: %v", err) - return err + return artifact.ListTagsOK{}, err } - log.Info(resp.Payload) + return *resp, nil +} + +// CreateTag creates a tag for a specific artifact. +func CreateTag(projectName, repoName, reference, tagName string) error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } + _, err = client.Artifact.CreateTag(ctx, &artifact.CreateTagParams{ + ProjectName: projectName, + RepositoryName: repoName, + Reference: reference, + Tag: &models.Tag{ + Name: tagName, + }, + }) + 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) return nil } diff --git a/pkg/api/project_handler.go b/pkg/api/project_handler.go index 716d7d9c..07824a30 100644 --- a/pkg/api/project_handler.go +++ b/pkg/api/project_handler.go @@ -7,7 +7,6 @@ import ( "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" ) @@ -71,16 +70,20 @@ func DeleteProject(projectName string) error { return nil } -func ListProject(opts plist.ListProjectOptions) (*project.ListProjectsOK, error) { +func ListProject(opts ...ListFlags) (project.ListProjectsOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return nil, err + return project.ListProjectsOK{}, err + } + var listFlags ListFlags + if len(opts) > 0 { + listFlags = opts[0] } - 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}) + response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{Page: &listFlags.Page, PageSize: &listFlags.PageSize, Q: &listFlags.Q, Sort: &listFlags.Sort, Name: &listFlags.Name, Public: &listFlags.Public}) if err != nil { - return nil, err + return project.ListProjectsOK{}, err } - return response, nil + return *response, nil } func LogsProject(projectName string) (*project.GetLogsOK, error) { diff --git a/pkg/api/registry_handler.go b/pkg/api/registry_handler.go index 8b34447f..882be20a 100644 --- a/pkg/api/registry_handler.go +++ b/pkg/api/registry_handler.go @@ -8,6 +8,33 @@ import ( log "github.com/sirupsen/logrus" ) +func ListRegistries(opts ...ListFlags) (*registry.ListRegistriesOK, error) { + ctx, client, err := utils.ContextWithClient() + if err != nil { + return nil, err + } + + var listFlags ListFlags + + if len(opts) > 0 { + listFlags = opts[0] + } + + response, err := client.Registry.ListRegistries(ctx, ®istry.ListRegistriesParams{ + Page: &listFlags.Page, + PageSize: &listFlags.PageSize, + Q: &listFlags.Q, + Name: &listFlags.Name, + Sort: &listFlags.Sort, + }) + + if err != nil { + return nil, err + } + + return response, nil +} + func CreateRegistry(opts create.CreateView) error { ctx, client, err := utils.ContextWithClient() if err != nil { diff --git a/pkg/api/repository_handler.go b/pkg/api/repository_handler.go index 02dfa81c..7e5662b4 100644 --- a/pkg/api/repository_handler.go +++ b/pkg/api/repository_handler.go @@ -3,7 +3,6 @@ package api import ( "github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository" "github.com/goharbor/harbor-cli/pkg/utils" - "github.com/goharbor/harbor-cli/pkg/views/repository/list" log "github.com/sirupsen/logrus" ) @@ -38,19 +37,18 @@ func RepoInfo(projectName, repoName string) error { return nil } -func ListRepository(ProjectName string) error { +func ListRepository(projectName string) (repository.ListRepositoriesOK, error) { ctx, client, err := utils.ContextWithClient() if err != nil { - return err + return repository.ListRepositoriesOK{}, err } - response, err := client.Repository.ListRepositories(ctx, &repository.ListRepositoriesParams{ProjectName: ProjectName}) + response, err := client.Repository.ListRepositories(ctx, &repository.ListRepositoriesParams{ProjectName: projectName}) if err != nil { - return err + return repository.ListRepositoriesOK{}, err } - list.ListRepositories(response.Payload) - return nil + return *response, nil } diff --git a/pkg/api/types.go b/pkg/api/types.go new file mode 100644 index 00000000..b6039b80 --- /dev/null +++ b/pkg/api/types.go @@ -0,0 +1,10 @@ +package api + +type ListFlags struct { + Name string + Page int64 + PageSize int64 + Q string + Sort string + Public bool +} diff --git a/pkg/prompt/prompt.go b/pkg/prompt/prompt.go new file mode 100644 index 00000000..f67f9b94 --- /dev/null +++ b/pkg/prompt/prompt.go @@ -0,0 +1,91 @@ +package prompt + +import ( + "github.com/goharbor/harbor-cli/pkg/api" + 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" + uview "github.com/goharbor/harbor-cli/pkg/views/user/select" + log "github.com/sirupsen/logrus" +) + +func GetRegistryNameFromUser() int64 { + registryId := make(chan int64) + go func() { + response, _ := api.ListRegistries() + rview.RegistryList(response.Payload, registryId) + + }() + + return <-registryId + +} + +func GetProjectNameFromUser() string { + projectName := make(chan string) + go func() { + response, _ := api.ListProject() + pview.ProjectList(response.Payload, projectName) + + }() + + return <-projectName +} + +func GetRepoNameFromUser(projectName string) string { + repositoryName := make(chan string) + + go func() { + + response, err := api.ListRepository(projectName) + if err != nil { + log.Fatal(err) + } + repoView.RepositoryList(response.Payload, repositoryName) + }() + + return <-repositoryName +} + +// complete the function +func GetReferenceFromUser(repositoryName string, projectName string) string { + reference := make(chan string) + go func() { + response, _ := api.ListArtifact(projectName, repositoryName) + aview.ListArtifacts(response.Payload, reference) + }() + return <-reference +} + +func GetUserIdFromUser() int64 { + userId := make(chan int64) + + go func() { + response, _ := api.ListUsers() + uview.UserList(response.Payload, userId) + }() + + return <-userId + +} + +func GetTagFromUser(repoName, projectName, reference string) string { + tag := make(chan string) + go func() { + response, _ := api.ListTags(projectName, repoName, reference) + tview.ListTags(response.Payload, tag) + }() + return <-tag +} + +func GetTagNameFromUser() string { + repoName := make(chan string) + + go func() { + + }() + + return <-repoName +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 906f78be..a64a67c7 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -1,24 +1,11 @@ package utils import ( - "context" "encoding/json" "fmt" "strings" - "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" - uview "github.com/goharbor/harbor-cli/pkg/views/user/select" log "github.com/sirupsen/logrus" - "github.com/spf13/viper" ) // Returns Harbor v2 client for given clientConfig @@ -36,74 +23,6 @@ func PrintPayloadInJSONFormat(payload any) { fmt.Println(string(jsonStr)) } -func GetRegistryNameFromUser() int64 { - registryId := make(chan int64) - go func() { - credentialName := viper.GetString("current-credential-name") - client := GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Registry.ListRegistries(ctx, ®istry.ListRegistriesParams{}) - if err != nil { - log.Fatal(err) - } - - rview.RegistryList(response.Payload, registryId) - - }() - - return <-registryId - -} - -func GetProjectNameFromUser() string { - projectName := make(chan string) - go func() { - credentialName := viper.GetString("current-credential-name") - client := GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Project.ListProjects(ctx, &project.ListProjectsParams{}) - if err != nil { - log.Fatal(err) - } - pview.ProjectList(response.Payload, projectName) - - }() - - return <-projectName -} - -func GetRepoNameFromUser(projectName string) string { - repositoryName := make(chan string) - - go func() { - credentialName := viper.GetString("current-credential-name") - client := GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.Repository.ListRepositories(ctx, &repository.ListRepositoriesParams{ProjectName: projectName}) - if err != nil { - log.Fatal(err) - } - repoView.RepositoryList(response.Payload, repositoryName) - }() - - return <-repositoryName -} - -// complete the function -func GetReferenceFromUser(repositoryName string, projectName string) string { - reference := make(chan string) - go func() { - credentialName := viper.GetString("current-credential-name") - client := GetClientByCredentialName(credentialName) - ctx := context.Background() - response, _ := client.Artifact.ListArtifacts(ctx, &artifact.ListArtifactsParams{ProjectName: projectName, RepositoryName: repositoryName}) - - aview.ListArtifacts(response.Payload, reference) - - }() - return <-reference -} - func ParseProjectRepo(projectRepo string) (string, string) { split := strings.Split(projectRepo, "/") if len(split) != 2 { @@ -119,36 +38,3 @@ func ParseProjectRepoReference(projectRepoReference string) (string, string, str } return split[0], split[1], split[2] } - -func GetUserIdFromUser() int64 { - userId := make(chan int64) - - go func() { - credentialName := viper.GetString("current-credential-name") - client := GetClientByCredentialName(credentialName) - ctx := context.Background() - response, err := client.User.ListUsers(ctx, &user.ListUsersParams{}) - if err != nil { - log.Fatal(err) - } - uview.UserList(response.Payload, userId) - }() - - 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/create/view.go b/pkg/views/artifact/tags/create/view.go new file mode 100644 index 00000000..dd7449e4 --- /dev/null +++ b/pkg/views/artifact/tags/create/view.go @@ -0,0 +1,30 @@ +package create + +import ( + "errors" + + "github.com/charmbracelet/huh" + log "github.com/sirupsen/logrus" +) + +func CreateTagView(tagName *string) { + theme := huh.ThemeCharm() + + err := huh.NewForm( + huh.NewGroup( + huh.NewInput(). + Title("Tag Name"). + Value(tagName). + Validate(func(str string) error { + if str == "" { + return errors.New("project name cannot be empty") + } + return nil + }), + ), + ).WithTheme(theme).Run() + + if err != nil { + log.Fatal(err) + } +} diff --git a/pkg/views/project/create/view.go b/pkg/views/project/create/view.go index 4d22dde3..484c0a05 100644 --- a/pkg/views/project/create/view.go +++ b/pkg/views/project/create/view.go @@ -78,7 +78,8 @@ func CreateProjectView(createView *CreateView) { Value(&createView.ProxyCache). Affirmative("yes"). Negative("no"), - + ), + huh.NewGroup( huh.NewSelect[string](). Validate(func(str string) error { if createView.ProxyCache && str == "" { @@ -90,7 +91,9 @@ func CreateProjectView(createView *CreateView) { Title("Registry ID"). Value(&createView.RegistryID). Options(registrySelectOptions...), - ), + ).WithHideFunc(func() bool { + return !createView.ProxyCache || len(registryOptions) == 0 + }), ).WithTheme(theme).Run() if err != nil { diff --git a/pkg/views/project/list/view.go b/pkg/views/project/list/view.go index 235b0b3c..0cf10be9 100644 --- a/pkg/views/project/list/view.go +++ b/pkg/views/project/list/view.go @@ -12,17 +12,6 @@ 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},