-
Notifications
You must be signed in to change notification settings - Fork 9
Hotfix #4020: Add delete scan params functionality #128
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,125 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/kondukto-io/kdt/client" | ||
| "github.com/kondukto-io/kdt/klog" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var scanParamsCmd = &cobra.Command{ | ||
| Use: "scanparams", | ||
| Short: "base command for scan parameter operations", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| if len(args) == 0 { | ||
| _ = cmd.Help() | ||
| qwm(ExitCodeSuccess, "") | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(scanParamsCmd) | ||
|
|
||
| scanParamsCmd.AddCommand(deleteScanParamsCmd) | ||
|
|
||
| deleteScanParamsCmd.Flags().StringP("project", "p", "", "kondukto project id or name (required)") | ||
| deleteScanParamsCmd.Flags().StringP("tool", "t", "", "tool name of scan params (required)") | ||
| deleteScanParamsCmd.Flags().StringP("meta", "m", "", "meta data of scan params") | ||
| deleteScanParamsCmd.Flags().StringP("branch", "b", "", "branch of scan params") | ||
| deleteScanParamsCmd.Flags().BoolP("force", "f", false, "force to delete (required)") | ||
| } | ||
|
|
||
| var deleteScanParamsCmd = &cobra.Command{ | ||
| Use: "delete", | ||
| Short: "delete scan parameters and vulnerabilities from Kondukto", | ||
| Run: deleteScanParamsRootCommand, | ||
| } | ||
|
|
||
| func deleteScanParamsRootCommand(cmd *cobra.Command, _ []string) { | ||
| c, err := client.New() | ||
| if err != nil { | ||
| qwe(ExitCodeError, err, "could not initialize Kondukto client") | ||
| } | ||
|
|
||
| scanParams := ScanParamsDelete{ | ||
| cmd: cmd, | ||
| client: c, | ||
| } | ||
|
|
||
| if err = scanParams.delete(); err != nil { | ||
| qwe(ExitCodeError, err, "failed to delete scan parameters") | ||
| } | ||
| } | ||
|
|
||
| type ScanParamsDelete struct { | ||
| cmd *cobra.Command | ||
| client *client.Client | ||
| } | ||
|
|
||
| func (s *ScanParamsDelete) delete() error { | ||
| projectName, err := getSanitizedFlagStr(s.cmd, "project") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get project flag: %w", err) | ||
| } | ||
|
|
||
| if projectName == "" { | ||
| return fmt.Errorf("project name is required") | ||
| } | ||
|
|
||
| scanner, err := s.cmd.Flags().GetString("tool") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse tool flag: %w", err) | ||
| } | ||
|
|
||
| if scanner == "" { | ||
| return fmt.Errorf("tool is required") | ||
| } | ||
|
|
||
| branch, err := s.cmd.Flags().GetString("branch") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse branch flag: %w", err) | ||
| } | ||
|
|
||
| meta, err := s.cmd.Flags().GetString("meta") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse meta flag: %w", err) | ||
| } | ||
|
|
||
| force, err := s.cmd.Flags().GetBool("force") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to parse force flag: %w", err) | ||
| } | ||
|
|
||
| if !force { | ||
| return fmt.Errorf("--force is required") | ||
| } | ||
|
|
||
| var request = client.ScanParamsDeleteParams{ | ||
| ToolName: scanner, | ||
| Branch: branch, | ||
| MetaData: meta, | ||
| } | ||
|
|
||
| if meta == "" && s.cmd.Flags().Changed("meta") { | ||
| request.MetaDataIsEmpty = true | ||
| } | ||
|
|
||
| if err := s.client.DeleteScanparamsBy(projectName, request); err != nil { | ||
| return fmt.Errorf("failed to delete scan parameters: %w", err) | ||
| } | ||
|
|
||
| var message = fmt.Sprintf("scan parameters deleted successfully for project: [%s] and scanner: [%s]", projectName, scanner) | ||
|
|
||
| if branch != "" { | ||
| message += fmt.Sprintf(" and branch: [%s]", branch) | ||
| } | ||
|
|
||
| if meta != "" { | ||
| message += fmt.Sprintf(" and metadata: [%s]", meta) | ||
| } | ||
|
|
||
| klog.Print(message) | ||
| return nil | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
--delete-permanentlyinstead of--force? It looks better for user perspective.