@@ -2,15 +2,10 @@ import process from 'node:process'
22
33import colors from 'yoctocolors-cjs'
44
5- import { Spinner } from '@socketsecurity/registry/lib/spinner'
6-
75import { createFullScan } from './create-full-scan'
8- import { handleUnsuccessfulApiResponse } from '../../utils/api'
9- import { AuthError } from '../../utils/errors'
106import { meowOrExit } from '../../utils/meow-with-subcommands'
117import { getFlagListOutput } from '../../utils/output-formatting'
12- import { getPackageFilesFullScans } from '../../utils/path-resolve'
13- import { getDefaultToken , setupSdk } from '../../utils/sdk'
8+ import { getDefaultToken } from '../../utils/sdk'
149
1510import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1611
@@ -75,6 +70,12 @@ const config: CliCommandConfig = {
7570 default : false ,
7671 description : 'Set as pending head'
7772 } ,
73+ readOnly : {
74+ type : 'boolean' ,
75+ default : false ,
76+ description :
77+ 'Similar to --dry-run except it can read from remote, stops before it would create an actual report'
78+ } ,
7879 tmp : {
7980 type : 'boolean' ,
8081 shortFlag : 't' ,
@@ -125,79 +126,46 @@ async function run(
125126 ? String ( cli . flags [ 'cwd' ] )
126127 : process . cwd ( )
127128
128- // Note exiting earlier to skirt a hidden auth requirement
129- if ( cli . flags [ 'dryRun' ] ) {
130- return console . log ( '[DryRun] Bailing now' )
131- }
132-
133- const socketSdk = await setupSdk ( )
134- const supportedFiles = await socketSdk
135- . getReportSupportedFiles ( )
136- . then ( res => {
137- if ( ! res . success )
138- handleUnsuccessfulApiResponse (
139- 'getReportSupportedFiles' ,
140- res ,
141- new Spinner ( )
142- )
143- // TODO: verify type at runtime? Consider it trusted data and assume type?
144- return ( res as any ) . data
145- } )
146- . catch ( ( cause : Error ) => {
147- throw new Error ( 'Failed getting supported files for report' , { cause } )
148- } )
149-
150- const packagePaths = await getPackageFilesFullScans (
151- cwd ,
152- targets ,
153- supportedFiles
154- )
129+ let { branch : branchName , repo : repoName } = cli . flags
155130
156- const { branch : branchName , repo : repoName } = cli . flags
131+ const apiToken = getDefaultToken ( )
157132
158- if ( ! orgSlug || ! repoName || ! branchName || ! packagePaths . length ) {
133+ if ( ! apiToken && ( ! orgSlug || ! repoName || ! branchName || ! targets . length ) ) {
134+ // Without api token we cannot recover because we can't request more info
135+ // from the server, to match and help with the current cwd/git status.
159136 // Use exit status of 2 to indicate incorrect usage, generally invalid
160137 // options or missing arguments.
161138 // https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
162139 process . exitCode = 2
163- console . error ( `${ colors . bgRed ( colors . white ( 'Input error' ) ) } : Please provide the required fields:\n
164- - Org name as the first argument ${ ! orgSlug ? colors . red ( '(missing!)' ) : colors . green ( '(ok)' ) } \n
165- - Repository name using --repo ${ ! repoName ? colors . red ( '(missing!)' ) : colors . green ( '(ok)' ) } \n
166- - Branch name using --branch ${ ! branchName ? colors . red ( '(missing!)' ) : colors . green ( '(ok)' ) } \n
167- - At least one TARGET (e.g. \`.\` or \`./package.json\`) ${
168- ! packagePaths . length
169- ? colors . red (
170- targets . length > 0
171- ? '(TARGET' +
172- ( targets . length ? 's' : '' ) +
173- ' contained no matching/supported files!)'
174- : '(missing)'
175- )
176- : colors . green ( '(ok)' )
177- } \n`)
140+ console . error ( `
141+ ${ colors . bgRed ( colors . white ( 'Input error' ) ) } : Please provide the required fields:\n
142+ - Org name as the first argument ${ ! orgSlug ? colors . red ( '(missing!)' ) : colors . green ( '(ok)' ) } \n
143+ - Repository name using --repo ${ ! repoName ? colors . red ( '(missing!)' ) : colors . green ( '(ok)' ) } \n
144+ - Branch name using --branch ${ ! branchName ? colors . red ( '(missing!)' ) : colors . green ( '(ok)' ) } \n
145+ - At least one TARGET (e.g. \`.\` or \`./package.json\`) ${ ! targets . length ? '(missing)' : colors . green ( '(ok)' ) } \n
146+ (Additionally, no API Token was set so we cannot auto-discover these details)\n
147+ ` )
178148 return
179149 }
180150
181- const apiToken = getDefaultToken ( )
182- if ( ! apiToken ) {
183- throw new AuthError (
184- 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
185- )
151+ // Note exiting earlier to skirt a hidden auth requirement
152+ if ( cli . flags [ 'dryRun' ] ) {
153+ return console . log ( '[DryRun] Bailing now' )
186154 }
187155
188156 await createFullScan ( {
189- apiToken,
190- orgSlug,
191- repoName : repoName as string ,
192157 branchName : branchName as string ,
158+ commitHash : ( cli . flags [ 'commitHash' ] as string ) ?? '' ,
193159 commitMessage : ( cli . flags [ 'commitMessage' ] as string ) ?? '' ,
160+ committers : ( cli . flags [ 'committers' ] as string ) ?? '' ,
161+ cwd,
194162 defaultBranch : Boolean ( cli . flags [ 'defaultBranch' ] ) ,
163+ orgSlug,
195164 pendingHead : Boolean ( cli . flags [ 'pendingHead' ] ) ,
196- tmp : Boolean ( cli . flags [ 'tmp' ] ) ,
197- packagePaths,
198- cwd,
199- commitHash : ( cli . flags [ 'commitHash' ] as string ) ?? '' ,
200- committers : ( cli . flags [ 'committers' ] as string ) ?? '' ,
201- pullRequest : ( cli . flags [ 'pullRequest' ] as number ) ?? undefined
165+ pullRequest : ( cli . flags [ 'pullRequest' ] as number ) ?? undefined ,
166+ readOnly : Boolean ( cli . flags [ 'readOnly' ] ) ,
167+ repoName : repoName as string ,
168+ targets,
169+ tmp : Boolean ( cli . flags [ 'tmp' ] )
202170 } )
203171}
0 commit comments