-
Notifications
You must be signed in to change notification settings - Fork 0
/
details.ts
151 lines (104 loc) · 3.89 KB
/
details.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Command, { Flags, Args } from '../../base'
import Table from 'cli-table3'
import { clOutput, clColor, clText } from '@commercelayer/cli-core'
import type { CommandError } from '@oclif/core/lib/interfaces'
export default class CleanupsDetails extends Command {
static description = 'show the details of an existing cleanup'
static aliases = ['clp:details']
static examples = [
'$ commercelayer cleanups:details <cleanup-id>',
'$ cl clp:details <cleanup-id>',
]
static flags = {
logs: Flags.boolean({
char: 'l',
description: 'show error logs related to the cleanup process',
}),
}
static args = {
id: Args.string({ name: 'id', description: 'unique id of the cleanup', required: true, hidden: false }),
}
async run(): Promise<any> {
const { args, flags } = await this.parse(CleanupsDetails)
const id = args.id
const cl = this.commercelayerInit(flags)
try {
const clp = await cl.cleanups.retrieve(id)
const table = new Table({
// head: ['ID', 'Topic', 'Circuit state', 'Failures'],
colWidths: [23, 67],
colAligns: ['right', 'left'],
wordWrap: true,
wrapOnWordBoundary: true
})
const exclude = new Set(['type', 'reference', 'reference_origin', 'metadata', 'errors_log'])
// let index = 0
table.push(...Object.entries(clp)
.filter(([k]) => !exclude.has(k))
.map(([k, v]) => {
return [
{ content: clColor.table.key.blueBright(k), hAlign: 'right', vAlign: 'center' },
this.formatValue(k, v),
]
}))
this.log()
this.log(table.toString())
this.log()
if (flags.logs) this.showLogs(clp.errors_log)
return clp
} catch (error) {
this.handleError(error as CommandError, flags, id)
}
}
private formatValue(field: string, value: any): any {
if (field.endsWith('_date') || field.endsWith('_at')) return clOutput.localeDate(value as string)
switch (field) {
case 'id': return clColor.api.id(value)
case 'resource_type': return clColor.magentaBright(value)
case 'topic': return clColor.magenta(value)
case 'status': return this.cleanupStatus(value as string)
case 'records_count': return clColor.yellowBright(value)
case 'errors_count': return clColor.msg.error(value)
case 'dry_data': return (value ? clText.symbols.check.small : '')
case 'includes': return (value as string[]).join(', ')
case 'filters':
case 'metadata': {
const t = new Table({ style: { compact: false } })
t.push(...Object.entries(value as object).map(([k, v]) => {
return [
{ content: clColor.cyan.italic(k), hAlign: 'left', vAlign: 'center' },
{ content: clColor.cli.value((typeof v === 'object') ? JSON.stringify(v) : v) } as any,
]
}))
return t.toString()
}
default: {
if ((typeof value === 'object') && (value !== null)) return JSON.stringify(value, undefined, 4)
return String(value)
}
}
}
private showLogs(errorLog?: Record<string, any> | null): void {
const tableOptions: Table.TableConstructorOptions = {
head: ['Code', 'Message'],
colWidths: [15, 75],
// colAligns: ['center', 'left'],
wordWrap: true,
style: {
head: ['brightCyan'],
compact: false,
},
}
// Errors
const errors = errorLog ? Object.keys(errorLog).length : 0
this.log()
this.log(clColor.msg.error(`${clColor.bold('ERROR LOG')}\t[ ${errors} error${errors === 1 ? '' : 's'} ]`))
if (errors > 0) {
const table = new Table(tableOptions)
table.push(...(Object.entries(errorLog || {}))
.map(([k, v]) => [{ content: ((k && (k !== 'unknown')) ? k : ''), vAlign: 'center' }, clOutput.printObject(v) as any]))
this.log(table.toString())
}
this.log()
}
}