/
config.ts
108 lines (95 loc) · 2.94 KB
/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import yaml from 'js-yaml'
import { Flags } from '@oclif/core'
import {
buildOutputFormatter,
calculateOutputFormat,
IOFormat,
outputItem,
outputList,
outputItemOrList,
SmartThingsCommand,
stringTranslateToId,
TableFieldDefinition,
writeOutput,
OutputListConfig,
} from '@smartthings/cli-lib'
function reservedKey(key: string): boolean {
return key === 'logging'
}
export type ConfigData = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[name: string]: any
}
export class ConfigItem {
public name: string
public active: string
token: string
apiUrl?: string
data: ConfigData
constructor(key: string, data: ConfigData, profileName: string) {
this.name = key
this.active = reservedKey(key) ? 'N/A' : key === profileName ? 'true' : ''
this.token = data?.token ?? ''
this.apiUrl = data?.clientIdProvider?.baseURL ?? ''
this.data = data
}
}
export default class ConfigCommand extends SmartThingsCommand<typeof ConfigCommand.flags> {
static description = 'list config file entries'
static flags = {
...SmartThingsCommand.flags,
...outputItemOrList.flags,
verbose: Flags.boolean({
description: 'Include additional data in table output',
char: 'v',
}),
}
static args = [{
name: 'name',
description: 'the config name',
}]
async run(): Promise<void> {
const listTableFieldDefinitions: TableFieldDefinition<ConfigItem>[] = [
'name',
{ label: 'Active', value: (item) => reservedKey(item.name) ? 'N/A' : item.active ? 'true' : '' },
]
const tableFieldDefinitions: TableFieldDefinition<ConfigItem>[] = [
...listTableFieldDefinitions,
{ label: 'Definition', value: (item) => yaml.dump(item.data) },
]
const outputListConfig: OutputListConfig<ConfigItem> = {
primaryKeyName: 'name',
sortKeyName: 'name',
listTableFieldDefinitions,
}
if (this.flags.verbose) {
outputListConfig.listTableFieldDefinitions.push('token')
}
const getConfig = async (name: string): Promise<ConfigItem> => {
const config = this.cliConfig.mergedProfiles
return new ConfigItem(name, config[name], this.profileName)
}
const listConfigs = async (): Promise<ConfigItem[]> => {
const config = this.cliConfig.mergedProfiles
const list = Object.keys(config).map(it => {
return new ConfigItem(it, config[it], this.profileName)
})
if (this.flags.verbose && !!list.find(it => it.data?.clientIdProvider?.baseURL)) {
listTableFieldDefinitions.push('apiUrl')
}
return list
}
if (this.args.name) {
const id = await stringTranslateToId(outputListConfig, this.args.name, listConfigs)
await outputItem(this, { tableFieldDefinitions }, () => getConfig(id))
} else {
const outputFormat = calculateOutputFormat(this)
if (outputFormat === IOFormat.COMMON) {
await outputList(this, outputListConfig, listConfigs, true)
} else {
const outputFormatter = buildOutputFormatter(this)
await writeOutput(outputFormatter(this.cliConfig.mergedProfiles), this.flags.output)
}
}
}
}