-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
125 lines (120 loc) · 3.36 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
// Copyright © 2020-2023 Truestamp Inc. All rights reserved.
import {
colors,
Command,
CompletionsCommand,
EnumType,
fromZodError,
HelpCommand,
ValidationError,
ZodError,
} from "./deps.ts";
import { auth } from "./commands/auth.ts";
import { commitments } from "./commands/commitments.ts";
import { entropyCommand } from "./commands/entropy.ts";
import { items } from "./commands/items.ts";
export const environmentType = new EnumType([
"development",
"staging",
"production",
]);
export const outputType = new EnumType(["silent", "text", "json"]);
// Top level command
const cmd = new Command()
.throwErrors()
.name("truestamp")
.version("1.1.0") // RELEASE VERSION : BUMP VERSION HERE
.description("Truestamp CLI")
.meta("deno", Deno.version.deno)
.meta("v8", Deno.version.v8)
.meta("typescript", Deno.version.typescript)
.help({
types: false,
hints: true,
})
.globalType("environment", environmentType)
.globalEnv("TRUESTAMP_ENV=<env:environment>", "Override API endpoint.", {
required: false,
prefix: "TRUESTAMP_", // prefix will be ignored when converting to option name. e.g. TRUESTAMP_ENV becomes 'env'
})
.globalOption(
"-E, --env <env:environment>",
"Override API endpoint. Overrides 'TRUESTAMP_ENV' env var.",
{
required: false,
default: "production",
},
)
.globalEnv(
"TRUESTAMP_API_KEY=<apiKey:string>",
"Force use of API key for authentication.",
{
required: false,
prefix: "TRUESTAMP_",
},
)
.globalOption(
"-A, --api-key <apiKey:string>",
"Use API key for authentication. Overrides 'TRUESTAMP_API_KEY' env var.",
)
.globalType("output", outputType)
.globalEnv("TRUESTAMP_OUTPUT=<output:output>", "Preferred output format.", {
required: false,
prefix: "TRUESTAMP_",
})
.globalOption(
"-o, --output <output:output>",
"Output format. Overrides 'TRUESTAMP_OUTPUT' env var.",
{
default: "text",
},
)
.globalEnv(
"TRUESTAMP_SIGNING_SECRET_KEY=<signingSecretKey:string>",
"A Base64 encoded ed25519 secret key.",
{
required: false,
prefix: "TRUESTAMP_",
},
)
.globalOption(
"-S, --signing-secret-key <signingSecretKey:string>",
"A Base64 encoded ed25519 secret key. Overrides 'TRUESTAMP_SIGNING_SECRET_KEY' env var.",
)
.action(() => {
cmd.showHelp();
})
.command("auth", auth)
.command("commitments", commitments)
.command("items", items)
.command("entropy", entropyCommand)
.command("completions", new CompletionsCommand())
.command("help", new HelpCommand().global());
try {
await cmd.parse(Deno.args);
} catch (error) {
if (error instanceof ValidationError) {
cmd.showHelp();
Deno.stderr.writeSync(
new TextEncoder().encode(
colors.yellow(
` ${colors.bold("Validation Error")}: ${error.message}\n`,
) + "\n",
),
);
} else if (error instanceof ZodError) {
const validationError = fromZodError(error);
Deno.stderr.writeSync(
new TextEncoder().encode(
colors.red(` ${colors.bold("Error")}: ${validationError}\n`) + "\n",
),
);
} else if (error instanceof Error) {
Deno.stderr.writeSync(
new TextEncoder().encode(
colors.red(` ${colors.bold("Error")}: ${error.message}\n`) + "\n",
),
);
}
Deno.exit(error instanceof ValidationError ? error.exitCode : 1);
}