Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add telemetry to track chectl command usage #992

Closed
wants to merge 108 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
fd0e334
Add telemetry for chectl
flacatus Nov 17, 2020
9d0ff02
Merge
flacatus Nov 23, 2020
3c3e119
Implement chectl telemetry flag for every command in part
flacatus Nov 24, 2020
b19d82d
Merge branch 'master' into telemetry
flacatus Nov 24, 2020
5182f45
feat: Add class for chectll configurations and improve segment commands
flacatus Nov 26, 2020
7841c01
Merge branch 'telemetry' of github.com:flacatus/chectl into telemetry
flacatus Nov 26, 2020
e73c59b
Merge master
flacatus Nov 26, 2020
e4a8fe2
Fixes
flacatus Nov 27, 2020
436181a
Fixes
flacatus Nov 27, 2020
5b5d30b
Improve chectl config class
flacatus Nov 30, 2020
6cbc448
Fixes
flacatus Nov 30, 2020
9464418
Merge Master
flacatus Nov 30, 2020
a7e32d3
Fixes
flacatus Nov 30, 2020
ff4e780
Refactor ConfigManager
tolusha Nov 30, 2020
52a2f2f
Small fixup
tolusha Nov 30, 2020
d66c0f6
Fixes
flacatus Dec 1, 2020
e1dbf6e
Merge remote-tracking branch 'upstream/master' into telemetry
flacatus Dec 1, 2020
83fee44
Test deploy
flacatus Dec 14, 2020
5d4f672
Test deploy
flacatus Dec 14, 2020
8f4738f
Test deploy
flacatus Dec 14, 2020
f41b524
Test deploy
flacatus Dec 14, 2020
37a1e6c
Test deploy
flacatus Dec 14, 2020
c240167
Test deploy
flacatus Dec 14, 2020
fc14ee2
Test deploy
flacatus Dec 14, 2020
d041fad
Test deploy
flacatus Dec 14, 2020
c8d9cb4
Test deploy
flacatus Dec 14, 2020
3c6613c
Test deploy
flacatus Dec 14, 2020
33cd252
Test deploy
flacatus Dec 14, 2020
c92f9f2
Test deploy
flacatus Dec 14, 2020
0119682
Test deploy
flacatus Dec 14, 2020
cc09501
Test deploy
flacatus Dec 14, 2020
b814f67
Test deploy
flacatus Dec 14, 2020
1cd0796
Test deploy
flacatus Dec 14, 2020
49595fb
Test deploy
flacatus Dec 14, 2020
1578075
Test deploy
flacatus Dec 14, 2020
0a3b112
Test deploy
flacatus Dec 14, 2020
4c31640
Test deploy
flacatus Dec 14, 2020
6eb6d21
Test deploy
flacatus Dec 14, 2020
01e5c77
Test deploy
flacatus Dec 14, 2020
d96a7b4
Test deploy
flacatus Dec 14, 2020
dfb005b
Test deploy
flacatus Dec 14, 2020
dc91eda
Test deploy
flacatus Dec 14, 2020
5e8be7a
Test deploy
flacatus Dec 14, 2020
f9a21d1
Test deploy
flacatus Dec 14, 2020
9b10a0d
Test deploy
flacatus Dec 14, 2020
abf74a1
Test deploy
flacatus Dec 14, 2020
c446044
Test deploy
flacatus Dec 14, 2020
6fb4558
Update release-master.yml
flacatus Dec 14, 2020
9c09a2d
Update release-master.yml
flacatus Dec 14, 2020
f53607f
Update release-master.yml
flacatus Dec 14, 2020
2f23c07
Update release-master.yml
flacatus Dec 14, 2020
c89da16
Merge
flacatus Dec 14, 2020
a855535
Update release-master.yml
flacatus Dec 14, 2020
b7f836e
Merge branch 'master' of github.com:flacatus/chectl
flacatus Dec 14, 2020
35fdb07
Update release-master.yml
flacatus Dec 14, 2020
d721668
Update release-master.yml
flacatus Dec 14, 2020
03267ff
Merge branch 'master' of github.com:flacatus/chectl
flacatus Dec 14, 2020
f488be7
Update release-master.yml
flacatus Dec 14, 2020
9dae69c
Update release-master.yml
flacatus Dec 14, 2020
3816b55
Update release-master.yml
flacatus Dec 14, 2020
167b40c
Update release-master.yml
flacatus Dec 14, 2020
0b8ec53
Update release-master.yml
flacatus Dec 14, 2020
40e6bd6
Update release-master.yml
flacatus Dec 14, 2020
88f7c92
Update release-master.yml
flacatus Dec 14, 2020
ebe265a
Update release-master.yml
flacatus Dec 14, 2020
13bd179
Update release-master.yml
flacatus Dec 14, 2020
c9a1a26
Update release-master.yml
flacatus Dec 14, 2020
369203a
Update release-master.yml
flacatus Dec 14, 2020
d973914
Update release-master.yml
flacatus Dec 14, 2020
e8e3c22
Update release-master.yml
flacatus Dec 14, 2020
aff745e
Update release-master.yml
flacatus Dec 14, 2020
7c76566
Update release-master.yml
flacatus Dec 14, 2020
ca061c4
Update release-master.yml
flacatus Dec 14, 2020
d8ddd56
Update release-master.yml
flacatus Dec 14, 2020
cffd9b6
Update release-master.yml
flacatus Dec 14, 2020
6792094
Update release-master.yml
flacatus Dec 14, 2020
1418194
Update release-master.yml
flacatus Dec 14, 2020
93fe288
Update release.yml
flacatus Dec 14, 2020
75c80a9
Merge branch 'master' of github.com:flacatus/chectl
flacatus Dec 14, 2020
a292891
Update make-release.sh
flacatus Dec 14, 2020
9e1bf69
Update release.yml
flacatus Dec 14, 2020
b8ddcb9
Update release-master.yml
flacatus Dec 14, 2020
a4b2b90
Update release-master.yml
flacatus Dec 14, 2020
1471b16
Update release-master.yml
flacatus Dec 14, 2020
d970afd
Update release-master.yml
flacatus Dec 14, 2020
db23a92
Update release-master.yml
flacatus Dec 14, 2020
6243ddf
Update release-master.yml
flacatus Dec 15, 2020
c35512f
Update release-master.yml
flacatus Dec 15, 2020
b646e7b
Update release-master.yml
flacatus Dec 15, 2020
57b0828
Update release-master.yml
flacatus Dec 15, 2020
ace6df0
Update release-master.yml
flacatus Dec 15, 2020
19fe548
Update release-master.yml
flacatus Dec 15, 2020
b80b11f
Update release-master.yml
flacatus Dec 15, 2020
762ee56
Update release.yml
flacatus Dec 15, 2020
57738c1
Update release-master.yml
flacatus Dec 15, 2020
f3e9931
FM
flacatus Dec 15, 2020
9412323
Merge pull request #12 from flacatus/release_test
flacatus Dec 15, 2020
27a49ee
Align branch with master
flacatus Jan 4, 2021
59ac042
Update readme
flacatus Jan 4, 2021
657a1c8
merge master and fix tests
flacatus Jan 4, 2021
4ebc511
fixes
flacatus Jan 4, 2021
3c9f0b0
Merge remote-tracking branch 'upstream/master'
flacatus Jan 5, 2021
97f122e
TEst changes
flacatus Jan 5, 2021
9100b5f
TEst changes
flacatus Jan 5, 2021
59b794d
TEst changes
flacatus Jan 5, 2021
dbb05af
TEst changes
flacatus Jan 5, 2021
e4e4248
TEst changes
flacatus Jan 5, 2021
b7f18ac
Merge branch 'master' into telemetry
flacatus Jan 5, 2021
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
29 changes: 29 additions & 0 deletions .ci/obfuscate/gnirts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'
flacatus marked this conversation as resolved.
Show resolved Hide resolved
/*********************************************************************
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

const gnirts = require('gnirts');
const fs = require('fs')

const rootDir = process.cwd()

const obfuscateJSFiles = () => {
const dirFilesToObfuscate = [ 'lib/hooks/analytics/analytics.js' ]
flacatus marked this conversation as resolved.
Show resolved Hide resolved

dirFilesToObfuscate.forEach((jsFilePath)=> {
if (fs.existsSync(`${rootDir}/${jsFilePath}`)) {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
let js = fs.readFileSync(`${rootDir}/${jsFilePath}`, {encoding: 'utf8'});
js = gnirts.mangle(js);
fs.writeFileSync(`${rootDir}/${jsFilePath}`, js);
}
})
}

obfuscateJSFiles()
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ before_deploy:
export GITHUB_RELEASE_NAME=$CHECTL_VERSION
fi;
sed -i sed "s#version\":\ \"\(.*\)\",#version\":\ \"$CHECTL_VERSION\",#g" package.json
sed -i '.bak' "s|INSERT-KEY-HERE|${SEGMENT_KEY}|g" src/hooks/analytics/analytics.ts
git tag $TRAVIS_TAG
TARGETS=linux-arm,linux-x64,linux-s390x,linux-ppc64le,darwin-x64,win32-x64,win32-x86
npx oclif-dev pack --targets=$TARGETS
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@types/request": "^2.48.5",
"@types/websocket": "^1.0.1",
"@types/ws": "^7.2.6",
"analytics-node": "^3.4.0-beta.3",
"ansi-colors": "4.1.1",
"axios": "^0.19.2",
"cli-ux": "^5.4.5",
Expand All @@ -36,6 +37,7 @@
"execa": "^4.0.3",
"fancy-test": "^1.4.9",
"fs-extra": "^9.0.1",
"gnirts": "^1.1.7",
"inquirer": "^7.3.3",
"js-yaml": "^3.14.0",
"listr": "^0.14.3",
Expand Down Expand Up @@ -92,6 +94,9 @@
"macos": {
"identifier": "che-incubator.chectl"
},
"hooks": {
"analytics": "./lib/hooks/analytics/analytics"
},
"plugins": [
"@oclif/plugin-autocomplete",
"@oclif/plugin-help",
Expand Down Expand Up @@ -144,7 +149,8 @@
"e2e-minikube-operator": "export PLATFORM=minikube && export INSTALLER=operator && yarn jest ./test/e2e/e2e.test.ts --testRegex='/test/(e2e)/.*.test.ts'",
"e2e-minishift": "export PLATFORM=minishift && export INSTALLER=operator && yarn jest ./test/e2e/e2e.test.ts --testRegex='/test/(e2e)/.*.test.ts'",
"e2e-openshift": "export PLATFORM=openshift && export INSTALLER=operator && yarn jest ./test/e2e/e2e.test.ts --testRegex='/test/(e2e)/.*.test.ts'",
"prepack": "rm -rf lib && rm -rf tsconfig.tsbuildinfo && tsc -b && oclif-dev manifest && oclif-dev readme",
"gnirts-ci": "node .ci/obfuscate/gnirts.js",
"prepack": "rm -rf lib && rm -rf tsconfig.tsbuildinfo && tsc -b && oclif-dev manifest && oclif-dev readme && yarn gnirts-ci",
"pack-binaries": "oclif-dev pack",
"postpack": "rm -f oclif.manifest.json",
"format": "tsfmt -r --useTsfmt tsfmt.json",
Expand Down
1 change: 1 addition & 0 deletions src/commands/auth/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class Delete extends Command {

async run() {
const { args, flags } = this.parse(Delete)
await this.config.runHook('analytics', { event: Delete.description, command: Delete.id, flags })

let cheApiEndpoint = CheApiClient.normalizeCheApiEndpointUrl(args[CHE_API_ENDPOINT_KEY])
const username: string | undefined = flags[USERNAME_KEY]
Expand Down
2 changes: 2 additions & 0 deletions src/commands/auth/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default class Get extends Command {
static description = 'Display active login session'

async run() {
await this.config.runHook('analytics', { event: Get.description, command: Get.id })
flacatus marked this conversation as resolved.
Show resolved Hide resolved

const loginManager = await CheServerLoginManager.getInstance(this.config.configDir)
const currentLogin = loginManager.getCurrentLoginInfo()
if (currentLogin.username) {
Expand Down
2 changes: 2 additions & 0 deletions src/commands/auth/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default class List extends Command {
static description = 'Show all existing login sessions'

async run() {
await this.config.runHook('analytics', { event: List.description, command: List.id })

const loginManager = await CheServerLoginManager.getInstance(this.config.configDir)
const logins = loginManager.getAllLogins()
const currentLogin = loginManager.getCurrentLoginInfo()
Expand Down
3 changes: 3 additions & 0 deletions src/commands/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export default class Login extends Command {
const { args, flags } = this.parse(Login)
const ctx = await initializeContext(flags)

// Not recommended to track user and password in telemetry
await this.config.runHook('analytics', { event: Login.description, command: Login.id })

const loginManager = await CheServerLoginManager.getInstance(this.config.configDir)

let cheApiClient: CheApiClient
Expand Down
3 changes: 2 additions & 1 deletion src/commands/auth/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default class Logout extends Command {
static description = 'Log out of the active login session'

async run() {
await this.config.runHook('analytics', { event: Logout.description, command: Logout.id })

const loginManager = await CheServerLoginManager.getInstance(this.config.configDir)
const currentLogin = loginManager.getCurrentLoginInfo()

Expand All @@ -30,5 +32,4 @@ export default class Logout extends Command {
loginManager.deleteLoginContext(cheApiEndpoint, username)
cli.info(`Succesfully logged out ${username} on ${cheApiEndpoint}`)
}

}
2 changes: 2 additions & 0 deletions src/commands/auth/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default class Use extends Command {
async run() {
const { args, flags } = this.parse(Use)

await this.config.runHook('analytics', { event: Use.description, command: Use.id, flags })

if (flags.interactive) {
await this.interactiveSwitch()
return
Expand Down
2 changes: 2 additions & 0 deletions src/commands/cacert/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default class Export extends Command {
const kube = new KubeHelper(flags)
const cheHelper = new CheHelper(flags)

await this.config.runHook('analytics', { event: Export.description, command: Export.id, flags })

if (!await kube.hasReadPermissionsForNamespace(flags.chenamespace)) {
throw new Error(`E_PERM_DENIED - Permission denied: no read access to '${flags.chenamespace}' namespace`)
}
Expand Down
2 changes: 2 additions & 0 deletions src/commands/dashboard/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default class Open extends Command {
const { flags } = this.parse(Open)

try {
await this.config.runHook('analytics', { event: Open.description, command: Open.id, flags })

const cheHelper = new CheHelper(flags)
const cheURL = await cheHelper.cheURL(flags.chenamespace)
const dashboardUrl = `${cheURL}/dashboard/`
Expand Down
2 changes: 2 additions & 0 deletions src/commands/devfile/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export default class Generate extends Command {
async run() {
const { flags } = this.parse(Generate)
kube = new KubeHelper(flags)
await this.config.runHook('analytics', { event: Generate.description, command: Generate.id, flags })

const notifier = require('node-notifier')

let name = flags.name || 'chectl-generated'
Expand Down
1 change: 1 addition & 0 deletions src/commands/server/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class Debug extends Command {
const { flags } = this.parse(Debug)
const ctx = await initializeContext(flags)

await this.config.runHook('analytics', { event: Debug.description, command: Debug.id, flags })
const cheTasks = new CheTasks(flags)
const apiTasks = new ApiTasks()
const tasks = new Listr([], { renderer: flags['listr-renderer'] as any })
Expand Down
2 changes: 2 additions & 0 deletions src/commands/server/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export default class Delete extends Command {
flags.yes = flags['skip-deletion-check']
}

await this.config.runHook('analytics', { event: Delete.description, command: Delete.id, flags })

const notifier = require('node-notifier')
const apiTasks = new ApiTasks()
const helmTasks = new HelmTasks(flags)
Expand Down
1 change: 1 addition & 0 deletions src/commands/server/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ export default class Deploy extends Command {
})

await this.setPlaformDefaults(flags, ctx)
await this.config.runHook('analytics', { event: Deploy.description, command: Deploy.id, flags })

if (flags.installer === 'olm' && flags['olm-suggested-namespace']) {
flags.chenamespace = DEFAULT_OLM_SUGGESTED_NAMESPACE
Expand Down
1 change: 1 addition & 0 deletions src/commands/server/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class Logs extends Command {
const apiTasks = new ApiTasks()
const tasks = new Listr([], { renderer: flags['listr-renderer'] as any })

await this.config.runHook('analytics', { event: Logs.description, command: Logs.id, flags })
tasks.add(apiTasks.testApiTasks(flags, this))
tasks.add(cheTasks.verifyCheNamespaceExistsTask(flags, this))
tasks.add(cheTasks.serverLogsTasks(flags, false))
Expand Down
1 change: 1 addition & 0 deletions src/commands/server/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class Status extends Command {

let openshiftOauth = 'No'

await this.config.runHook('analytics', { event: Status.description, command: Status.id, flags })
const cr = await kube.getCheCluster(flags.chenamespace)
if (ctx.isOpenShift && cr && cr.spec && cr.spec.auth && cr.spec.auth.openShiftoAuth) {
openshiftOauth = 'Yes'
Expand Down
2 changes: 2 additions & 0 deletions src/commands/server/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default class Stop extends Command {
const cheTasks = new CheTasks(flags)
const apiTasks = new ApiTasks()

await this.config.runHook('analytics', { event: Stop.description, command: Stop.id, flags })

let tasks = new Listr(undefined,
{
renderer: flags['listr-renderer'] as any,
Expand Down
1 change: 1 addition & 0 deletions src/commands/server/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default class Update extends Command {
await this.setDefaultInstaller(flags)
cli.info(`› Installer type is set to: '${flags.installer}'`)
}
await this.config.runHook('analytics', { event: Update.description, command: Update.id, flags })

const kubeHelper = new KubeHelper(flags)
const installerTasks = new InstallerTasks()
Expand Down
2 changes: 2 additions & 0 deletions src/commands/workspace/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export default class Create extends Command {
async run() {
const { flags } = this.parse(Create)

await this.config.runHook('analytics', { event: Create.description, command: Create.id, flags })

const devfilePath = this.getDevfilePath(flags.devfile)
const cheHelper = new CheHelper(flags)

Expand Down
3 changes: 2 additions & 1 deletion src/commands/workspace/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ export default class Delete extends Command {
const { flags } = this.parse(Delete)
const { args } = this.parse(Delete)

const workspaceId = args.workspace
await this.config.runHook('analytics', { event: Delete.description, command: Delete.id, flags })

const workspaceId = args.workspace
const { cheApiEndpoint, accessToken } = await getLoginData(this.config.configDir, flags[CHE_API_ENDPOINT_KEY], flags[ACCESS_TOKEN_KEY])
const cheApiClient = CheApiClient.getInstance(cheApiEndpoint)
await cheApiClient.deleteWorkspaceById(workspaceId, accessToken)
Expand Down
2 changes: 2 additions & 0 deletions src/commands/workspace/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default class Inject extends Command {
async run() {
const { flags } = this.parse(Inject)

await this.config.runHook('analytics', { event: Inject.description, command: Inject.id, flags })

const notifier = require('node-notifier')
const cheHelper = new CheHelper(flags)

Expand Down
2 changes: 2 additions & 0 deletions src/commands/workspace/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default class List extends Command {
async run() {
const { flags } = this.parse(List)

await this.config.runHook('analytics', { event: List.description, command: List.id, flags })

const { cheApiEndpoint, accessToken } = await getLoginData(this.config.configDir, flags[CHE_API_ENDPOINT_KEY], flags[ACCESS_TOKEN_KEY])
const cheApiClient = CheApiClient.getInstance(cheApiEndpoint)
const workspaces = await cheApiClient.getAllWorkspaces(accessToken)
Expand Down
1 change: 1 addition & 0 deletions src/commands/workspace/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class Logs extends Command {
const { flags } = this.parse(Logs)
const logsDirectory = path.resolve(flags.directory ? flags.directory : path.resolve(os.tmpdir(), 'chectl-logs', Date.now().toString()))

await this.config.runHook('analytics', { event: Logs.description, command: Logs.id, flags })
const cheHelper = new CheHelper(flags)
const workspaceRun = await cheHelper.readWorkspacePodLog(flags.namespace, flags.workspace, logsDirectory)

Expand Down
1 change: 1 addition & 0 deletions src/commands/workspace/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class Start extends Command {
const { flags } = this.parse(Start)
const { args } = this.parse(Start)

await this.config.runHook('analytics', { event: Start.description, command: Start.id, flags })
const workspaceId = args.workspace
const cheHelper = new CheHelper(flags)

Expand Down
3 changes: 2 additions & 1 deletion src/commands/workspace/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export default class Stop extends Command {
const { flags } = this.parse(Stop)
const { args } = this.parse(Stop)

const workspaceId = args.workspace
await this.config.runHook('analytics', { event: Stop.description, command: Stop.id, flags })

const workspaceId = args.workspace
const { cheApiEndpoint, accessToken } = await getLoginData(this.config.configDir, flags[CHE_API_ENDPOINT_KEY], flags[ACCESS_TOKEN_KEY])
const cheApiClient = CheApiClient.getInstance(cheApiEndpoint)
await cheApiClient.stopWorkspace(workspaceId, accessToken)
Expand Down
44 changes: 44 additions & 0 deletions src/hooks/analytics/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*********************************************************************
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import { cli } from 'cli-ux'
import { IConfig } from '@oclif/config'
import { existsSync, mkdirsSync } from 'fs-extra'

import { SegmentAdapter } from './segment'

export const hook = async (options: {event: string, flags: any, command: string, config: IConfig }) => {
flacatus marked this conversation as resolved.
Show resolved Hide resolved
let confirmed

const segment = new SegmentAdapter({
// tslint:disable-next-line:no-single-line-block-comment
segmentWriteKey: /* @mangle */'INSERT-KEY-HERE' /* @/mangle */,
})

// Check if exist config dir and if not procceed to create it
if (!existsSync(options.config.configDir)) {
mkdirsSync(options.config.configDir)
}

// In case if segment info doesn't exist ask user if allow chectl to collect data and store the confirmation in cache
if (!segment.checkIfSegmentConfigFileExist(options.config.configDir)) {
confirmed = await cli.confirm('Do you allow chectl to collect anonymous usage data? Please confirm - press y/n')
segment.storeSegmentConfig(options.config.configDir, confirmed)
}

try {
// If user allow to collect data, chectl start to send the data to segment
if (confirmed || segment.checkIfSegmentCollectIsAllowed(options.config.configDir)) {
segment.onTrack(options)
}
} catch {
return this
}
}
Loading