diff --git a/internal/commands/scan.go b/internal/commands/scan.go index aeeb7925e..b3d6ff1f8 100644 --- a/internal/commands/scan.go +++ b/internal/commands/scan.go @@ -66,141 +66,229 @@ func NewScanCommand(scansWrapper wrappers.ScansWrapper, }, } - createScanCmd := &cobra.Command{ - Use: "create", - Short: "Create and run a new scan", - Long: "The create command enables the ability to create and run a new scan in CxAST.", - Example: heredoc.Doc(` - $ cx scan create --project-name --sources - `), + createScanCmd := scanCreateSubCommand(scansWrapper, uploadsWrapper, resultsWrapper) + + listScansCmd := scanListSubCommand(scansWrapper) + + showScanCmd := scanShowSubCommand(scansWrapper) + + workflowScanCmd := scanWorkflowSubCommand(scansWrapper) + + deleteScanCmd := scanDeleteSubCommand(scansWrapper) + + cancelScanCmd := scanCancelSubCommand(scansWrapper) + + tagsCmd := scanTagsSubCommand(scansWrapper) + + addFormatFlagToMultipleCommands( + []*cobra.Command{listScansCmd, showScanCmd, workflowScanCmd}, + util.FormatTable, util.FormatList, util.FormatJSON, + ) + addFormatFlagToMultipleCommands( + []*cobra.Command{createScanCmd}, + util.FormatList, util.FormatTable, util.FormatJSON, + ) + scanCmd.AddCommand(createScanCmd, showScanCmd, workflowScanCmd, listScansCmd, deleteScanCmd, cancelScanCmd, tagsCmd) + return scanCmd +} + +func scanTagsSubCommand(scansWrapper wrappers.ScansWrapper) *cobra.Command { + tagsCmd := &cobra.Command{ + Use: "tags", + Short: "Get a list of all available tags to filter by", + Long: "The tags command enables the ability to provide a list of all the available tags in CxAST.", + Example: heredoc.Doc( + ` + $ cx scan tags + `, + ), Annotations: map[string]string{ - "command:doc": heredoc.Doc(` - https://checkmarx.atlassian.net/wiki/x/WguYtw - `), + "command:doc": heredoc.Doc( + ` + https://checkmarx.atlassian.net/wiki/x/546Xtw + `, + ), }, - RunE: runCreateScanCommand(scansWrapper, uploadsWrapper, resultsWrapper), + RunE: runGetTagsCommand(scansWrapper), } + return tagsCmd +} - createScanCmd.PersistentFlags().BoolP(WaitFlag, "", false, "Wait for scan completion (default true)") - createScanCmd.PersistentFlags().IntP(WaitDelayFlag, "", WaitDelayDefault, "Polling wait time in seconds") - createScanCmd.PersistentFlags().StringP(SourcesFlag, SourcesFlagSh, "", "Sources like: directory, zip file or git URL.") - createScanCmd.PersistentFlags().StringP(SourceDirFilterFlag, SourceDirFilterFlagSh, "", "Source file filtering pattern") - createScanCmd.PersistentFlags().StringP( - IncludeFilterFlag, - IncludeFilterFlagSh, - "", - "Only files scannable by AST are included by default."+ - " Add a comma separated list of extra inclusions, ex: *zip,file.txt", - ) - createScanCmd.PersistentFlags().String(ProjectName, "", "Name of the project") - createScanCmd.PersistentFlags().String(IncrementalSast, "false", "Incremental SAST scan should be performed.") - createScanCmd.PersistentFlags().String(PresetName, "", "The name of the Checkmarx preset to use.") - createScanCmd.PersistentFlags().String(ScanTypes, "", "Scan types, ex: (sast,kics,sca)") - createScanCmd.PersistentFlags().String(TagList, "", "List of tags, ex: (tagA,tagB:val,etc)") - createScanCmd.PersistentFlags().StringP(BranchFlag, BranchFlagSh, commonParams.Branch, BranchFlagUsage) - // Link the environment variable to the CLI argument(s). - _ = viper.BindPFlag(commonParams.BranchKey, createScanCmd.PersistentFlags().Lookup(BranchFlag)) - - listScansCmd := &cobra.Command{ - Use: "list", - Short: "List all scans in CxAST", - Long: "The list command provides a list of all the scans in CxAST.", - Example: heredoc.Doc(` - $ cx scan list - `), +func scanCancelSubCommand(scansWrapper wrappers.ScansWrapper) *cobra.Command { + cancelScanCmd := &cobra.Command{ + Use: "cancel", + Short: "Cancel one or more scans from running", + Long: "The cancel command enables the ability to cancel one or more running scans in CxAST.", + Example: heredoc.Doc( + ` + $ cx scan cancel --scan-id + `, + ), Annotations: map[string]string{ - "command:doc": heredoc.Doc(` - https://checkmarx.atlassian.net/wiki/x/K46Xtw - `), + "command:doc": heredoc.Doc( + ` + https://checkmarx.atlassian.net/wiki/x/aY2Xtw + `, + ), }, - RunE: runListScansCommand(scansWrapper), + RunE: runCancelScanCommand(scansWrapper), } - listScansCmd.PersistentFlags().StringSlice(FilterFlag, []string{}, filterScanListFlagUsage) + addScanIDFlag(cancelScanCmd, "One or more scan IDs to cancel, ex: ,,...") + return cancelScanCmd +} - showScanCmd := &cobra.Command{ - Use: "show", - Short: "Show information about a scan", - Long: "The show command enables the ability to show information about a requested scan in CxAST.", - Example: heredoc.Doc(` - $ cx scan show --scan-id - `), +func scanDeleteSubCommand(scansWrapper wrappers.ScansWrapper) *cobra.Command { + deleteScanCmd := &cobra.Command{ + Use: "delete", + Short: "Deletes one or more scans", + Example: heredoc.Doc( + ` + $ cx scan delete --scan-id + `, + ), Annotations: map[string]string{ - "command:doc": heredoc.Doc(` - https://checkmarx.atlassian.net/wiki/x/qAyYtw - `), + "command:doc": heredoc.Doc( + ` + https://checkmarx.atlassian.net/wiki/x/-AuYtw + `, + ), }, - RunE: runGetScanByIDCommand(scansWrapper), + RunE: runDeleteScanCommand(scansWrapper), } - addScanIDFlag(showScanCmd, "Scan ID to show.") + addScanIDFlag(deleteScanCmd, "One or more scan IDs to delete, ex: ,,...") + return deleteScanCmd +} +func scanWorkflowSubCommand(scansWrapper wrappers.ScansWrapper) *cobra.Command { workflowScanCmd := &cobra.Command{ Use: "workflow ", Short: "Show information about a scan workflow", Long: "The workflow command enables the ability to provide information about a requested scan workflow in CxAST.", - Example: heredoc.Doc(` + Example: heredoc.Doc( + ` $ cx scan workflow --scan-id - `), + `, + ), Annotations: map[string]string{ - "command:doc": heredoc.Doc(` + "command:doc": heredoc.Doc( + ` https://checkmarx.atlassian.net/wiki/x/Ug2Ytw - `), + `, + ), }, RunE: runScanWorkflowByIDCommand(scansWrapper), } addScanIDFlag(workflowScanCmd, "Scan ID to workflow.") + return workflowScanCmd +} - deleteScanCmd := &cobra.Command{ - Use: "delete", - Short: "Deletes one or more scans", - Example: heredoc.Doc(` - $ cx scan delete --scan-id - `), +func scanShowSubCommand(scansWrapper wrappers.ScansWrapper) *cobra.Command { + showScanCmd := &cobra.Command{ + Use: "show", + Short: "Show information about a scan", + Long: "The show command enables the ability to show information about a requested scan in CxAST.", + Example: heredoc.Doc( + ` + $ cx scan show --scan-id + `, + ), Annotations: map[string]string{ - "command:doc": heredoc.Doc(` - https://checkmarx.atlassian.net/wiki/x/-AuYtw - `), + "command:doc": heredoc.Doc( + ` + https://checkmarx.atlassian.net/wiki/x/qAyYtw + `, + ), }, - RunE: runDeleteScanCommand(scansWrapper), + RunE: runGetScanByIDCommand(scansWrapper), } - addScanIDFlag(deleteScanCmd, "One or more scan IDs to delete, ex: ,,...") + addScanIDFlag(showScanCmd, "Scan ID to show.") + return showScanCmd +} - cancelScanCmd := &cobra.Command{ - Use: "cancel", - Short: "Cancel one or more scans from running", - Long: "The cancel command enables the ability to cancel one or more running scans in CxAST.", - Example: heredoc.Doc(` - $ cx scan cancel --scan-id - `), +func scanListSubCommand(scansWrapper wrappers.ScansWrapper) *cobra.Command { + listScansCmd := &cobra.Command{ + Use: "list", + Short: "List all scans in CxAST", + Long: "The list command provides a list of all the scans in CxAST.", + Example: heredoc.Doc( + ` + $ cx scan list + `, + ), Annotations: map[string]string{ - "command:doc": heredoc.Doc(` - https://checkmarx.atlassian.net/wiki/x/aY2Xtw - `), + "command:doc": heredoc.Doc( + ` + https://checkmarx.atlassian.net/wiki/x/K46Xtw + `, + ), }, - RunE: runCancelScanCommand(scansWrapper), + RunE: runListScansCommand(scansWrapper), } - addScanIDFlag(cancelScanCmd, "One or more scan IDs to cancel, ex: ,,...") + listScansCmd.PersistentFlags().StringSlice(FilterFlag, []string{}, filterScanListFlagUsage) + return listScansCmd +} - tagsCmd := &cobra.Command{ - Use: "tags", - Short: "Get a list of all available tags to filter by", - Long: "The tags command enables the ability to provide a list of all the available tags in CxAST.", - Example: heredoc.Doc(` - $ cx scan tags - `), +func scanCreateSubCommand( + scansWrapper wrappers.ScansWrapper, + uploadsWrapper wrappers.UploadsWrapper, + resultsWrapper wrappers.ResultsWrapper, +) *cobra.Command { + createScanCmd := &cobra.Command{ + Use: "create", + Short: "Create and run a new scan", + Long: "The create command enables the ability to create and run a new scan in CxAST.", + Example: heredoc.Doc( + ` + $ cx scan create --project-name --sources + `, + ), Annotations: map[string]string{ - "command:doc": heredoc.Doc(` - https://checkmarx.atlassian.net/wiki/x/546Xtw - `), + "command:doc": heredoc.Doc( + ` + https://checkmarx.atlassian.net/wiki/x/WguYtw + `, + ), }, - RunE: runGetTagsCommand(scansWrapper), + RunE: runCreateScanCommand(scansWrapper, uploadsWrapper, resultsWrapper), } - addFormatFlagToMultipleCommands([]*cobra.Command{listScansCmd, showScanCmd, workflowScanCmd}, - util.FormatTable, util.FormatList, util.FormatJSON) - addFormatFlagToMultipleCommands([]*cobra.Command{createScanCmd}, - util.FormatList, util.FormatTable, util.FormatJSON) - scanCmd.AddCommand(createScanCmd, showScanCmd, workflowScanCmd, listScansCmd, deleteScanCmd, cancelScanCmd, tagsCmd) - return scanCmd + createScanCmd.PersistentFlags().BoolP(WaitFlag, "", false, "Wait for scan completion (default true)") + createScanCmd.PersistentFlags().IntP(WaitDelayFlag, "", WaitDelayDefault, "Polling wait time in seconds") + createScanCmd.PersistentFlags().StringP( + SourcesFlag, + SourcesFlagSh, + "", + "Sources like: directory, zip file or git URL.", + ) + createScanCmd.PersistentFlags().StringP( + SourceDirFilterFlag, + SourceDirFilterFlagSh, + "", + "Source file filtering pattern", + ) + createScanCmd.PersistentFlags().StringP( + IncludeFilterFlag, + IncludeFilterFlagSh, + "", + "Only files scannable by AST are included by default."+ + " Add a comma separated list of extra inclusions, ex: *zip,file.txt", + ) + createScanCmd.PersistentFlags().String(ProjectName, "", "Name of the project") + err := createScanCmd.MarkPersistentFlagRequired(ProjectName) + if err != nil { + log.Fatal(err) + } + createScanCmd.PersistentFlags().String(IncrementalSast, "false", "Incremental SAST scan should be performed.") + createScanCmd.PersistentFlags().String(PresetName, "", "The name of the Checkmarx preset to use.") + createScanCmd.PersistentFlags().String(ScanTypes, "", "Scan types, ex: (sast,kics,sca)") + createScanCmd.PersistentFlags().String(TagList, "", "List of tags, ex: (tagA,tagB:val,etc)") + createScanCmd.PersistentFlags().StringP(BranchFlag, BranchFlagSh, commonParams.Branch, BranchFlagUsage) + // Link the environment variable to the CLI argument(s). + err = viper.BindPFlag(commonParams.BranchKey, createScanCmd.PersistentFlags().Lookup(BranchFlag)) + if err != nil { + log.Fatal(err) + } + return createScanCmd } func findProject(projectName string) string {