Skip to content

Commit 341022a

Browse files
committed
Improve suggestion handling
1 parent cf4ef17 commit 341022a

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/commands/scan/cmd-scan-create.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { suggestTarget } from './suggest_target'
1313
import constants from '../../constants'
1414
import { meowOrExit } from '../../utils/meow-with-subcommands'
1515
import { getFlagListOutput } from '../../utils/output-formatting'
16-
import { getDefaultToken, setupSdk } from '../../utils/sdk'
16+
import { getDefaultToken } from '../../utils/sdk'
1717

1818
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1919

@@ -172,20 +172,17 @@ async function run(
172172
// If the current cwd is unknown and is used as a repo slug anyways, we will
173173
// first need to register the slug before we can use it.
174174
let repoDefaultBranch = ''
175-
176175
// Only do suggestions with an apiToken and when not in dryRun mode
177176
if (apiToken && !dryRun) {
178-
const socketSdk = await setupSdk()
179-
180177
if (!orgSlug) {
181-
const suggestion = await suggestOrgSlug(socketSdk)
178+
const suggestion = await suggestOrgSlug()
182179
if (suggestion) orgSlug = suggestion
183180
updatedInput = true
184181
}
185182

186183
// (Don't bother asking for the rest if we didn't get an org slug above)
187184
if (orgSlug && !repoName) {
188-
const suggestion = await suggestRepoSlug(socketSdk, orgSlug)
185+
const suggestion = await suggestRepoSlug(orgSlug)
189186
if (suggestion) {
190187
repoDefaultBranch = suggestion.defaultBranch
191188
repoName = suggestion.slug
@@ -201,15 +198,15 @@ async function run(
201198
}
202199
}
203200

204-
if (updatedInput) {
201+
if (updatedInput && repoName && branchName && orgSlug && targets?.length) {
205202
logger.error(
206203
'Note: You can invoke this command next time to skip the interactive questions:'
207204
)
208205
logger.error('```')
209206
logger.error(
210207
` socket scan create [other flags...] --repo ${repoName} --branch ${branchName} ${orgSlug} ${targets.join(' ')}`
211208
)
212-
logger.error('```')
209+
logger.error('```\n')
213210
}
214211

215212
if (!orgSlug || !repoName || !branchName || !targets.length) {

src/commands/scan/suggest-org-slug.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { logger } from '@socketsecurity/registry/lib/logger'
12
import { select } from '@socketsecurity/registry/lib/prompts'
2-
import { SocketSdk } from '@socketsecurity/sdk'
33

44
import { handleApiCall } from '../../utils/api'
5+
import { setupSdk } from '../../utils/sdk'
56

6-
export async function suggestOrgSlug(
7-
socketSdk: SocketSdk
8-
): Promise<string | void> {
7+
export async function suggestOrgSlug(): Promise<string | void> {
8+
const socketSdk = await setupSdk()
99
const result = await handleApiCall(
1010
socketSdk.getOrganizations(),
1111
'looking up organizations'
@@ -33,6 +33,8 @@ export async function suggestOrgSlug(
3333
return proceed
3434
}
3535
} else {
36-
// TODO: in verbose mode, report this error to stderr
36+
logger.fail(
37+
'Failed to lookup organization list from API, unable to suggest.'
38+
)
3739
}
3840
}

src/commands/scan/suggest-repo-slug.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import path from 'node:path'
22
import process from 'node:process'
33

4+
import { logger } from '@socketsecurity/registry/lib/logger'
45
import { select } from '@socketsecurity/registry/lib/prompts'
5-
import { SocketSdk } from '@socketsecurity/sdk'
66

77
import { handleApiCall } from '../../utils/api'
8+
import { setupSdk } from '../../utils/sdk'
89

9-
export async function suggestRepoSlug(
10-
socketSdk: SocketSdk,
11-
orgSlug: string
12-
): Promise<{
10+
export async function suggestRepoSlug(orgSlug: string): Promise<{
1311
slug: string
1412
defaultBranch: string
1513
} | void> {
14+
const socketSdk = await setupSdk()
15+
1616
// Same as above, but if there's a repo with the same name as cwd then
1717
// default the selection to that name.
1818
const result = await handleApiCall(
@@ -28,6 +28,7 @@ export async function suggestRepoSlug(
2828
}),
2929
'looking up known repos'
3030
)
31+
3132
// Ignore a failed request here. It was not the primary goal of
3233
// running this command and reporting it only leads to end-user confusion.
3334
if (result.success) {
@@ -92,7 +93,7 @@ export async function suggestRepoSlug(
9293
return { slug: repoName, defaultBranch: repoDefaultBranch }
9394
}
9495
} else {
95-
// TODO: in verbose mode, report this error to stderr
96+
logger.fail('Failed to lookup repo list from API, unable to suggest.')
9697
}
9798
}
9899

0 commit comments

Comments
 (0)