Skip to content

Commit

Permalink
AST-4609 mark project name as a required flag (#259)
Browse files Browse the repository at this point in the history
* AST-4609 mark project name as a required flag

* AST-4609 split scan command setup in multiple methods
  • Loading branch information
diogopcx authored Aug 19, 2021
1 parent fe0c970 commit 8a30162
Showing 1 changed file with 187 additions and 99 deletions.
286 changes: 187 additions & 99 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Project Name> --sources <path or repository url>
`),
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 <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: <scan-id>,<scan-id>,...")
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 <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 <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: <scan-id>,<scan-id>,...")
return deleteScanCmd
}

func scanWorkflowSubCommand(scansWrapper wrappers.ScansWrapper) *cobra.Command {
workflowScanCmd := &cobra.Command{
Use: "workflow <scan id>",
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 <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 <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 <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: <scan-id>,<scan-id>,...")
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 <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: <scan-id>,<scan-id>,...")
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 <Project Name> --sources <path or repository url>
`,
),
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 {
Expand Down

0 comments on commit 8a30162

Please sign in to comment.