-
Notifications
You must be signed in to change notification settings - Fork 161
feat(user): add fuzzy, match, range flags for commands #682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mujib-Ahasan
wants to merge
9
commits into
goharbor:main
Choose a base branch
from
Mujib-Ahasan:new-query-flags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
55ef85f
feat(user): add fuzzy,match,range flag for user list command
Mujib-Ahasan a6c3420
refractor: add fuzzy,match,range flags for resource query
Mujib-Ahasan f42b6aa
New query flags added to few list subcommands
Mujib-Ahasan b1951cc
New query flags added to list scanners subcommand
Mujib-Ahasan f37a5c5
new query flags added to robot list
Mujib-Ahasan 52c9f28
Merge remote-tracking branch 'origin/main' into new-query-flags
Mujib-Ahasan 6b179a2
fix: error handling and small nits
Mujib-Ahasan 06e2d35
fix: error handling
Mujib-Ahasan f8d25bb
fix: small error handling
Mujib-Ahasan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| 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" | ||
| "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/list" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func ArtifactTagsCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "tags", | ||
| Short: "Manage tags of an artifact", | ||
| Example: ` harbor artifact tags list <project>/<repository>/<reference>`, | ||
| } | ||
|
|
||
| cmd.AddCommand( | ||
| ListTagsCmd(), | ||
| DeleteTagsCmd(), | ||
| CreateTagsCmd(), | ||
| ) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| 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) { | ||
| var err error | ||
| var projectName, repoName, reference string | ||
| var tagName string | ||
| if len(args) > 0 { | ||
| projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) | ||
| if err != nil { | ||
| log.Errorf("failed to parse project/repo/reference: %v", err) | ||
| } | ||
| tagName = args[1] | ||
| } else { | ||
| projectName, err = prompt.GetProjectNameFromUser() | ||
| if err != nil { | ||
| log.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) | ||
| } | ||
| repoName = prompt.GetRepoNameFromUser(projectName) | ||
| reference = prompt.GetReferenceFromUser(repoName, projectName) | ||
| create.CreateTagView(&tagName) | ||
| } | ||
| err = api.CreateTag(projectName, repoName, reference, tagName) | ||
| if err != nil { | ||
| log.Errorf("failed to create tag: %v", err) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func ListTagsCmd() *cobra.Command { | ||
|
|
||
| var ( | ||
| opts api.ListFlags | ||
| // For querying, opts.Q | ||
| fuzzy []string | ||
| match []string | ||
| ranges []string | ||
| ) | ||
|
|
||
| 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 tags *artifact.ListTagsOK | ||
| var projectName, repoName, reference string | ||
|
|
||
| if len(fuzzy) != 0 || len(match) != 0 || len(ranges) != 0 { | ||
| q, qErr := utils.BuildQueryParam(fuzzy, match, ranges, | ||
| []string{"id", "repository_id", "artifact_id", "name", "immutable"}, | ||
| ) | ||
| if qErr != nil { | ||
| log.Errorf("error while building query parameter: %v", qErr) | ||
| return | ||
| } | ||
|
|
||
| opts.Q = q | ||
|
Mujib-Ahasan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| if len(args) > 0 { | ||
| projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) | ||
| if err != nil { | ||
| log.Errorf("failed to parse project/repo/reference: %v", err) | ||
| } | ||
| } else { | ||
| projectName, err = prompt.GetProjectNameFromUser() | ||
| if err != nil { | ||
| log.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) | ||
| } | ||
| repoName = prompt.GetRepoNameFromUser(projectName) | ||
| if repoName == "" { | ||
| return | ||
| } | ||
| reference = prompt.GetReferenceFromUser(repoName, projectName) | ||
| } | ||
|
|
||
| tags, err = api.ListTags(projectName, repoName, reference, opts) | ||
|
|
||
| if err != nil { | ||
| log.Errorf("failed to list tags: %v", err) | ||
| return | ||
| } | ||
|
|
||
| FormatFlag := viper.GetString("output-format") | ||
| if FormatFlag != "" { | ||
| err = utils.PrintFormat(tags, FormatFlag) | ||
| if err != nil { | ||
| log.Error(err) | ||
| return | ||
| } | ||
| } else { | ||
| list.ListTags(tags.Payload) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources") | ||
| flags.StringSliceVar(&fuzzy, "fuzzy", nil, "Fuzzy match filter (key=value)") | ||
| flags.StringSliceVar(&match, "match", nil, "exact match filter (key=value)") | ||
| flags.StringSliceVar(&ranges, "range", nil, "range filter (key=min~max)") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func DeleteTagsCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "delete", | ||
| 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 | ||
| var projectName, repoName, reference string | ||
| var tagName string | ||
| if len(args) > 0 { | ||
| projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0]) | ||
| if err != nil { | ||
| log.Errorf("failed to parse project/repo/reference: %v", err) | ||
| } | ||
| tagName = args[1] | ||
| } else { | ||
| projectName, err = prompt.GetProjectNameFromUser() | ||
| if err != nil { | ||
| log.Errorf("failed to get project name: %v", utils.ParseHarborErrorMsg(err)) | ||
| } | ||
| repoName = prompt.GetRepoNameFromUser(projectName) | ||
| reference = prompt.GetReferenceFromUser(repoName, projectName) | ||
| tagName = prompt.GetTagFromUser(repoName, projectName, reference) | ||
| } | ||
| err = api.DeleteTag(projectName, repoName, reference, tagName) | ||
| if err != nil { | ||
| log.Errorf("failed to delete tag: %v", err) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,14 @@ import ( | |
|
|
||
| // ListRobotCommand creates a new `harbor project robot list` command | ||
| func ListRobotCommand() *cobra.Command { | ||
| var opts api.ListFlags | ||
|
|
||
| var ( | ||
| opts api.ListFlags | ||
| // For querying, opts.Q | ||
| fuzzy []string | ||
| match []string | ||
| ranges []string | ||
| ) | ||
|
|
||
| projectQString := constants.ProjectQString | ||
| cmd := &cobra.Command{ | ||
|
|
@@ -96,6 +103,19 @@ Examples: | |
| opts.Q = projectQString + strconv.FormatInt(projectID, 10) | ||
| } | ||
|
|
||
| if len(fuzzy) != 0 || len(match) != 0 || len(ranges) != 0 { | ||
| q, qErr := utils.BuildQueryParam(fuzzy, match, ranges, | ||
| []string{"id", "secret", "description", "name", "disable", "level", "duration", "editable"}, | ||
| ) | ||
| if qErr != nil { | ||
| return qErr | ||
| } | ||
| if opts.Q != "" { | ||
| opts.Q += "," | ||
| } | ||
| opts.Q += q | ||
|
Mujib-Ahasan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| robots, err := api.ListRobot(opts) | ||
| if err != nil { | ||
| log.Errorf("failed to get robots list: %v", err) | ||
|
|
@@ -120,13 +140,10 @@ Examples: | |
| flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page") | ||
| flags.Int64VarP(&opts.ProjectID, "project-id", "", 0, "Project ID") | ||
| flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources") | ||
|
Comment on lines
141
to
142
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No query conversion here |
||
| flags.StringVarP( | ||
| &opts.Sort, | ||
| "sort", | ||
| "", | ||
| "", | ||
| "Sort the resource list in ascending or descending order", | ||
| ) | ||
| flags.StringSliceVar(&fuzzy, "fuzzy", nil, "Fuzzy match filter (key=value)") | ||
| flags.StringSliceVar(&match, "match", nil, "exact match filter (key=value)") | ||
| flags.StringSliceVar(&ranges, "range", nil, "range filter (key=min~max)") | ||
| flags.StringVarP(&opts.Sort, "sort", "", "", "Sort the resource list in ascending or descending order") | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.