Skip to content

Commit

Permalink
fix(cli): update boolean logic and add package parser util (#2449)
Browse files Browse the repository at this point in the history
* Update isHelpOrVersionCmd logic & add utils

* Update packages/cli/src/lib/utils/packageParser.ts

Co-authored-by: RyanDagg <ryandagg@users.noreply.github.com>

* Update packages/cli/src/lib/utils/packageParser.ts

Co-authored-by: RyanDagg <ryandagg@users.noreply.github.com>

* Update logic with refactored package parser

---------

Co-authored-by: RyanDagg <ryandagg@users.noreply.github.com>
  • Loading branch information
zwhitfield3 and ryandagg committed Aug 17, 2023
1 parent 0a4a28e commit 9c7099c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
48 changes: 22 additions & 26 deletions packages/cli/src/global_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Rollbar from 'rollbar'
import {APIClient} from '@heroku-cli/command'
import {Config} from '@oclif/core'
import opentelemetry, {SpanStatusCode} from '@opentelemetry/api'
import {getAllHelpFlags, getAllVersionFlags} from './lib/utils/packageParser'
const {Resource} = require('@opentelemetry/resources')
const {SemanticResourceAttributes} = require('@opentelemetry/semantic-conventions')
const {registerInstrumentations} = require('@opentelemetry/instrumentation')
Expand Down Expand Up @@ -85,42 +86,37 @@ export function initializeInstrumentation() {
export function setupTelemetry(config: any, opts: any) {
const now = new Date()
const cmdStartTime = now.getTime()
const isHelpOrVersionCmd = (opts.id === 'version' || opts.id === '--help')
const isHelpOrVersionCmd = (getAllVersionFlags().includes(opts.id) || getAllHelpFlags().includes(opts.id))
const isRegularCmd = Boolean(opts.Command)

const baseTelemetryObject = {
command: opts.id,
os: config.platform,
version: config.version,
exitCode: 0,
exitState: 'successful',
cliRunDuration: 0,
commandRunDuration: cmdStartTime,
lifecycleHookCompletion: {
init: true,
prerun: false,
postrun: false,
command_not_found: false,
},
isVersionOrHelp: true,
}

if (isHelpOrVersionCmd) {
return {
command: opts.id,
os: config.platform,
version: config.version,
exitCode: 0,
exitState: 'successful',
cliRunDuration: 0,
commandRunDuration: cmdStartTime,
lifecycleHookCompletion: {
init: true,
prerun: false,
postrun: false,
command_not_found: false,
},
isVersionOrHelp: true,
}
return baseTelemetryObject
}

if (isRegularCmd) {
return {
...baseTelemetryObject,
command: opts.Command.id,
os: config.platform,
version: config.version,
exitCode: 0,
exitState: 'successful',
cliRunDuration: 0,
commandRunDuration: cmdStartTime,
lifecycleHookCompletion: {
init: true,
...baseTelemetryObject.lifecycleHookCompletion,
prerun: true,
postrun: false,
command_not_found: false,
},
isVersionOrHelp: false,
}
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/hooks/init/version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Hook} from '@oclif/core'
import {getAllVersionFlags} from '../../lib/utils/packageParser'

const allowlist = [
'HEROKU_API_KEY',
Expand All @@ -12,7 +13,7 @@ const allowlist = [
]

const version: Hook.Init = async function () {
if (['-v', '--version', 'version'].includes(process.argv[2])) {
if (getAllVersionFlags().includes(process.argv[2])) {
for (const env of allowlist) {
if (process.env[env]) {
const value = env === 'HEROKU_API_KEY' ? 'to [REDACTED]' : `to ${process.env[env]}`
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/lib/utils/packageParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {oclif} = require('../../../package.json')

export function getAllVersionFlags() {
return [...oclif.additionalVersionFlags, '--version']
}

export function getAllHelpFlags() {
return [...oclif.additionalHelpFlags, '--help', 'help']
}

0 comments on commit 9c7099c

Please sign in to comment.