Skip to content

Commit 01a2477

Browse files
committed
wip
1 parent c3abf4f commit 01a2477

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed

src/commands/diff-scan/get.ts

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import chalk from 'chalk'
2+
// @ts-ignore
3+
import chalkTable from 'chalk-table'
4+
import meow from 'meow'
5+
import ora from 'ora'
6+
7+
import { outputFlags } from '../../flags'
8+
import {
9+
handleApiCall,
10+
handleUnsuccessfulApiResponse
11+
} from '../../utils/api-helpers'
12+
import { printFlagList } from '../../utils/formatting'
13+
import { getDefaultKey, setupSdk } from '../../utils/sdk'
14+
15+
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
16+
import type { Ora } from 'ora'
17+
import { AuthError } from '../../utils/errors'
18+
19+
export const get: CliSubcommand = {
20+
description: 'Get a diff scan for an organization',
21+
async run(argv, importMeta, { parentName }) {
22+
const name = `${parentName} get`
23+
const input = setupCommand(name, get.description, argv, importMeta)
24+
if (input) {
25+
const apiKey = getDefaultKey()
26+
if(!apiKey){
27+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
28+
}
29+
const spinnerText = 'Listing scans... \n'
30+
const spinner = ora(spinnerText).start()
31+
await listOrgFullScan(input.orgSlug, input, spinner, apiKey)
32+
}
33+
}
34+
}
35+
36+
const listFullScanFlags: { [key: string]: any } = {
37+
sort: {
38+
type: 'string',
39+
shortFlag: 's',
40+
default: 'created_at',
41+
description:
42+
'Sorting option (`name` or `created_at`) - default is `created_at`'
43+
},
44+
direction: {
45+
type: 'string',
46+
shortFlag: 'd',
47+
default: 'desc',
48+
description: 'Direction option (`desc` or `asc`) - Default is `desc`'
49+
},
50+
perPage: {
51+
type: 'number',
52+
shortFlag: 'pp',
53+
default: 30,
54+
description: 'Results per page - Default is 30'
55+
},
56+
page: {
57+
type: 'number',
58+
shortFlag: 'p',
59+
default: 1,
60+
description: 'Page number - Default is 1'
61+
},
62+
fromTime: {
63+
type: 'string',
64+
shortFlag: 'f',
65+
default: '',
66+
description: 'From time - as a unix timestamp'
67+
},
68+
untilTime: {
69+
type: 'string',
70+
shortFlag: 'u',
71+
default: '',
72+
description: 'Until time - as a unix timestamp'
73+
}
74+
}
75+
76+
// Internal functions
77+
78+
type CommandContext = {
79+
outputJson: boolean
80+
outputMarkdown: boolean
81+
orgSlug: string
82+
sort: string
83+
direction: string
84+
per_page: number
85+
page: number
86+
from_time: string
87+
until_time: string
88+
}
89+
90+
function setupCommand(
91+
name: string,
92+
description: string,
93+
argv: readonly string[],
94+
importMeta: ImportMeta
95+
): CommandContext | undefined {
96+
const flags: { [key: string]: any } = {
97+
...outputFlags,
98+
...listFullScanFlags
99+
}
100+
101+
const cli = meow(
102+
`
103+
Usage
104+
$ ${name} <org slug>
105+
106+
Options
107+
${printFlagList(flags, 6)}
108+
109+
Examples
110+
$ ${name} FakeOrg
111+
`,
112+
{
113+
argv,
114+
description,
115+
importMeta,
116+
flags
117+
}
118+
)
119+
120+
const {
121+
json: outputJson,
122+
markdown: outputMarkdown,
123+
sort,
124+
direction,
125+
perPage,
126+
page,
127+
fromTime,
128+
untilTime
129+
} = cli.flags
130+
131+
if (!cli.input[0]) {
132+
console.error(
133+
`${chalk.bgRed('Input error')}: Please specify an organization slug.\n`
134+
)
135+
cli.showHelp()
136+
return
137+
}
138+
139+
const { 0: orgSlug = '' } = cli.input
140+
141+
return <CommandContext>{
142+
outputJson,
143+
outputMarkdown,
144+
orgSlug,
145+
sort,
146+
direction,
147+
per_page: perPage,
148+
page,
149+
from_time: fromTime,
150+
until_time: untilTime
151+
}
152+
}
153+
154+
async function listOrgFullScan(
155+
orgSlug: string,
156+
input: CommandContext,
157+
spinner: Ora,
158+
apiKey: string
159+
): Promise<void> {
160+
const socketSdk = await setupSdk(apiKey)
161+
const result = await handleApiCall(
162+
socketSdk.getOrgFullScanList(orgSlug, input),
163+
'Listing scans'
164+
)
165+
166+
if (!result.success) {
167+
handleUnsuccessfulApiResponse('getOrgFullScanList', result, spinner)
168+
return
169+
}
170+
spinner.stop()
171+
172+
console.log(`\n Listing scans for: ${orgSlug}\n`)
173+
174+
const options = {
175+
columns: [
176+
{ field: 'id', name: chalk.magenta('ID') },
177+
{ field: 'report_url', name: chalk.magenta('Scan URL') },
178+
{ field: 'branch', name: chalk.magenta('Branch') },
179+
{ field: 'created_at', name: chalk.magenta('Created at') }
180+
]
181+
}
182+
183+
const formattedResults = result.data.results.map(d => {
184+
return {
185+
id: d.id,
186+
report_url: chalk.underline(`${d.html_report_url}`),
187+
created_at: d.created_at
188+
? new Date(d.created_at).toLocaleDateString('en-us', {
189+
year: 'numeric',
190+
month: 'numeric',
191+
day: 'numeric'
192+
})
193+
: '',
194+
branch: d.branch
195+
}
196+
})
197+
198+
console.log(`${chalkTable(options, formattedResults)}\n`)
199+
}

src/commands/diff-scan/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { get } from './get'
2+
import { meowWithSubcommands } from '../../utils/meow-with-subcommands'
3+
4+
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
5+
6+
const description = 'Diff scans related commands'
7+
8+
export const diffScan: CliSubcommand = {
9+
description,
10+
run: async (argv, importMeta, { parentName }) => {
11+
await meowWithSubcommands(
12+
{
13+
get
14+
},
15+
{
16+
argv,
17+
description,
18+
importMeta,
19+
name: parentName + ' diff-scan'
20+
}
21+
)
22+
}
23+
}

0 commit comments

Comments
 (0)