forked from 1Password/op-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
292 lines (247 loc) · 6.97 KB
/
cli.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { spawnSync } from "child_process";
import { lookpath } from "lookpath";
import semverCoerce from "semver/functions/coerce";
import semverSatisfies from "semver/functions/satisfies";
import { version } from "../package.json";
import {
FieldAssignment,
FieldLabelSelector,
FieldTypeSelector,
GlobalFlags,
} from "./index";
export type FlagValue =
| string
| string[]
| boolean
| FieldLabelSelector
| FieldTypeSelector;
export type Flags = Record<string, FlagValue>;
type Arg = string | FieldAssignment;
export interface ClientInfo {
name: string;
id: string;
build: string;
}
export type ValidationErrorType = "not-found" | "version";
export class ValidationError extends Error {
public constructor(
public type: ValidationErrorType,
public requiredVersion?: string,
public currentVersion?: string,
) {
let message: string;
switch (type) {
case "not-found":
message = "Could not find `op` executable";
break;
case "version":
message = `CLI version ${currentVersion} does not satisfy required version ${requiredVersion}`;
break;
}
super(message);
this.name = "ValidationError";
}
}
export class ExecutionError extends Error {
public constructor(message: string, public status: number) {
super(message);
this.name = "ExecutionError";
}
}
export class CLIError extends ExecutionError {
static errorRegex = /\[ERROR] (\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}) (.+)/;
public timestamp?: Date;
public constructor(public originalMessage: string, status: number) {
const errorMatch = originalMessage.match(CLIError.errorRegex);
let parsedMessage: string;
let parsedTimestamp: Date;
if (errorMatch) {
parsedMessage = errorMatch[2];
parsedTimestamp = new Date(errorMatch[1]);
} else {
parsedMessage = "Unknown error";
}
super(parsedMessage, status);
this.name = "CLIError";
this.timestamp = parsedTimestamp;
}
}
export const semverToInt = (input: string) =>
input
.split(".")
.map((n) => n.padStart(2, "0"))
.join("");
export const camelToHyphen = (str: string) =>
str.replace(/([A-Za-z])(?=[A-Z])/g, "$1-").toLowerCase();
export const sanitizeInput = (str: string) =>
str.replace(/(["$'\\`])/g, "\\$1");
const equalArray = (a: any[], b: any[]) =>
a.length === b.length && a.every((val, index) => val === b[index]);
export const parseFlagValue = (value: FlagValue) => {
if (typeof value === "string") {
return `=${sanitizeInput(value)}`;
}
if (Array.isArray(value)) {
return `=${value.join(",")}`;
}
if (typeof value === "object") {
let fields = "";
if ("label" in value) {
fields += (value.label || []).map((label) => `label=${label}`).join(",");
}
if ("type" in value) {
fields += (value.type || []).map((type) => `type=${type}`).join(",");
}
if (fields.length > 0) {
return `=${sanitizeInput(fields)}`;
}
}
// If we get here, it's a boolean and boolean CLI flags don't have a value
return "";
};
export const createFlags = (flags: Flags): string[] =>
Object.entries(flags)
.filter(([_, value]) => Boolean(value))
.map(
([flag, value]) =>
`--${camelToHyphen(sanitizeInput(flag))}${parseFlagValue(value)}`,
);
export const createFieldAssignment = ([
label,
type,
value,
]: FieldAssignment): string =>
`${sanitizeInput(label)}[${sanitizeInput(type)}]=${sanitizeInput(value)}`;
export const defaultClientInfo: ClientInfo = {
name: "1Password for JavaScript",
id: "JS",
build: semverToInt(version),
};
export class CLI {
public static recommendedVersion = ">=2.4.0";
public clientInfo: ClientInfo = defaultClientInfo;
public globalFlags: Partial<GlobalFlags> = {};
public connect?: { host: string; token: string };
public setClientInfo(clientInfo: ClientInfo) {
this.clientInfo = clientInfo;
}
public getVersion(): string {
return this.execute<string>([], { flags: { version: true }, json: false });
}
public async validate(requiredVersion: string = CLI.recommendedVersion) {
const cliExists = !!(await lookpath("op"));
if (!cliExists) {
throw new ValidationError("not-found");
}
const version = this.getVersion();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const semVersion = semverCoerce(version);
if (!semverSatisfies(semVersion, requiredVersion)) {
throw new ValidationError("version", requiredVersion, version);
}
}
private createParts(
subCommand: string[],
args: Arg[],
flags: Flags,
json: boolean,
): string[] {
const parts = subCommand.map((part) => sanitizeInput(part));
for (const arg of args) {
if (typeof arg === "string") {
parts.push(sanitizeInput(arg));
// If it's an array assume it's a field assignment
} else if (Array.isArray(arg)) {
parts.push(createFieldAssignment(arg));
} else {
throw new TypeError("Invalid argument");
}
}
if (json) {
flags = { ...flags, format: "json" };
}
// Version >=2.6.2 of the CLI changed how it handled piped input
// in order to fix an issue with item creation, but in the process
// it broke piping for other commands. We have a macOS/Linux-only
// workaround, but not one for Windows, so for now we cannot support
// the inject command on Windows past this version until the CLI
// team fixes the issue.
if (equalArray(subCommand, ["inject"])) {
const version = semverCoerce(cli.getVersion());
if (semverSatisfies(version, ">=2.6.2")) {
if (process.platform === "win32") {
throw new ExecutionError(
"Inject is not supported on Windows for version >=2.6.2 of the CLI",
1,
);
} else {
flags = { ...flags, inFile: "/dev/stdin" };
}
}
}
return [
...parts,
...createFlags({
...this.globalFlags,
...flags,
}),
];
}
public execute<TData extends string | Record<string, any> | void>(
subCommand: string[],
{
args = [],
flags = {},
stdin,
json = true,
}: {
args?: Arg[];
flags?: Flags;
stdin?: string | Record<string, any>;
json?: boolean;
} = {},
): TData {
let input: NodeJS.ArrayBufferView;
const parts = this.createParts(subCommand, args, flags, json);
if (stdin) {
input = Buffer.from(
typeof stdin === "string" ? stdin : JSON.stringify(stdin),
);
}
const { status, error, stdout, stderr } = spawnSync("op", parts, {
stdio: "pipe",
input,
env: {
...process.env,
...(this.connect && {
OP_CONNECT_HOST: this.connect.host,
OP_CONNECT_TOKEN: this.connect.token,
}),
OP_INTEGRATION_NAME: this.clientInfo.name,
OP_INTEGRATION_ID: this.clientInfo.id,
OP_INTEGRATION_BUILDNUMBER: this.clientInfo.build,
},
});
if (error) {
throw new ExecutionError(error.message, status);
}
const cliError = stderr.toString();
if (cliError.length > 0) {
throw new CLIError(cliError, status);
}
const output = stdout.toString().trim();
if (output.length === 0) {
return;
}
if (!json) {
return output as TData;
}
try {
return JSON.parse(output) as TData;
} catch (error) {
console.log(output);
throw error;
}
}
}
export const cli = new CLI();