-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathBaseCommand.ts
More file actions
71 lines (58 loc) · 2.1 KB
/
BaseCommand.ts
File metadata and controls
71 lines (58 loc) · 2.1 KB
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
import 'dotenv/config'
import process from 'node:process'
import { Command, Option } from 'clipanion'
import { Transloadit as TransloaditClient } from '../../Transloadit.ts'
import { getEnvCredentials } from '../helpers.ts'
import type { IOutputCtl } from '../OutputCtl.ts'
import OutputCtl, { LOG_LEVEL_DEFAULT, LOG_LEVEL_NAMES, parseLogLevel } from '../OutputCtl.ts'
abstract class BaseCommand extends Command {
logLevelOption = Option.String('-l,--log-level', {
description: `Log level: ${LOG_LEVEL_NAMES.join(', ')} or 3-8 (default: notice)`,
})
json = Option.Boolean('-j,--json', false, {
description: 'Output in JSON format',
})
endpoint = Option.String('--endpoint', {
description:
'API endpoint URL (default: https://api2.transloadit.com, or TRANSLOADIT_ENDPOINT env var)',
})
protected output!: IOutputCtl
protected client!: TransloaditClient
protected setupOutput(): void {
const logLevel = this.logLevelOption ? parseLogLevel(this.logLevelOption) : LOG_LEVEL_DEFAULT
this.output = new OutputCtl({
logLevel,
jsonMode: this.json,
})
}
protected setupClient(): boolean {
const creds = getEnvCredentials()
if (!creds) {
this.output.error(
'Please provide API authentication in the environment variables TRANSLOADIT_KEY and TRANSLOADIT_SECRET',
)
return false
}
const endpoint = this.endpoint || process.env.TRANSLOADIT_ENDPOINT
this.client = new TransloaditClient({ ...creds, ...(endpoint && { endpoint }) })
return true
}
abstract override execute(): Promise<number | undefined>
}
export abstract class AuthenticatedCommand extends BaseCommand {
override async execute(): Promise<number | undefined> {
this.setupOutput()
if (!this.setupClient()) {
return 1
}
return await this.run()
}
protected abstract run(): Promise<number | undefined>
}
export abstract class UnauthenticatedCommand extends BaseCommand {
override async execute(): Promise<number | undefined> {
this.setupOutput()
return await this.run()
}
protected abstract run(): Promise<number | undefined>
}