forked from che-incubator/chectl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.ts
67 lines (56 loc) · 2.47 KB
/
list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*********************************************************************
* Copyright (c) 2019 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 { Command, flags } from '@oclif/command'
import { cli } from 'cli-ux'
import { CheHelper } from '../../api/che'
import { CheApiClient } from '../../api/che-api-client'
import { KubeHelper } from '../../api/kube'
import { accessToken, ACCESS_TOKEN_KEY, cheApiEndpoint, cheNamespace, CHE_API_ENDPOINT_KEY as CHE_API_ENDPOINT_KEY, skipKubeHealthzCheck } from '../../common-flags'
export default class List extends Command {
static description = 'list workspaces'
static flags: flags.Input<any> = {
help: flags.help({ char: 'h' }),
chenamespace: cheNamespace,
[CHE_API_ENDPOINT_KEY]: cheApiEndpoint,
[ACCESS_TOKEN_KEY]: accessToken,
'skip-kubernetes-health-check': skipKubeHealthzCheck
}
async run() {
const { flags } = this.parse(List)
let workspaces = []
let cheApiEndpoint = flags[CHE_API_ENDPOINT_KEY]
if (!cheApiEndpoint) {
const kube = new KubeHelper(flags)
if (!await kube.hasReadPermissionsForNamespace(flags.chenamespace)) {
throw new Error(`Eclipse Che API endpoint is required. Use flag --${CHE_API_ENDPOINT_KEY} to provide it.`)
}
const cheHelper = new CheHelper(flags)
cheApiEndpoint = await cheHelper.cheURL(flags.chenamespace) + '/api'
}
const cheApiClient = CheApiClient.getInstance(cheApiEndpoint)
await cheApiClient.checkCheApiEndpointUrl()
workspaces = await cheApiClient.getAllWorkspaces(flags[ACCESS_TOKEN_KEY])
this.printWorkspaces(workspaces)
}
private printWorkspaces(workspaces: any[]): void {
const data: any[] = []
workspaces.forEach((workspace: any) => {
data.push({
id: workspace.id,
name: workspace.devfile.metadata.name,
namespace: workspace.attributes.infrastructureNamespace,
status: workspace.status,
created: new Date(parseInt(workspace.attributes.created, 10)).toISOString(),
updated: workspace.attributes.updated ? new Date(parseInt(workspace.attributes.updated, 10)).toISOString() : ''
})
})
cli.table(data, { id: {}, name: {}, namespace: {}, status: {}, created: {}, updated: {} })
}
}