-
Notifications
You must be signed in to change notification settings - Fork 4
/
command.ts
113 lines (91 loc) · 2.53 KB
/
command.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
import { InternalCredentialManager } from "../aws/common/credentials.js"
import { IamRoleArn } from "../aws/common/model.js"
import { InternalCommandContext } from "../context/command-context.js"
import { TkmLogger } from "../utils/logging.js"
import { Timer } from "../utils/timer.js"
export type Project = string
export interface OperationState {
failed: boolean
}
export interface CommandRole {
readonly iamRoleArn: IamRoleArn
}
export type OutputFormat = "text" | "json" | "yaml"
export type CommandStatus = "SUCCESS" | "FAILED" | "CANCELLED" | "SKIPPED"
export const SUCCESS: CommandStatus = "SUCCESS"
export const FAILED: CommandStatus = "FAILED"
export const CANCELLED: CommandStatus = "CANCELLED"
export const SKIPPED: CommandStatus = "SKIPPED"
export interface SuccessHolder {
readonly success: boolean
}
export interface CommandOutputBase extends SuccessHolder {
readonly message: string
readonly status: CommandStatus
readonly error?: Error
}
export interface CommandOutput extends CommandOutputBase {
readonly timer: Timer
readonly outputFormat: OutputFormat
}
export interface ResultsOutput<T> extends CommandOutput {
readonly results: ReadonlyArray<T>
}
export interface CommandInput {
readonly timer: Timer
readonly outputFormat: OutputFormat
}
export interface IO<O extends CommandOutput> extends TkmLogger {
readonly printOutput: (output: O) => O
}
export const resolveCommandOutputBase = (
items: ReadonlyArray<CommandOutputBase>,
): CommandOutputBase => {
if (items.length === 0) {
return {
status: "SUCCESS",
message: "Success",
success: true,
}
}
const success = items.every((i) => i.success)
if (items.every((i) => i.status === "CANCELLED")) {
return {
status: "CANCELLED",
message: "Cancelled",
success,
}
}
if (items.every((i) => i.status === "SKIPPED")) {
return {
status: "SKIPPED",
message: "Skipped",
success,
}
}
const status = success ? "SUCCESS" : "FAILED"
const message = success ? "Success" : "Failed"
return {
status,
message,
success,
}
}
export interface CommandHandlerArgs<
C,
I extends IO<OUT>,
IN extends CommandInput,
OUT extends CommandOutput,
> {
readonly ctx: InternalCommandContext
readonly configRepository: C
readonly io: I
readonly input: IN
readonly credentialManager: InternalCredentialManager
}
export type CommandHandler<
C,
I extends IO<OUT>,
IN extends CommandInput,
OUT extends CommandOutput,
> = (args: CommandHandlerArgs<C, I, IN, OUT>) => Promise<OUT>