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 #1052

Merged
merged 1 commit into from
Jan 5, 2021
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
31 changes: 31 additions & 0 deletions .ci/obfuscate/gnirts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*********************************************************************
* 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
**********************************************************************/
'use strict'

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

const rootDir = process.cwd()

const obfuscateJSFiles = () => {
const jsFilesToObfuscate = [ 'lib/hooks/analytics/analytics.js' ]

jsFilesToObfuscate.forEach((jsFilePath)=> {
const fileToObfuscate = path.join(rootDir, jsFilePath)
if (fs.existsSync(fileToObfuscate)) {
let js = fs.readFileSync(fileToObfuscate, {encoding: 'utf8'});
js = gnirts.mangle(js);
fs.writeFileSync(fileToObfuscate, js);
}
})
}

obfuscateJSFiles()
2 changes: 2 additions & 0 deletions .github/workflows/release-build-and-push-to-GH-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ jobs:
git config --local user.name ${{ github.actor }}

sed -i "s#version\":\ \"\(.*\)\",#version\":\ \"${{ steps.TAG_UTIL.outputs.chectl_version }}\",#g" package.json
sed -i "s|INSERT-KEY-HERE|${{ secrets.SEGMENT_WRITE_KEY }}|g" src/hooks/analytics/analytics.ts

git tag ${{ steps.TAG_UTIL.outputs.gh_tag }}

TARGETS=linux-arm,linux-x64,linux-s390x,linux-ppc64le,darwin-x64,win32-x64,win32-x86
Expand Down
58 changes: 55 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ ARGUMENTS
OPTIONS
-h, --help show CLI help
-u, --username=username Eclipse Che username
--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry

EXAMPLES
# Delete login session of the specified user on the cluster:
Expand All @@ -144,7 +145,8 @@ USAGE
$ chectl auth:get

OPTIONS
-h, --help show CLI help
-h, --help show CLI help
--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/auth/get.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/auth/get.ts)_
Expand All @@ -158,7 +160,8 @@ USAGE
$ chectl auth:list

OPTIONS
-h, --help show CLI help
-h, --help show CLI help
--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/auth/list.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/auth/list.ts)_
Expand Down Expand Up @@ -186,6 +189,8 @@ OPTIONS

-u, --username=username Eclipse Che username

--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry

EXAMPLES
# Log in with username and password (when OpenShift OAuth is not enabled):
chectl auth:login https://che-che.apps-crc.testing/api -u username -p password
Expand Down Expand Up @@ -214,7 +219,8 @@ USAGE
$ chectl auth:logout

OPTIONS
-h, --help show CLI help
-h, --help show CLI help
--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/auth/logout.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/auth/logout.ts)_
Expand All @@ -234,6 +240,7 @@ OPTIONS
-h, --help show CLI help
-i, --interactive Select an active login session in interactive mode
-u, --username=username Eclipse Che username
--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry

EXAMPLES
# Set an active login session for the specified user on the given cluster:
Expand Down Expand Up @@ -303,6 +310,9 @@ OPTIONS

--skip-kubernetes-health-check
Skip Kubernetes health check

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/cacert/export.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/cacert/export.ts)_
Expand All @@ -320,6 +330,8 @@ OPTIONS

-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where Eclipse Che server is supposed to be
deployed

--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/dashboard/open.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/dashboard/open.ts)_
Expand Down Expand Up @@ -352,6 +364,8 @@ OPTIONS

--selector=selector label selector to filter the Kubernetes resources. For example
--selector="app.kubernetes.io/name=employee-manager"

--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/devfile/generate.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/devfile/generate.ts)_
Expand Down Expand Up @@ -390,6 +404,8 @@ OPTIONS
--debug-port=debug-port [default: 8000] Eclipse Che server debug port

--skip-kubernetes-health-check Skip Kubernetes health check

--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/server/debug.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/debug.ts)_
Expand Down Expand Up @@ -423,6 +439,9 @@ OPTIONS
engine is the DevWorkspace

--skip-kubernetes-health-check Skip Kubernetes health check

--telemetry=on|off Enable or disable telemetry. This flag skips
a prompt and enable/disable telemetry
```

_See code: [src/commands/server/delete.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/delete.ts)_
Expand Down Expand Up @@ -578,6 +597,9 @@ OPTIONS
the latest known version.
This parameter is used only when the installer is 'olm'.

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry

--workspace-engine=che-server|dev-workspace
[default: che-server] Workspace Engine. If not set, default is "che-server". "dev-workspace" is experimental.

Expand Down Expand Up @@ -605,6 +627,8 @@ OPTIONS
--deployment-name=deployment-name [default: che] Eclipse Che deployment name

--skip-kubernetes-health-check Skip Kubernetes health check

--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/server/logs.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/logs.ts)_
Expand Down Expand Up @@ -656,6 +680,8 @@ OPTIONS

-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where Eclipse Che server is supposed to be
deployed

--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/server/status.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/status.ts)_
Expand Down Expand Up @@ -694,6 +720,9 @@ OPTIONS

--skip-kubernetes-health-check
Skip Kubernetes health check

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/server/stop.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/stop.ts)_
Expand Down Expand Up @@ -729,6 +758,9 @@ OPTIONS

--skip-kubernetes-health-check Skip Kubernetes health check

--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and
enable/disable telemetry

EXAMPLES
# Update Eclipse Che:
chectl server:update
Expand Down Expand Up @@ -793,6 +825,9 @@ OPTIONS

--skip-kubernetes-health-check
Skip Kubernetes health check

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/workspace/create.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/workspace/create.ts)_
Expand Down Expand Up @@ -830,6 +865,9 @@ OPTIONS

--skip-kubernetes-health-check
Skip Kubernetes health check

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/workspace/delete.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/workspace/delete.ts)_
Expand Down Expand Up @@ -874,6 +912,9 @@ OPTIONS

--skip-kubernetes-health-check
Skip Kubernetes health check

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/workspace/inject.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/workspace/inject.ts)_
Expand Down Expand Up @@ -905,6 +946,9 @@ OPTIONS

--skip-kubernetes-health-check
Skip Kubernetes health check

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/workspace/list.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/workspace/list.ts)_
Expand All @@ -927,6 +971,8 @@ OPTIONS
-w, --workspace=workspace (required) Target workspace id. Can be found in workspace configuration 'id' field.

--skip-kubernetes-health-check Skip Kubernetes health check

--telemetry=on|off Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/workspace/logs.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/workspace/logs.ts)_
Expand Down Expand Up @@ -964,6 +1010,9 @@ OPTIONS

--skip-kubernetes-health-check
Skip Kubernetes health check

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/workspace/start.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/workspace/start.ts)_
Expand Down Expand Up @@ -998,6 +1047,9 @@ OPTIONS

--skip-kubernetes-health-check
Skip Kubernetes health check

--telemetry=on|off
Enable or disable telemetry. This flag skips a prompt and enable/disable telemetry
```

_See code: [src/commands/workspace/stop.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/workspace/stop.ts)_
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
64 changes: 64 additions & 0 deletions src/api/config-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*********************************************************************
* 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 * as fs from 'fs-extra'
import { merge } from 'lodash'
import * as path from 'path'

import { ChectlContext } from './context'

/**
* ChectlConfig contains necessary methods to interact with cache configDir of chectl.
*/
export class ConfigManager {
private static configManager: ConfigManager
private static readonly CHECTL_CONFIG_FILE_NAME = 'config.json'

private data: any
private readonly configPath: string

private constructor(configDir: string) {
if (!fs.existsSync(configDir)) {
fs.mkdirsSync(configDir)
}

this.configPath = path.join(configDir, ConfigManager.CHECTL_CONFIG_FILE_NAME)
this.data = this.readData()
}

static getInstance(): ConfigManager {
if (this.configManager) {
return this.configManager
}

const ctx = ChectlContext.get()
const configDir = ctx[ChectlContext.CONFIG_DIR]

this.configManager = new ConfigManager(configDir)
return this.configManager
}

public setProperty(name: string, value: any): void {
this.data = merge(this.data, { [name]: value })
fs.writeFileSync(this.configPath, JSON.stringify(this.data))
}

public getProperty(name: string): any {
return this.data[name]
}

private readData(): any {
if (!fs.existsSync(this.configPath)) {
return {}
}

return JSON.parse(fs.readFileSync(this.configPath, 'utf8'))
}
}
5 changes: 4 additions & 1 deletion src/commands/auth/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { cli } from 'cli-ux'
import { CheApiClient } from '../../api/che-api-client'
import { CheServerLoginManager } from '../../api/che-login-manager'
import { ChectlContext } from '../../api/context'
import { CHE_API_ENDPOINT_KEY, username, USERNAME_KEY } from '../../common-flags'
import { CHE_API_ENDPOINT_KEY, CHE_TELEMETRY, username, USERNAME_KEY } from '../../common-flags'
import { DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'

export default class Delete extends Command {
static description = 'Delete specified login session(s)'
Expand All @@ -29,6 +30,7 @@ export default class Delete extends Command {
static flags: flags.Input<any> = {
help: flags.help({ char: 'h' }),
[USERNAME_KEY]: username,
telemetry: CHE_TELEMETRY
}

static examples = [
Expand All @@ -42,6 +44,7 @@ export default class Delete extends Command {
const { args, flags } = this.parse(Delete)
await ChectlContext.init(flags, this)

await this.config.runHook(DEFAULT_ANALYTIC_HOOK_NAME, { command: Delete.id, flags })
let cheApiEndpoint = CheApiClient.normalizeCheApiEndpointUrl(args[CHE_API_ENDPOINT_KEY])
const username: string | undefined = flags[USERNAME_KEY]

Expand Down
Loading