Skip to content

Commit ad4cf00

Browse files
committed
Refactor remaining commands
1 parent 87c7c2d commit ad4cf00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2247
-1959
lines changed

src/cli.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ import { messageWithCauses, stackWithCauses } from 'pony-cause'
77
import updateNotifier from 'tiny-updater'
88
import colors from 'yoctocolors-cjs'
99

10-
import { cmdAction } from './commands/action/cmd-action.ts'
11-
import { cmdAnalytics } from './commands/analytics/cmd-analytics.ts'
12-
import { cmdAuditLog } from './commands/audit-log/cmd-audit-log.ts'
13-
import { cmdCdxgen } from './commands/cdxgen/cmd-cdxgen.ts'
14-
import { cmdScanCreate } from './commands/dependencies/cmd-dependencies.ts'
15-
import { cmdDiffScan } from './commands/diff-scan/cmd-diff-scan.ts'
16-
import { cmdFix } from './commands/fix/cmd-fix.ts'
17-
import { cmdInfo } from './commands/info/cmd-info.ts'
18-
import { loginCommand } from './commands/login'
19-
import { logoutCommand } from './commands/logout'
20-
import { cmdManifest } from './commands/manifest/cmd-manifest.ts'
21-
import { npmCommand } from './commands/npm'
22-
import { npxCommand } from './commands/npx'
23-
import { optimizeCommand } from './commands/optimize'
24-
import { organizationCommand } from './commands/organization'
25-
import { rawNpmCommand } from './commands/raw-npm'
26-
import { rawNpxCommand } from './commands/raw-npx'
27-
import { cmdReport } from './commands/report/cmd-report.ts'
28-
import { cmdRepos } from './commands/repos/cmd-repos.ts'
29-
import { cmdScan } from './commands/scan/cmd-scan.ts'
30-
import { threatFeedCommand } from './commands/threat-feed'
31-
import { wrapperCommand } from './commands/wrapper'
10+
import { cmdAction } from './commands/action/cmd-action'
11+
import { cmdAnalytics } from './commands/analytics/cmd-analytics'
12+
import { cmdAuditLog } from './commands/audit-log/cmd-audit-log'
13+
import { cmdCdxgen } from './commands/cdxgen/cmd-cdxgen'
14+
import { cmdScanCreate } from './commands/dependencies/cmd-dependencies'
15+
import { cmdDiffScan } from './commands/diff-scan/cmd-diff-scan'
16+
import { cmdFix } from './commands/fix/cmd-fix'
17+
import { cmdInfo } from './commands/info/cmd-info'
18+
import { cmdLogin } from './commands/login/cmd-login'
19+
import { cmdLogout } from './commands/logout/cmd-logout'
20+
import { cmdManifest } from './commands/manifest/cmd-manifest'
21+
import { cmdNpm } from './commands/npm/cmd-npm'
22+
import { cmdNpx } from './commands/npx/cmd-npx'
23+
import { cmdOptimize } from './commands/optimize/cmd-optimize'
24+
import { cmdOrganization } from './commands/organization/cmd-organization'
25+
import { cmdRawNpm } from './commands/raw-npm/cmd-raw-npm'
26+
import { cmdRawNpx } from './commands/raw-npx/cmd-raw-npx'
27+
import { cmdReport } from './commands/report/cmd-report'
28+
import { cmdRepos } from './commands/repos/cmd-repos'
29+
import { cmdScan } from './commands/scan/cmd-scan'
30+
import { cmdThreatFeed } from './commands/threat-feed/cmd-threat-feed'
31+
import { cmdWrapper } from './commands/wrapper/cmd-wrapper'
3232
import constants from './constants'
3333
import { AuthError, InputError } from './utils/errors'
3434
import { logSymbols } from './utils/logging'
@@ -51,23 +51,23 @@ void (async () => {
5151
cdxgen: cmdCdxgen,
5252
fix: cmdFix,
5353
info: cmdInfo,
54-
login: loginCommand,
55-
logout: logoutCommand,
56-
npm: npmCommand,
57-
npx: npxCommand,
58-
optimize: optimizeCommand,
59-
organization: organizationCommand,
60-
'raw-npm': rawNpmCommand,
61-
'raw-npx': rawNpxCommand,
54+
login: cmdLogin,
55+
logout: cmdLogout,
56+
npm: cmdNpm,
57+
npx: cmdNpx,
58+
optimize: cmdOptimize,
59+
organization: cmdOrganization,
60+
'raw-npm': cmdRawNpm,
61+
'raw-npx': cmdRawNpx,
6262
report: cmdReport,
63-
wrapper: wrapperCommand,
63+
wrapper: cmdWrapper,
6464
scan: cmdScan,
6565
'audit-log': cmdAuditLog,
6666
repos: cmdRepos,
6767
dependencies: cmdScanCreate,
6868
analytics: cmdAnalytics,
6969
'diff-scan': cmdDiffScan,
70-
'threat-feed': threatFeedCommand,
70+
'threat-feed': cmdThreatFeed,
7171
manifest: cmdManifest
7272
},
7373
{

src/commands/login.ts

Lines changed: 0 additions & 165 deletions
This file was deleted.

src/commands/login/apply-login.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { updateSetting } from '../../utils/settings.ts'
2+
3+
export function applyLogin(
4+
apiToken: string,
5+
enforcedOrgs: Array<string>,
6+
apiBaseUrl: string | undefined,
7+
apiProxy: string | undefined
8+
) {
9+
updateSetting('enforcedOrgs', enforcedOrgs)
10+
updateSetting('apiToken', apiToken)
11+
updateSetting('apiBaseUrl', apiBaseUrl)
12+
updateSetting('apiProxy', apiProxy)
13+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import terminalLink from 'terminal-link'
2+
3+
import {
4+
type Separator,
5+
confirm,
6+
password,
7+
select
8+
} from '@socketsecurity/registry/lib/prompts'
9+
import { Spinner } from '@socketsecurity/registry/lib/spinner'
10+
11+
import { applyLogin } from './apply-login.ts'
12+
import constants from '../../constants.ts'
13+
import { AuthError } from '../../utils/errors.ts'
14+
import { setupSdk } from '../../utils/sdk.ts'
15+
import { getSetting } from '../../utils/settings.ts'
16+
17+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
18+
19+
// TODO: this type should come from a general Socket REST API type doc
20+
type Choice<Value> = {
21+
value: Value
22+
name?: string
23+
description?: string
24+
disabled?: boolean | string
25+
type?: never
26+
}
27+
type OrgChoice = Choice<string>
28+
type OrgChoices = Array<Separator | OrgChoice>
29+
const { SOCKET_PUBLIC_API_TOKEN } = constants
30+
31+
export async function attemptLogin(
32+
apiBaseUrl: string | undefined,
33+
apiProxy: string | undefined
34+
) {
35+
const apiToken =
36+
(await password({
37+
message: `Enter your ${terminalLink(
38+
'Socket.dev API key',
39+
'https://docs.socket.dev/docs/api-keys'
40+
)} (leave blank for a public key)`
41+
})) || SOCKET_PUBLIC_API_TOKEN
42+
43+
apiBaseUrl ??= getSetting('apiBaseUrl') ?? undefined
44+
apiProxy ??= getSetting('apiProxy') ?? undefined
45+
46+
const spinner = new Spinner({ text: 'Verifying API key...' }).start()
47+
48+
let orgs: SocketSdkReturnType<'getOrganizations'>['data']
49+
try {
50+
const sdk = await setupSdk(apiToken, apiBaseUrl, apiProxy)
51+
const result = await sdk.getOrganizations()
52+
if (!result.success) {
53+
throw new AuthError()
54+
}
55+
orgs = result.data
56+
spinner.success('API key verified')
57+
} catch {
58+
spinner.error('Invalid API key')
59+
return
60+
}
61+
62+
const enforcedChoices: OrgChoices = Object.values(orgs.organizations)
63+
.filter(org => org?.plan === 'enterprise')
64+
.map(org => ({
65+
name: org.name,
66+
value: org.id
67+
}))
68+
69+
let enforcedOrgs: Array<string> = []
70+
71+
if (enforcedChoices.length > 1) {
72+
const id = <string | null>await select({
73+
message:
74+
"Which organization's policies should Socket enforce system-wide?",
75+
choices: enforcedChoices.concat({
76+
name: 'None',
77+
value: '',
78+
description: 'Pick "None" if this is a personal device'
79+
})
80+
})
81+
if (id) {
82+
enforcedOrgs = [id]
83+
}
84+
} else if (enforcedChoices.length) {
85+
const confirmOrg = await confirm({
86+
message: `Should Socket enforce ${(enforcedChoices[0] as OrgChoice)?.name}'s security policies system-wide?`,
87+
default: true
88+
})
89+
if (confirmOrg) {
90+
const existing = <OrgChoice>enforcedChoices[0]
91+
if (existing) {
92+
enforcedOrgs = [existing.value]
93+
}
94+
}
95+
}
96+
97+
const oldToken = getSetting('apiToken')
98+
try {
99+
applyLogin(apiToken, enforcedOrgs, apiBaseUrl, apiProxy)
100+
spinner.success(`API credentials ${oldToken ? 'updated' : 'set'}`)
101+
} catch {
102+
spinner.error(`API login failed`)
103+
}
104+
}

0 commit comments

Comments
 (0)