-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathfetch-default-org-slug.mts
More file actions
64 lines (55 loc) · 1.75 KB
/
fetch-default-org-slug.mts
File metadata and controls
64 lines (55 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { debugFn } from '@socketsecurity/registry/lib/debug'
import constants from '../../constants.mts'
import { getConfigValueOrUndef } from '../../utils/config.mts'
import { fetchOrganization } from '../organization/fetch-organization-list.mts'
import type { CResult } from '../../types.mts'
// Use the config defaultOrg when set, otherwise discover from remote.
export async function getDefaultOrgSlug(
silence?: boolean,
): Promise<CResult<string>> {
const defaultOrgResult = getConfigValueOrUndef('defaultOrg')
if (defaultOrgResult) {
debugFn(
'notice',
'use: org from "defaultOrg" value of socket/settings local app data',
defaultOrgResult,
)
return { ok: true, data: defaultOrgResult }
}
const envOrgSlug = constants.ENV.SOCKET_CLI_ORG_SLUG
if (envOrgSlug) {
debugFn(
'notice',
'use: org from SOCKET_CLI_ORG_SLUG environment variable',
envOrgSlug,
)
return { ok: true, data: envOrgSlug }
}
const orgsCResult = await fetchOrganization({ silence })
if (!orgsCResult.ok) {
return orgsCResult
}
const { organizations } = orgsCResult.data
const keys = Object.keys(organizations)
if (!keys.length) {
return {
ok: false,
message: 'Failed to establish identity',
data: `No organization associated with the Socket API token. Unable to continue.`,
}
}
const slug = (organizations as any)[keys[0]!]?.name ?? undefined
if (!slug) {
return {
ok: false,
message: 'Failed to establish identity',
data: `Cannot determine the default organization for the API token. Unable to continue.`,
}
}
debugFn('notice', 'resolve: org from Socket API', slug)
return {
ok: true,
message: 'Retrieved default org from server',
data: slug,
}
}