Skip to content

Commit

Permalink
feat: manage prompts separately
Browse files Browse the repository at this point in the history
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
  • Loading branch information
amands98 committed May 29, 2024
1 parent 0d785e7 commit a97d084
Show file tree
Hide file tree
Showing 29 changed files with 333 additions and 267 deletions.
11 changes: 6 additions & 5 deletions cmd/harbor/root/artifact/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions cmd/harbor/root/artifact/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions cmd/harbor/root/artifact/list.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)

},
}

Expand Down
30 changes: 11 additions & 19 deletions cmd/harbor/root/artifact/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -31,21 +31,17 @@ func StartScanArtifactCommand() *cobra.Command {
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 = 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
Expand All @@ -58,21 +54,17 @@ func StopScanArtifactCommand() *cobra.Command {
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 = 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
Expand Down
67 changes: 46 additions & 21 deletions cmd/harbor/root/artifact/tags.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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 <project>/<repository>/<reference> <tag>`,
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 <project>/<repository>/<reference>`,
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)

},
}

Expand All @@ -55,20 +84,16 @@ func DeleteTagsCmd() *cobra.Command {
Short: "Delete a tag of an artifact",
Example: `harbor artifact tags delete <project>/<repository>/<reference> <tag>`,
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) > 0 {
projectName, repoName, reference := utils.ParseProjectRepoReference(args[0])
tag := args[1]
err = api.DeleteTag(projectName, repoName, reference, tag)
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)
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/project/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions cmd/harbor/root/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ 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"
)

// 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",
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion cmd/harbor/root/project/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/project/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/registry/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/registry/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit a97d084

Please sign in to comment.