Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions src/commands/analytics/cmd-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { displayAnalytics } from './display-analytics'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
import { getDefaultToken } from '../../utils/sdk'

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

Expand All @@ -21,6 +19,19 @@ const config: CliCommandConfig = {
flags: {
...commonFlags,
...outputFlags,
file: {
type: 'string',
shortFlag: 'f',
default: '-',
description:
'Path to a local file to save the output. Only valid with --json/--markdown. Defaults to stdout.'
},
repo: {
type: 'string',
shortFlag: 'r',
default: '',
description: 'Name of the repository. Only valid when scope=repo'
},
scope: {
type: 'string',
shortFlag: 's',
Expand All @@ -33,18 +44,6 @@ const config: CliCommandConfig = {
shortFlag: 't',
default: 7,
description: 'Time filter - either 7, 30 or 90, default: 7'
},
repo: {
type: 'string',
shortFlag: 'r',
default: '',
description: 'Name of the repository'
},
file: {
type: 'string',
shortFlag: 'f',
default: '',
description: 'Path to a local file to save the output'
}
},
help: (command, { flags }) => `
Expand Down Expand Up @@ -82,21 +81,32 @@ async function run(
parentName
})

const { repo, scope, time } = cli.flags
const { file, json, markdown, repo, scope, time } = cli.flags

const badScope = scope !== 'org' && scope !== 'repo'
const badTime = time !== 7 && time !== 30 && time !== 90
const badRepo = scope === 'repo' && !repo
const badFile = file !== '-' && !json && !markdown
const badFlags = json && markdown

if (badScope || badTime || badRepo) {
if (badScope || badTime || badRepo || badFile || badFlags) {
// Use exit status of 2 to indicate incorrect usage, generally invalid
// options or missing arguments.
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
process.exitCode = 2
logger.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
logger.error(
`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
- Scope must be "repo" or "org" ${badScope ? colors.red('(bad!)') : colors.green('(ok)')}\n
- The time filter must either be 7, 30 or 90 ${badTime ? colors.red('(bad!)') : colors.green('(ok)')}\n
- Repository name using --repo when scope is "repo" ${badRepo ? colors.red('(bad!)') : colors.green('(ok)')}\n`)
${scope === 'repo' ? `- Repository name using --repo when scope is "repo" ${badRepo ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}\n
${badFlags ? `- The \`--json\` and \`--markdown\` flags can not be used at the same time ${badFlags ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}\n
${badFile ? `- The \`--file\` flag is only valid when using \`--json\` or \`--markdown\` ${badFile ? colors.red('(bad!)') : colors.green('(ok)')}` : ''}\n
`
.trim()
.split('\n')
.filter(s => !!s.trim())
.join('\n') + '\n'
)
return
}

Expand All @@ -105,19 +115,11 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API token.'
)
}

return await displayAnalytics({
apiToken,
scope,
time,
repo: String(repo || ''),
outputJson: Boolean(cli.flags['json']),
filePath: String(cli.flags['file'] || '')
outputKind: json ? 'json' : markdown ? 'markdown' : 'print',
filePath: String(file || '')
})
}
Loading
Loading