Skip to content

Commit c6a6695

Browse files
authored
Merge pull request #321 from SocketDev/ref_analytics
Refactor the analytics command
2 parents 02179ab + 2bd8b59 commit c6a6695

File tree

7 files changed

+219
-195
lines changed

7 files changed

+219
-195
lines changed

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import updateNotifier from 'tiny-updater'
88
import colors from 'yoctocolors-cjs'
99

1010
import { actionCommand } from './commands/action'
11-
import { analyticsCommand } from './commands/analytics'
11+
import { analyticsCommand } from './commands/analytics/analytics-command'
1212
import { auditLogCommand } from './commands/audit-log'
1313
import { cdxgenCommand } from './commands/cdxgen'
1414
import { dependenciesCommand } from './commands/dependencies'
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import meowOrDie from 'meow'
2+
import colors from 'yoctocolors-cjs'
3+
4+
import { runAnalytics } from './run-analytics.ts'
5+
import { commonFlags, outputFlags } from '../../flags'
6+
import { AuthError, InputError } from '../../utils/errors'
7+
import { getFlagListOutput } from '../../utils/output-formatting'
8+
import { getDefaultToken } from '../../utils/sdk.ts'
9+
10+
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
11+
import type { CommandContext } from '../types.ts'
12+
13+
const config: CliCommandConfig = {
14+
commandName: 'analytics',
15+
description: `Look up analytics data`,
16+
hidden: false,
17+
flags: {
18+
...commonFlags,
19+
...outputFlags,
20+
scope: {
21+
type: 'string',
22+
shortFlag: 's',
23+
default: 'org',
24+
description:
25+
"Scope of the analytics data - either 'org' or 'repo', default: org"
26+
},
27+
time: {
28+
type: 'number',
29+
shortFlag: 't',
30+
default: 7,
31+
description: 'Time filter - either 7, 30 or 90, default: 7'
32+
},
33+
repo: {
34+
type: 'string',
35+
shortFlag: 'r',
36+
default: '',
37+
description: 'Name of the repository'
38+
},
39+
file: {
40+
type: 'string',
41+
shortFlag: 'f',
42+
default: '',
43+
description: 'Path to a local file to save the output'
44+
}
45+
},
46+
help: (parentName, { commandName, flags }) => `
47+
Usage
48+
$ ${parentName} ${commandName} --scope=<scope> --time=<time filter>
49+
50+
Default parameters are set to show the organization-level analytics over the
51+
last 7 days.
52+
53+
Options
54+
${getFlagListOutput(flags, 6)}
55+
56+
Examples
57+
$ ${parentName} ${commandName} --scope=org --time=7
58+
$ ${parentName} ${commandName} --scope=org --time=30
59+
$ ${parentName} ${commandName} --scope=repo --repo=test-repo --time=30
60+
`
61+
}
62+
63+
export const analyticsCommand = {
64+
description: config.description,
65+
hidden: config.hidden,
66+
run: run
67+
}
68+
69+
async function run(
70+
argv: readonly string[],
71+
importMeta: ImportMeta,
72+
{ parentName }: { parentName: string }
73+
): Promise<void> {
74+
const cli = meowOrDie(config.help(parentName, config), {
75+
argv,
76+
description: config.description,
77+
importMeta,
78+
flags: config.flags
79+
})
80+
81+
const { repo, scope, time } = cli.flags
82+
83+
if (scope !== 'org' && scope !== 'repo') {
84+
throw new InputError("The scope must either be 'org' or 'repo'")
85+
}
86+
87+
if (time !== 7 && time !== 30 && time !== 90) {
88+
throw new InputError('The time filter must either be 7, 30 or 90')
89+
}
90+
91+
if (scope === 'repo' && !repo) {
92+
console.error(
93+
`${colors.bgRed(colors.white('Input error'))}: Please provide a repository name when using the repository scope.`
94+
)
95+
cli.showHelp()
96+
}
97+
98+
const apiToken = getDefaultToken()
99+
if (!apiToken) {
100+
throw new AuthError(
101+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API token.'
102+
)
103+
}
104+
105+
return await runAnalytics(apiToken, {
106+
scope,
107+
time,
108+
repo,
109+
outputJson: cli.flags['json'],
110+
file: cli.flags['file']
111+
} as CommandContext)
112+
}

0 commit comments

Comments
 (0)