@sixb/cli 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -18
- package/dist/commands/api.d.ts.map +1 -1
- package/dist/commands/app.d.ts.map +1 -1
- package/dist/commands/dev-runner.d.ts +14 -0
- package/dist/commands/dev-runner.d.ts.map +1 -0
- package/dist/commands/dev.d.ts +2 -12
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/init.d.ts +0 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/login.d.ts +8 -0
- package/dist/commands/login.d.ts.map +1 -0
- package/dist/commands/logout.d.ts +6 -0
- package/dist/commands/logout.d.ts.map +1 -0
- package/dist/commands/profile.d.ts +7 -0
- package/dist/commands/profile.d.ts.map +1 -0
- package/dist/commands/service-account.d.ts +1 -0
- package/dist/commands/service-account.d.ts.map +1 -1
- package/dist/commands/status.d.ts +8 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/token.d.ts +1 -0
- package/dist/commands/token.d.ts.map +1 -1
- package/dist/lib/api-client.d.ts +0 -11
- package/dist/lib/api-client.d.ts.map +1 -1
- package/dist/lib/command-line.d.ts +176 -0
- package/dist/lib/command-line.d.ts.map +1 -0
- package/dist/lib/dev-supervisor.d.ts +4 -0
- package/dist/lib/dev-supervisor.d.ts.map +1 -0
- package/dist/lib/dev-watch.d.ts +7 -0
- package/dist/lib/dev-watch.d.ts.map +1 -0
- package/dist/lib/errors.d.ts +6 -0
- package/dist/lib/errors.d.ts.map +1 -1
- package/dist/lib/instance-command.d.ts +3 -0
- package/dist/lib/instance-command.d.ts.map +1 -0
- package/dist/lib/loadSixb.d.ts.map +1 -1
- package/dist/lib/production.d.ts +1 -0
- package/dist/lib/production.d.ts.map +1 -1
- package/dist/lib/profile-api-client.d.ts +6 -0
- package/dist/lib/profile-api-client.d.ts.map +1 -0
- package/dist/lib/profiles.d.ts +34 -0
- package/dist/lib/profiles.d.ts.map +1 -0
- package/dist/lib/runtime.d.ts.map +1 -1
- package/dist/lib/worker-registry.d.ts.map +1 -1
- package/dist/ui/index.d.ts +5 -5
- package/dist/ui/index.d.ts.map +1 -1
- package/package.json +16 -15
- package/src/commands/api.tsx +3 -5
- package/src/commands/app.tsx +2 -8
- package/src/commands/dev-runner.tsx +181 -0
- package/src/commands/dev.tsx +27 -157
- package/src/commands/init.tsx +1 -13
- package/src/commands/login.tsx +196 -0
- package/src/commands/logout.tsx +36 -0
- package/src/commands/profile.tsx +101 -0
- package/src/commands/service-account.tsx +18 -15
- package/src/commands/status.tsx +93 -0
- package/src/commands/token.tsx +11 -8
- package/src/index.tsx +134 -156
- package/src/lib/api-client.ts +4 -65
- package/src/lib/command-line.ts +743 -0
- package/src/lib/dev-supervisor.ts +170 -0
- package/src/lib/dev-watch.ts +200 -0
- package/src/lib/errors.ts +13 -0
- package/src/lib/instance-command.ts +80 -0
- package/src/lib/loadSixb.ts +0 -2
- package/src/lib/production.ts +11 -0
- package/src/lib/profile-api-client.ts +38 -0
- package/src/lib/profiles.ts +262 -0
- package/src/lib/runtime.ts +10 -4
- package/src/lib/worker-registry.ts +7 -1
- package/src/ui/index.tsx +39 -140
- package/dist/commands/auth.d.ts +0 -8
- package/dist/commands/auth.d.ts.map +0 -1
- package/src/commands/auth.tsx +0 -96
package/src/commands/init.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { relative, sep } from "node:path"
|
|
2
2
|
import { scaffoldProject } from "create-sixb/scaffold"
|
|
3
|
-
import {
|
|
3
|
+
import { InitView, renderStatic } from "../ui"
|
|
4
4
|
|
|
5
5
|
export async function runInit(dir?: string): Promise<void> {
|
|
6
6
|
const result = await scaffoldProject(dir ?? ".", { allowExisting: true })
|
|
@@ -15,18 +15,6 @@ export async function runInit(dir?: string): Promise<void> {
|
|
|
15
15
|
)
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export async function runCreate(name: string): Promise<void> {
|
|
19
|
-
const result = await scaffoldProject(name)
|
|
20
|
-
|
|
21
|
-
await renderStatic(
|
|
22
|
-
<CreateView
|
|
23
|
-
name={result.name}
|
|
24
|
-
targetDir={result.targetDir}
|
|
25
|
-
commands={nextStepsFor(result.targetDir)}
|
|
26
|
-
/>
|
|
27
|
-
)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
18
|
function nextStepsFor(targetDir: string): string[] {
|
|
31
19
|
const target = relative(process.cwd(), targetDir)
|
|
32
20
|
const commands: string[] = []
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { CliError, createInstanceApiClient, writeJson } from "@sixb/cli-core"
|
|
2
|
+
import { normalizeApiUrl } from "../lib/api-client"
|
|
3
|
+
import { assertProfileName, updateConfig } from "../lib/profiles"
|
|
4
|
+
import { KeyValueResultView, renderStatic } from "../ui"
|
|
5
|
+
|
|
6
|
+
export interface LoginCommandOptions {
|
|
7
|
+
readonly apiUrl?: string
|
|
8
|
+
readonly profile?: string
|
|
9
|
+
readonly tokenStdin?: boolean
|
|
10
|
+
readonly json?: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function runLogin(options: LoginCommandOptions = {}): Promise<void> {
|
|
14
|
+
if (!options.apiUrl?.trim()) {
|
|
15
|
+
throw new Error("Usage: sixb login <api-url> [--profile <name>] [--token-stdin]")
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const apiUrl = normalizeApiUrl(options.apiUrl)
|
|
19
|
+
let token: string | undefined
|
|
20
|
+
let projectId: string
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
projectId = await fetchProject(apiUrl)
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (!isAuthorizationError(error)) throw error
|
|
26
|
+
token = options.tokenStdin
|
|
27
|
+
? await readTokenFromStdin()
|
|
28
|
+
: await authorizeDevice(apiUrl, options.profile?.trim() || "sixb CLI")
|
|
29
|
+
projectId = await fetchProject(apiUrl, token)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const profile = options.profile?.trim() || projectId
|
|
33
|
+
assertProfileName(profile)
|
|
34
|
+
await updateConfig((config) => ({
|
|
35
|
+
version: 1,
|
|
36
|
+
currentProfile: profile,
|
|
37
|
+
profiles: {
|
|
38
|
+
...config.profiles,
|
|
39
|
+
[profile]: { apiUrl, projectId, ...(token ? { token } : {}) },
|
|
40
|
+
},
|
|
41
|
+
}))
|
|
42
|
+
|
|
43
|
+
const result = { profile, projectId, apiUrl, authenticated: Boolean(token) }
|
|
44
|
+
if (options.json) {
|
|
45
|
+
writeJson(result)
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
await renderStatic(
|
|
50
|
+
<KeyValueResultView
|
|
51
|
+
title={`Logged in with profile "${profile}"`}
|
|
52
|
+
items={[
|
|
53
|
+
{ label: "Project", value: projectId },
|
|
54
|
+
{ label: "API", value: apiUrl },
|
|
55
|
+
{ label: "Authentication", value: token ? "stored token" : "not required" },
|
|
56
|
+
]}
|
|
57
|
+
/>
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface DeviceAuthorizationStart {
|
|
62
|
+
readonly deviceCode: string
|
|
63
|
+
readonly userCode: string
|
|
64
|
+
readonly verificationUriComplete: string
|
|
65
|
+
readonly expiresAt: string
|
|
66
|
+
readonly interval: number
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function authorizeDevice(apiUrl: string, tokenName: string): Promise<string> {
|
|
70
|
+
const api = createInstanceApiClient({ kind: "local", baseUrl: apiUrl })
|
|
71
|
+
const start = parseDeviceAuthorizationStart(
|
|
72
|
+
await api.post("/api/auth/device-authorizations", {
|
|
73
|
+
clientName: "sixb CLI",
|
|
74
|
+
tokenName,
|
|
75
|
+
expiresIn: "90d",
|
|
76
|
+
}),
|
|
77
|
+
apiUrl
|
|
78
|
+
)
|
|
79
|
+
process.stderr.write(`Opening ${start.verificationUriComplete}\n`)
|
|
80
|
+
process.stderr.write(`Confirm code: ${start.userCode}\n`)
|
|
81
|
+
await openBrowser(start.verificationUriComplete)
|
|
82
|
+
process.stderr.write("Waiting for browser authorization...\n")
|
|
83
|
+
|
|
84
|
+
const serverDeadline = new Date(start.expiresAt).getTime()
|
|
85
|
+
const deadline = Math.min(serverDeadline, Date.now() + 15 * 60 * 1000)
|
|
86
|
+
const intervalMs = Math.max(1, Math.min(10, start.interval)) * 1000
|
|
87
|
+
while (Date.now() < deadline) {
|
|
88
|
+
const result = await api.post("/api/auth/device-authorizations/token", {
|
|
89
|
+
deviceCode: start.deviceCode,
|
|
90
|
+
})
|
|
91
|
+
if (!isRecord(result) || typeof result.status !== "string") {
|
|
92
|
+
throw new Error("[SixbCLI] The Sixb API returned an invalid device authorization response.")
|
|
93
|
+
}
|
|
94
|
+
if (result.status === "approved" && typeof result.accessToken === "string") {
|
|
95
|
+
return requireToken(result.accessToken)
|
|
96
|
+
}
|
|
97
|
+
if (result.status === "denied") {
|
|
98
|
+
throw new Error("[SixbCLI] Browser authorization was denied.")
|
|
99
|
+
}
|
|
100
|
+
if (result.status === "expired") {
|
|
101
|
+
throw new Error("[SixbCLI] Browser authorization expired. Run `sixb login` again.")
|
|
102
|
+
}
|
|
103
|
+
if (result.status !== "pending") {
|
|
104
|
+
throw new Error("[SixbCLI] The Sixb API returned an unknown authorization status.")
|
|
105
|
+
}
|
|
106
|
+
await Bun.sleep(intervalMs)
|
|
107
|
+
}
|
|
108
|
+
throw new Error("[SixbCLI] Browser authorization expired. Run `sixb login` again.")
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function parseDeviceAuthorizationStart(value: unknown, apiUrl: string): DeviceAuthorizationStart {
|
|
112
|
+
if (
|
|
113
|
+
!isRecord(value) ||
|
|
114
|
+
typeof value.deviceCode !== "string" ||
|
|
115
|
+
!value.deviceCode ||
|
|
116
|
+
value.deviceCode.length > 1_024 ||
|
|
117
|
+
typeof value.userCode !== "string" ||
|
|
118
|
+
!value.userCode ||
|
|
119
|
+
typeof value.verificationUriComplete !== "string" ||
|
|
120
|
+
typeof value.expiresAt !== "string" ||
|
|
121
|
+
typeof value.interval !== "number" ||
|
|
122
|
+
!Number.isFinite(value.interval) ||
|
|
123
|
+
!Number.isFinite(new Date(value.expiresAt).getTime())
|
|
124
|
+
) {
|
|
125
|
+
throw new Error("[SixbCLI] The Sixb API returned an invalid device authorization.")
|
|
126
|
+
}
|
|
127
|
+
const verificationUrl = new URL(value.verificationUriComplete)
|
|
128
|
+
if (
|
|
129
|
+
verificationUrl.origin !== new URL(apiUrl).origin ||
|
|
130
|
+
(verificationUrl.protocol !== "http:" && verificationUrl.protocol !== "https:")
|
|
131
|
+
) {
|
|
132
|
+
throw new Error("[SixbCLI] The device verification URL does not match the Sixb API origin.")
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
deviceCode: value.deviceCode,
|
|
136
|
+
userCode: value.userCode,
|
|
137
|
+
verificationUriComplete: value.verificationUriComplete,
|
|
138
|
+
expiresAt: value.expiresAt,
|
|
139
|
+
interval: value.interval,
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function openBrowser(url: string): Promise<void> {
|
|
144
|
+
if (process.env.SIXB_CLI_NO_BROWSER === "1") return
|
|
145
|
+
const command =
|
|
146
|
+
process.platform === "darwin"
|
|
147
|
+
? ["open", url]
|
|
148
|
+
: process.platform === "win32"
|
|
149
|
+
? ["cmd.exe", "/c", "start", "", url]
|
|
150
|
+
: ["xdg-open", url]
|
|
151
|
+
try {
|
|
152
|
+
const process = Bun.spawn({ cmd: command, stdout: "ignore", stderr: "ignore" })
|
|
153
|
+
await process.exited
|
|
154
|
+
} catch {
|
|
155
|
+
// The URL and confirmation code were printed, so manual completion remains available.
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function fetchProject(apiUrl: string, token?: string): Promise<string> {
|
|
160
|
+
const api = createInstanceApiClient({
|
|
161
|
+
kind: "local",
|
|
162
|
+
baseUrl: apiUrl,
|
|
163
|
+
...(token ? { token } : {}),
|
|
164
|
+
})
|
|
165
|
+
const result = await api.get("/api/project")
|
|
166
|
+
if (!isRecord(result) || typeof result.id !== "string" || !result.id.trim()) {
|
|
167
|
+
throw new Error("[SixbCLI] The Sixb API returned project metadata without a project id.")
|
|
168
|
+
}
|
|
169
|
+
return result.id.trim()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function isAuthorizationError(error: unknown): boolean {
|
|
173
|
+
return error instanceof CliError && (error.body.status === 401 || error.body.status === 403)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function readTokenFromStdin(): Promise<string> {
|
|
177
|
+
let value = ""
|
|
178
|
+
for await (const chunk of process.stdin) {
|
|
179
|
+
value += chunk.toString()
|
|
180
|
+
if (value.length > 16_384) {
|
|
181
|
+
throw new Error("[SixbCLI] The token read from stdin is too large.")
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return requireToken(value)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function requireToken(value: string): string {
|
|
188
|
+
const token = value.trim()
|
|
189
|
+
if (!token) throw new Error("[SixbCLI] API token cannot be empty.")
|
|
190
|
+
if (/\s/.test(token)) throw new Error("[SixbCLI] API token cannot contain whitespace.")
|
|
191
|
+
return token
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
195
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
196
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { writeJson } from "@sixb/cli-core"
|
|
2
|
+
import { readConfig, requireProfile, updateConfig } from "../lib/profiles"
|
|
3
|
+
import { KeyValueResultView, renderStatic } from "../ui"
|
|
4
|
+
|
|
5
|
+
export interface LogoutCommandOptions {
|
|
6
|
+
readonly profile?: string
|
|
7
|
+
readonly json?: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function runLogout(options: LogoutCommandOptions = {}): Promise<void> {
|
|
11
|
+
const current = await readConfig()
|
|
12
|
+
const name = options.profile?.trim() || current.currentProfile
|
|
13
|
+
if (!name) throw new Error("[SixbCLI] No current profile to remove.")
|
|
14
|
+
requireProfile(current, name)
|
|
15
|
+
|
|
16
|
+
await updateConfig((config) => {
|
|
17
|
+
const profiles = { ...config.profiles }
|
|
18
|
+
delete profiles[name]
|
|
19
|
+
return {
|
|
20
|
+
version: 1,
|
|
21
|
+
...(config.currentProfile !== name ? { currentProfile: config.currentProfile } : {}),
|
|
22
|
+
profiles,
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
if (options.json) {
|
|
27
|
+
writeJson({ removedProfile: name })
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
await renderStatic(
|
|
31
|
+
<KeyValueResultView
|
|
32
|
+
title={`Removed profile "${name}"`}
|
|
33
|
+
items={[{ label: "Profile", value: name }]}
|
|
34
|
+
/>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { writeJson } from "@sixb/cli-core"
|
|
2
|
+
import { readConfig, requireProfile, updateConfig } from "../lib/profiles"
|
|
3
|
+
import { KeyValueResultView, renderStatic, TableResultView } from "../ui"
|
|
4
|
+
|
|
5
|
+
export interface ProfileCommandOptions {
|
|
6
|
+
readonly action?: string
|
|
7
|
+
readonly name?: string
|
|
8
|
+
readonly json?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function runProfile(options: ProfileCommandOptions = {}): Promise<void> {
|
|
12
|
+
const action = options.action
|
|
13
|
+
if (action === "list") return listProfiles(options)
|
|
14
|
+
if (action === "show") return showProfile(options)
|
|
15
|
+
if (action === "use") return selectProfile(options)
|
|
16
|
+
throw new Error("Usage: sixb profile <list|show|use> [name]")
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function listProfiles(options: ProfileCommandOptions): Promise<void> {
|
|
20
|
+
const config = await readConfig()
|
|
21
|
+
const profiles = Object.entries(config.profiles)
|
|
22
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
23
|
+
.map(([name, profile]) => ({
|
|
24
|
+
name,
|
|
25
|
+
current: name === config.currentProfile,
|
|
26
|
+
projectId: profile.projectId,
|
|
27
|
+
apiUrl: profile.apiUrl,
|
|
28
|
+
authenticated: Boolean(profile.token),
|
|
29
|
+
}))
|
|
30
|
+
|
|
31
|
+
if (options.json) {
|
|
32
|
+
writeJson({ currentProfile: config.currentProfile ?? null, profiles })
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
await renderStatic(
|
|
37
|
+
<TableResultView
|
|
38
|
+
title="Sixb profiles"
|
|
39
|
+
headers={["Current", "Profile", "Project", "API", "Token"]}
|
|
40
|
+
rows={profiles.map((profile) => [
|
|
41
|
+
profile.current ? "*" : "",
|
|
42
|
+
profile.name,
|
|
43
|
+
profile.projectId,
|
|
44
|
+
profile.apiUrl,
|
|
45
|
+
profile.authenticated ? "yes" : "no",
|
|
46
|
+
])}
|
|
47
|
+
emptyMessage="No profiles. Run `sixb login <api-url>`."
|
|
48
|
+
/>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function showProfile(options: ProfileCommandOptions): Promise<void> {
|
|
53
|
+
const config = await readConfig()
|
|
54
|
+
const name = options.name?.trim() || config.currentProfile
|
|
55
|
+
if (!name) throw new Error("[SixbCLI] No current profile. Run `sixb login <api-url>`.")
|
|
56
|
+
const profile = requireProfile(config, name)
|
|
57
|
+
const result = {
|
|
58
|
+
name,
|
|
59
|
+
current: name === config.currentProfile,
|
|
60
|
+
projectId: profile.projectId,
|
|
61
|
+
apiUrl: profile.apiUrl,
|
|
62
|
+
authenticated: Boolean(profile.token),
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (options.json) {
|
|
66
|
+
writeJson(result)
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
await renderStatic(
|
|
71
|
+
<KeyValueResultView
|
|
72
|
+
title={`Profile "${name}"`}
|
|
73
|
+
items={[
|
|
74
|
+
{ label: "Current", value: result.current ? "yes" : "no" },
|
|
75
|
+
{ label: "Project", value: result.projectId },
|
|
76
|
+
{ label: "API", value: result.apiUrl },
|
|
77
|
+
{ label: "Stored token", value: result.authenticated ? "yes" : "no" },
|
|
78
|
+
]}
|
|
79
|
+
/>
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function selectProfile(options: ProfileCommandOptions): Promise<void> {
|
|
84
|
+
const name = options.name?.trim()
|
|
85
|
+
if (!name) throw new Error("Usage: sixb profile use <name>")
|
|
86
|
+
await updateConfig((config) => {
|
|
87
|
+
requireProfile(config, name)
|
|
88
|
+
return { ...config, currentProfile: name }
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
if (options.json) {
|
|
92
|
+
writeJson({ currentProfile: name })
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
await renderStatic(
|
|
96
|
+
<KeyValueResultView
|
|
97
|
+
title={`Using profile "${name}"`}
|
|
98
|
+
items={[{ label: "Profile", value: name }]}
|
|
99
|
+
/>
|
|
100
|
+
)
|
|
101
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { writeJson } from "@sixb/cli-core"
|
|
1
2
|
import {
|
|
2
3
|
createAuthServiceAccount,
|
|
3
4
|
createAuthServiceAccountAccessToken,
|
|
@@ -7,7 +8,8 @@ import {
|
|
|
7
8
|
listAuthServiceAccounts,
|
|
8
9
|
revokeAuthServiceAccountAccessToken,
|
|
9
10
|
} from "@sixb/client"
|
|
10
|
-
import { createCliSixbClient,
|
|
11
|
+
import { createCliSixbClient, unwrapSixbApiResult } from "../lib/api-client"
|
|
12
|
+
import { resolveProfileApiClientConfig } from "../lib/profile-api-client"
|
|
11
13
|
import { KeyValueResultView, renderStatic, SecretResultView, TableResultView } from "../ui"
|
|
12
14
|
import {
|
|
13
15
|
formatDate,
|
|
@@ -22,6 +24,7 @@ export interface ServiceAccountCommandOptions {
|
|
|
22
24
|
readonly positionals?: readonly string[]
|
|
23
25
|
readonly apiUrl?: string
|
|
24
26
|
readonly token?: string
|
|
27
|
+
readonly profile?: string
|
|
25
28
|
readonly id?: string
|
|
26
29
|
readonly name?: string
|
|
27
30
|
readonly description?: string
|
|
@@ -32,7 +35,7 @@ export interface ServiceAccountCommandOptions {
|
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
export async function runServiceAccount(options: ServiceAccountCommandOptions = {}) {
|
|
35
|
-
const command = options.positionals?.[0]
|
|
38
|
+
const command = options.positionals?.[0]
|
|
36
39
|
if (command === "token") {
|
|
37
40
|
await runServiceAccountToken(options)
|
|
38
41
|
return
|
|
@@ -57,11 +60,11 @@ export async function runServiceAccount(options: ServiceAccountCommandOptions =
|
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
async function listServiceAccounts(options: ServiceAccountCommandOptions) {
|
|
60
|
-
const client = createCliSixbClient(
|
|
63
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
61
64
|
const result = unwrapSixbApiResult(await listAuthServiceAccounts({ client }))
|
|
62
65
|
|
|
63
66
|
if (options.json) {
|
|
64
|
-
|
|
67
|
+
writeJson(result)
|
|
65
68
|
return
|
|
66
69
|
}
|
|
67
70
|
|
|
@@ -85,7 +88,7 @@ async function createServiceAccount(options: ServiceAccountCommandOptions) {
|
|
|
85
88
|
const id = options.id?.trim()
|
|
86
89
|
const description = options.description?.trim()
|
|
87
90
|
const groupIds = normalizeGroupIds(options.groupIds)
|
|
88
|
-
const client = createCliSixbClient(
|
|
91
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
89
92
|
const result = unwrapSixbApiResult(
|
|
90
93
|
await createAuthServiceAccount({
|
|
91
94
|
client,
|
|
@@ -99,7 +102,7 @@ async function createServiceAccount(options: ServiceAccountCommandOptions) {
|
|
|
99
102
|
)
|
|
100
103
|
|
|
101
104
|
if (options.json) {
|
|
102
|
-
|
|
105
|
+
writeJson(result)
|
|
103
106
|
return
|
|
104
107
|
}
|
|
105
108
|
|
|
@@ -122,7 +125,7 @@ async function disableServiceAccount(options: ServiceAccountCommandOptions) {
|
|
|
122
125
|
throw new Error("Usage: sixb service-account disable <service-account-id>")
|
|
123
126
|
}
|
|
124
127
|
|
|
125
|
-
const client = createCliSixbClient(
|
|
128
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
126
129
|
const result = unwrapSixbApiResult(
|
|
127
130
|
await disableAuthServiceAccount({
|
|
128
131
|
client,
|
|
@@ -131,7 +134,7 @@ async function disableServiceAccount(options: ServiceAccountCommandOptions) {
|
|
|
131
134
|
)
|
|
132
135
|
|
|
133
136
|
if (options.json) {
|
|
134
|
-
|
|
137
|
+
writeJson(result)
|
|
135
138
|
return
|
|
136
139
|
}
|
|
137
140
|
|
|
@@ -148,7 +151,7 @@ async function disableServiceAccount(options: ServiceAccountCommandOptions) {
|
|
|
148
151
|
}
|
|
149
152
|
|
|
150
153
|
async function runServiceAccountToken(options: ServiceAccountCommandOptions) {
|
|
151
|
-
const action = options.positionals?.[1]
|
|
154
|
+
const action = options.positionals?.[1]
|
|
152
155
|
if (action === "list") {
|
|
153
156
|
await listServiceAccountTokens(options)
|
|
154
157
|
return
|
|
@@ -172,7 +175,7 @@ async function listServiceAccountTokens(options: ServiceAccountCommandOptions) {
|
|
|
172
175
|
options,
|
|
173
176
|
"Usage: sixb service-account token list <service-account-id>"
|
|
174
177
|
)
|
|
175
|
-
const client = createCliSixbClient(
|
|
178
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
176
179
|
const result = unwrapSixbApiResult(
|
|
177
180
|
await listAuthServiceAccountAccessTokens({
|
|
178
181
|
client,
|
|
@@ -181,7 +184,7 @@ async function listServiceAccountTokens(options: ServiceAccountCommandOptions) {
|
|
|
181
184
|
)
|
|
182
185
|
|
|
183
186
|
if (options.json) {
|
|
184
|
-
|
|
187
|
+
writeJson(result)
|
|
185
188
|
return
|
|
186
189
|
}
|
|
187
190
|
|
|
@@ -215,7 +218,7 @@ async function createServiceAccountToken(options: ServiceAccountCommandOptions)
|
|
|
215
218
|
|
|
216
219
|
const expiresAt = resolveExpiration(options)
|
|
217
220
|
const groupIds = normalizeGroupIds(options.groupIds)
|
|
218
|
-
const client = createCliSixbClient(
|
|
221
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
219
222
|
const result = unwrapSixbApiResult(
|
|
220
223
|
await createAuthServiceAccountAccessToken({
|
|
221
224
|
client,
|
|
@@ -229,7 +232,7 @@ async function createServiceAccountToken(options: ServiceAccountCommandOptions)
|
|
|
229
232
|
)
|
|
230
233
|
|
|
231
234
|
if (options.json) {
|
|
232
|
-
|
|
235
|
+
writeJson(result)
|
|
233
236
|
return
|
|
234
237
|
}
|
|
235
238
|
|
|
@@ -258,7 +261,7 @@ async function revokeServiceAccountToken(options: ServiceAccountCommandOptions)
|
|
|
258
261
|
throw new Error("Usage: sixb service-account token revoke <service-account-id> <token-id>")
|
|
259
262
|
}
|
|
260
263
|
|
|
261
|
-
const client = createCliSixbClient(
|
|
264
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
262
265
|
const result = unwrapSixbApiResult(
|
|
263
266
|
await revokeAuthServiceAccountAccessToken({
|
|
264
267
|
client,
|
|
@@ -267,7 +270,7 @@ async function revokeServiceAccountToken(options: ServiceAccountCommandOptions)
|
|
|
267
270
|
)
|
|
268
271
|
|
|
269
272
|
if (options.json) {
|
|
270
|
-
|
|
273
|
+
writeJson(result)
|
|
271
274
|
return
|
|
272
275
|
}
|
|
273
276
|
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { CliError, createInstanceApiClient, writeJson } from "@sixb/cli-core"
|
|
2
|
+
import { resolveProfile } from "../lib/profiles"
|
|
3
|
+
import { KeyValueResultView, renderStatic } from "../ui"
|
|
4
|
+
|
|
5
|
+
export interface StatusCommandOptions {
|
|
6
|
+
readonly apiUrl?: string
|
|
7
|
+
readonly token?: string
|
|
8
|
+
readonly profile?: string
|
|
9
|
+
readonly json?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function runStatus(options: StatusCommandOptions = {}): Promise<void> {
|
|
13
|
+
const resolved = await resolveProfile(options)
|
|
14
|
+
try {
|
|
15
|
+
const project = await createInstanceApiClient({
|
|
16
|
+
kind: "local",
|
|
17
|
+
baseUrl: resolved.apiUrl,
|
|
18
|
+
...(resolved.token ? { token: resolved.token } : {}),
|
|
19
|
+
...(resolved.profile ? { profile: resolved.profile } : {}),
|
|
20
|
+
}).get("/api/project")
|
|
21
|
+
const projectId = projectIdFrom(project)
|
|
22
|
+
await renderStatus({
|
|
23
|
+
connected: true,
|
|
24
|
+
authenticated: Boolean(resolved.token),
|
|
25
|
+
apiUrl: resolved.apiUrl,
|
|
26
|
+
source: resolved.source,
|
|
27
|
+
...(resolved.profile ? { profile: resolved.profile } : {}),
|
|
28
|
+
projectId,
|
|
29
|
+
json: options.json,
|
|
30
|
+
})
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (!(error instanceof CliError) || (error.body.status !== 401 && error.body.status !== 403)) {
|
|
33
|
+
throw error
|
|
34
|
+
}
|
|
35
|
+
await renderStatus({
|
|
36
|
+
connected: true,
|
|
37
|
+
authenticated: false,
|
|
38
|
+
apiUrl: resolved.apiUrl,
|
|
39
|
+
source: resolved.source,
|
|
40
|
+
...(resolved.profile ? { profile: resolved.profile } : {}),
|
|
41
|
+
message: error.message,
|
|
42
|
+
json: options.json,
|
|
43
|
+
})
|
|
44
|
+
process.exitCode = 1
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function renderStatus(input: {
|
|
49
|
+
readonly connected: boolean
|
|
50
|
+
readonly authenticated: boolean
|
|
51
|
+
readonly apiUrl: string
|
|
52
|
+
readonly source: string
|
|
53
|
+
readonly profile?: string
|
|
54
|
+
readonly projectId?: string
|
|
55
|
+
readonly message?: string
|
|
56
|
+
readonly json?: boolean
|
|
57
|
+
}): Promise<void> {
|
|
58
|
+
if (input.json) {
|
|
59
|
+
const { json: _json, ...result } = input
|
|
60
|
+
writeJson(result)
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
await renderStatic(
|
|
65
|
+
<KeyValueResultView
|
|
66
|
+
title={input.message ? "Authentication failed" : "Connected"}
|
|
67
|
+
titleColor={input.message ? "red" : "green"}
|
|
68
|
+
items={[
|
|
69
|
+
...(input.profile ? [{ label: "Profile", value: input.profile }] : []),
|
|
70
|
+
...(input.projectId ? [{ label: "Project", value: input.projectId }] : []),
|
|
71
|
+
{ label: "API", value: input.apiUrl },
|
|
72
|
+
{
|
|
73
|
+
label: "Authentication",
|
|
74
|
+
value: input.authenticated ? "token" : input.message ? "failed" : "not required",
|
|
75
|
+
},
|
|
76
|
+
]}
|
|
77
|
+
message={input.message}
|
|
78
|
+
/>
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function projectIdFrom(value: unknown): string {
|
|
83
|
+
if (
|
|
84
|
+
typeof value !== "object" ||
|
|
85
|
+
value === null ||
|
|
86
|
+
!("id" in value) ||
|
|
87
|
+
typeof value.id !== "string" ||
|
|
88
|
+
!value.id.trim()
|
|
89
|
+
) {
|
|
90
|
+
throw new Error("[SixbCLI] The Sixb API returned project metadata without a project id.")
|
|
91
|
+
}
|
|
92
|
+
return value.id.trim()
|
|
93
|
+
}
|
package/src/commands/token.tsx
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { writeJson } from "@sixb/cli-core"
|
|
1
2
|
import {
|
|
2
3
|
createAuthPersonalAccessToken,
|
|
3
4
|
listAuthAccessTokens,
|
|
4
5
|
revokeAuthAccessToken,
|
|
5
6
|
} from "@sixb/client"
|
|
6
|
-
import { createCliSixbClient,
|
|
7
|
+
import { createCliSixbClient, unwrapSixbApiResult } from "../lib/api-client"
|
|
8
|
+
import { resolveProfileApiClientConfig } from "../lib/profile-api-client"
|
|
7
9
|
import { KeyValueResultView, renderStatic, SecretResultView, TableResultView } from "../ui"
|
|
8
10
|
import {
|
|
9
11
|
formatDate,
|
|
@@ -17,6 +19,7 @@ export interface TokenCommandOptions {
|
|
|
17
19
|
readonly positionals?: readonly string[]
|
|
18
20
|
readonly apiUrl?: string
|
|
19
21
|
readonly token?: string
|
|
22
|
+
readonly profile?: string
|
|
20
23
|
readonly id?: string
|
|
21
24
|
readonly name?: string
|
|
22
25
|
readonly expiresAt?: string
|
|
@@ -26,7 +29,7 @@ export interface TokenCommandOptions {
|
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
export async function runToken(options: TokenCommandOptions = {}) {
|
|
29
|
-
const action = options.action
|
|
32
|
+
const action = options.action
|
|
30
33
|
if (action === "list") {
|
|
31
34
|
await listTokens(options)
|
|
32
35
|
return
|
|
@@ -46,11 +49,11 @@ export async function runToken(options: TokenCommandOptions = {}) {
|
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
async function listTokens(options: TokenCommandOptions) {
|
|
49
|
-
const client = createCliSixbClient(
|
|
52
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
50
53
|
const result = unwrapSixbApiResult(await listAuthAccessTokens({ client }))
|
|
51
54
|
|
|
52
55
|
if (options.json) {
|
|
53
|
-
|
|
56
|
+
writeJson(result)
|
|
54
57
|
return
|
|
55
58
|
}
|
|
56
59
|
|
|
@@ -80,7 +83,7 @@ async function createToken(options: TokenCommandOptions) {
|
|
|
80
83
|
|
|
81
84
|
const expiresAt = resolveExpiration(options)
|
|
82
85
|
const groupIds = normalizeGroupIds(options.groupIds)
|
|
83
|
-
const client = createCliSixbClient(
|
|
86
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
84
87
|
const result = unwrapSixbApiResult(
|
|
85
88
|
await createAuthPersonalAccessToken({
|
|
86
89
|
client,
|
|
@@ -93,7 +96,7 @@ async function createToken(options: TokenCommandOptions) {
|
|
|
93
96
|
)
|
|
94
97
|
|
|
95
98
|
if (options.json) {
|
|
96
|
-
|
|
99
|
+
writeJson(result)
|
|
97
100
|
return
|
|
98
101
|
}
|
|
99
102
|
|
|
@@ -117,7 +120,7 @@ async function revokeToken(options: TokenCommandOptions) {
|
|
|
117
120
|
throw new Error("Usage: sixb token revoke <token-id>")
|
|
118
121
|
}
|
|
119
122
|
|
|
120
|
-
const client = createCliSixbClient(
|
|
123
|
+
const client = createCliSixbClient(await resolveProfileApiClientConfig(options))
|
|
121
124
|
const result = unwrapSixbApiResult(
|
|
122
125
|
await revokeAuthAccessToken({
|
|
123
126
|
client,
|
|
@@ -126,7 +129,7 @@ async function revokeToken(options: TokenCommandOptions) {
|
|
|
126
129
|
)
|
|
127
130
|
|
|
128
131
|
if (options.json) {
|
|
129
|
-
|
|
132
|
+
writeJson(result)
|
|
130
133
|
return
|
|
131
134
|
}
|
|
132
135
|
|