skyboard-cli 0.1.0
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 -0
- package/bin/sb.js +2 -0
- package/package.json +32 -0
- package/src/__tests__/auth-state.test.ts +111 -0
- package/src/__tests__/card-ref.test.ts +42 -0
- package/src/__tests__/column-match.test.ts +57 -0
- package/src/__tests__/commands.test.ts +326 -0
- package/src/__tests__/helpers.ts +154 -0
- package/src/__tests__/materialize.test.ts +257 -0
- package/src/__tests__/pds.test.ts +213 -0
- package/src/commands/add.ts +47 -0
- package/src/commands/boards.ts +64 -0
- package/src/commands/cards.ts +65 -0
- package/src/commands/cols.ts +51 -0
- package/src/commands/comment.ts +64 -0
- package/src/commands/edit.ts +89 -0
- package/src/commands/login.ts +19 -0
- package/src/commands/logout.ts +14 -0
- package/src/commands/mv.ts +84 -0
- package/src/commands/new.ts +80 -0
- package/src/commands/rm.ts +79 -0
- package/src/commands/show.ts +100 -0
- package/src/commands/status.ts +78 -0
- package/src/commands/use.ts +72 -0
- package/src/commands/whoami.ts +22 -0
- package/src/index.ts +143 -0
- package/src/lib/auth.ts +244 -0
- package/src/lib/card-ref.ts +32 -0
- package/src/lib/column-match.ts +45 -0
- package/src/lib/config.ts +182 -0
- package/src/lib/display.ts +112 -0
- package/src/lib/materialize.ts +138 -0
- package/src/lib/pds.ts +440 -0
- package/src/lib/permissions.ts +28 -0
- package/src/lib/schemas.ts +97 -0
- package/src/lib/tid.ts +22 -0
- package/src/lib/types.ts +144 -0
- package/tsconfig.json +15 -0
- package/vitest.config.ts +7 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { loadAuthInfo } from "../lib/config.js";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
|
|
4
|
+
export function whoamiCommand(opts: { json?: boolean }): void {
|
|
5
|
+
const info = loadAuthInfo();
|
|
6
|
+
if (!info) {
|
|
7
|
+
if (opts.json) {
|
|
8
|
+
console.log(JSON.stringify({ loggedIn: false }));
|
|
9
|
+
} else {
|
|
10
|
+
console.log("Not logged in. Run `sb login <handle>` first.");
|
|
11
|
+
}
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (opts.json) {
|
|
16
|
+
console.log(JSON.stringify({ loggedIn: true, ...info }, null, 2));
|
|
17
|
+
} else {
|
|
18
|
+
console.log(`${chalk.bold("Handle:")} ${info.handle}`);
|
|
19
|
+
console.log(`${chalk.bold("DID:")} ${info.did}`);
|
|
20
|
+
console.log(`${chalk.bold("PDS:")} ${info.service}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { loginCommand } from "./commands/login.js";
|
|
4
|
+
import { logoutCommand } from "./commands/logout.js";
|
|
5
|
+
import { whoamiCommand } from "./commands/whoami.js";
|
|
6
|
+
import { statusCommand } from "./commands/status.js";
|
|
7
|
+
import { boardsCommand } from "./commands/boards.js";
|
|
8
|
+
import { useCommand } from "./commands/use.js";
|
|
9
|
+
import { addCommand } from "./commands/add.js";
|
|
10
|
+
import { colsCommand } from "./commands/cols.js";
|
|
11
|
+
import { cardsCommand } from "./commands/cards.js";
|
|
12
|
+
import { showCommand } from "./commands/show.js";
|
|
13
|
+
import { newCommand } from "./commands/new.js";
|
|
14
|
+
import { mvCommand } from "./commands/mv.js";
|
|
15
|
+
import { editCommand } from "./commands/edit.js";
|
|
16
|
+
import { commentCommand } from "./commands/comment.js";
|
|
17
|
+
import { rmCommand } from "./commands/rm.js";
|
|
18
|
+
|
|
19
|
+
const program = new Command();
|
|
20
|
+
|
|
21
|
+
program
|
|
22
|
+
.name("sb")
|
|
23
|
+
.description("Skyboard CLI — manage kanban boards on AT Protocol")
|
|
24
|
+
.version("0.1.0");
|
|
25
|
+
|
|
26
|
+
// Auth commands
|
|
27
|
+
program
|
|
28
|
+
.command("login")
|
|
29
|
+
.description("Log in via AT Protocol OAuth (opens browser)")
|
|
30
|
+
.argument("<handle>", "Your AT Protocol handle (e.g. alice.bsky.social)")
|
|
31
|
+
.action(loginCommand);
|
|
32
|
+
|
|
33
|
+
program
|
|
34
|
+
.command("logout")
|
|
35
|
+
.description("Log out and clear stored session")
|
|
36
|
+
.action(logoutCommand);
|
|
37
|
+
|
|
38
|
+
program
|
|
39
|
+
.command("whoami")
|
|
40
|
+
.description("Show current authenticated user")
|
|
41
|
+
.option("--json", "Output as JSON")
|
|
42
|
+
.action(whoamiCommand);
|
|
43
|
+
|
|
44
|
+
program
|
|
45
|
+
.command("status")
|
|
46
|
+
.description("Show current auth state and board summary")
|
|
47
|
+
.option("--json", "Output as JSON")
|
|
48
|
+
.action(statusCommand);
|
|
49
|
+
|
|
50
|
+
// Board navigation
|
|
51
|
+
program
|
|
52
|
+
.command("boards")
|
|
53
|
+
.description("List all boards (owned + joined)")
|
|
54
|
+
.option("--json", "Output as JSON")
|
|
55
|
+
.action(boardsCommand);
|
|
56
|
+
|
|
57
|
+
program
|
|
58
|
+
.command("use")
|
|
59
|
+
.description("Set default board for subsequent commands")
|
|
60
|
+
.argument("<board>", "Board name, rkey, AT URI, or web URL")
|
|
61
|
+
.action(useCommand);
|
|
62
|
+
|
|
63
|
+
program
|
|
64
|
+
.command("add")
|
|
65
|
+
.description("Join a board by AT URI or web URL")
|
|
66
|
+
.argument("<link>", "AT URI or web URL of the board")
|
|
67
|
+
.action(addCommand);
|
|
68
|
+
|
|
69
|
+
program
|
|
70
|
+
.command("cols")
|
|
71
|
+
.description("Show columns for the current board")
|
|
72
|
+
.option("--board <ref>", "Override default board")
|
|
73
|
+
.option("--json", "Output as JSON")
|
|
74
|
+
.action(colsCommand);
|
|
75
|
+
|
|
76
|
+
// Card operations
|
|
77
|
+
program
|
|
78
|
+
.command("cards")
|
|
79
|
+
.description("List cards grouped by column")
|
|
80
|
+
.option("-c, --column <column>", "Filter by column (name, prefix, or number)")
|
|
81
|
+
.option("-l, --label <label>", "Filter by label name")
|
|
82
|
+
.option("-s, --search <text>", "Search in title and description")
|
|
83
|
+
.option("--board <ref>", "Override default board")
|
|
84
|
+
.option("--json", "Output as JSON")
|
|
85
|
+
.action(cardsCommand);
|
|
86
|
+
|
|
87
|
+
program
|
|
88
|
+
.command("new")
|
|
89
|
+
.description("Create a new card")
|
|
90
|
+
.argument("<title>", "Card title")
|
|
91
|
+
.option("-c, --column <column>", "Target column (default: first column)")
|
|
92
|
+
.option("-d, --description <desc>", "Card description")
|
|
93
|
+
.option("--board <ref>", "Override default board")
|
|
94
|
+
.option("--json", "Output as JSON")
|
|
95
|
+
.action(newCommand);
|
|
96
|
+
|
|
97
|
+
program
|
|
98
|
+
.command("show")
|
|
99
|
+
.description("Show card details, comments, and history")
|
|
100
|
+
.argument("<ref>", "Card reference (rkey prefix, min 4 chars)")
|
|
101
|
+
.option("--board <ref>", "Override default board")
|
|
102
|
+
.option("--json", "Output as JSON")
|
|
103
|
+
.action(showCommand);
|
|
104
|
+
|
|
105
|
+
program
|
|
106
|
+
.command("mv")
|
|
107
|
+
.description("Move a card to a different column")
|
|
108
|
+
.argument("<ref>", "Card reference (rkey prefix)")
|
|
109
|
+
.argument("<column>", "Target column (name, prefix, or number)")
|
|
110
|
+
.option("--board <ref>", "Override default board")
|
|
111
|
+
.option("--json", "Output as JSON")
|
|
112
|
+
.action(mvCommand);
|
|
113
|
+
|
|
114
|
+
program
|
|
115
|
+
.command("edit")
|
|
116
|
+
.description("Edit card fields")
|
|
117
|
+
.argument("<ref>", "Card reference (rkey prefix)")
|
|
118
|
+
.option("-t, --title <title>", "New title")
|
|
119
|
+
.option("-d, --description <desc>", "New description")
|
|
120
|
+
.option("-l, --label <label...>", "Set labels (by name)")
|
|
121
|
+
.option("--board <ref>", "Override default board")
|
|
122
|
+
.option("--json", "Output as JSON")
|
|
123
|
+
.action(editCommand);
|
|
124
|
+
|
|
125
|
+
program
|
|
126
|
+
.command("comment")
|
|
127
|
+
.description("Add a comment to a card")
|
|
128
|
+
.argument("<ref>", "Card reference (rkey prefix)")
|
|
129
|
+
.argument("<text>", "Comment text")
|
|
130
|
+
.option("--board <ref>", "Override default board")
|
|
131
|
+
.option("--json", "Output as JSON")
|
|
132
|
+
.action(commentCommand);
|
|
133
|
+
|
|
134
|
+
program
|
|
135
|
+
.command("rm")
|
|
136
|
+
.description("Delete a card (owner only)")
|
|
137
|
+
.argument("<ref>", "Card reference (rkey prefix)")
|
|
138
|
+
.option("-f, --force", "Skip confirmation")
|
|
139
|
+
.option("--board <ref>", "Override default board")
|
|
140
|
+
.option("--json", "Output as JSON")
|
|
141
|
+
.action(rmCommand);
|
|
142
|
+
|
|
143
|
+
program.parse();
|
package/src/lib/auth.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { Agent } from "@atproto/api";
|
|
2
|
+
import { NodeOAuthClient } from "@atproto/oauth-client-node";
|
|
3
|
+
import type { NodeSavedSession, NodeSavedState } from "@atproto/oauth-client-node";
|
|
4
|
+
import { requestLocalLock } from "@atproto/oauth-client";
|
|
5
|
+
import { createServer } from "node:http";
|
|
6
|
+
import {
|
|
7
|
+
writeStateFile,
|
|
8
|
+
readStateFile,
|
|
9
|
+
deleteStateFile,
|
|
10
|
+
writeSessionFile,
|
|
11
|
+
readSessionFile,
|
|
12
|
+
deleteSessionFile,
|
|
13
|
+
loadAuthInfo,
|
|
14
|
+
saveAuthInfo,
|
|
15
|
+
clearAuthInfo,
|
|
16
|
+
} from "./config.js";
|
|
17
|
+
|
|
18
|
+
const OAUTH_SCOPE =
|
|
19
|
+
"atproto repo:dev.skyboard.board repo:dev.skyboard.task repo:dev.skyboard.op repo:dev.skyboard.trust repo:dev.skyboard.comment repo:dev.skyboard.approval repo:dev.skyboard.reaction";
|
|
20
|
+
|
|
21
|
+
// File-based stores implementing the NodeOAuthClient interfaces
|
|
22
|
+
const stateStore = {
|
|
23
|
+
async set(key: string, state: NodeSavedState): Promise<void> {
|
|
24
|
+
writeStateFile(key, state);
|
|
25
|
+
},
|
|
26
|
+
async get(key: string): Promise<NodeSavedState | undefined> {
|
|
27
|
+
return readStateFile(key) as NodeSavedState | undefined;
|
|
28
|
+
},
|
|
29
|
+
async del(key: string): Promise<void> {
|
|
30
|
+
deleteStateFile(key);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const sessionStore = {
|
|
35
|
+
async set(sub: string, session: NodeSavedSession): Promise<void> {
|
|
36
|
+
writeSessionFile(sub, session);
|
|
37
|
+
},
|
|
38
|
+
async get(sub: string): Promise<NodeSavedSession | undefined> {
|
|
39
|
+
return readSessionFile(sub) as NodeSavedSession | undefined;
|
|
40
|
+
},
|
|
41
|
+
async del(sub: string): Promise<void> {
|
|
42
|
+
deleteSessionFile(sub);
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function createOAuthClient(port: number): NodeOAuthClient {
|
|
47
|
+
const redirectUri = `http://127.0.0.1:${port}/callback`;
|
|
48
|
+
return new NodeOAuthClient({
|
|
49
|
+
clientMetadata: {
|
|
50
|
+
client_id: `http://localhost?redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(OAUTH_SCOPE)}`,
|
|
51
|
+
client_name: "Skyboard CLI",
|
|
52
|
+
redirect_uris: [redirectUri],
|
|
53
|
+
scope: OAUTH_SCOPE,
|
|
54
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
55
|
+
response_types: ["code"],
|
|
56
|
+
token_endpoint_auth_method: "none",
|
|
57
|
+
application_type: "native",
|
|
58
|
+
dpop_bound_access_tokens: true,
|
|
59
|
+
},
|
|
60
|
+
stateStore,
|
|
61
|
+
sessionStore,
|
|
62
|
+
requestLock: requestLocalLock,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Find a free port by binding to port 0.
|
|
68
|
+
*/
|
|
69
|
+
async function findFreePort(): Promise<number> {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const srv = createServer();
|
|
72
|
+
srv.listen(0, "127.0.0.1", () => {
|
|
73
|
+
const addr = srv.address();
|
|
74
|
+
if (addr && typeof addr === "object") {
|
|
75
|
+
const port = addr.port;
|
|
76
|
+
srv.close(() => resolve(port));
|
|
77
|
+
} else {
|
|
78
|
+
srv.close(() => reject(new Error("Could not determine port")));
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
srv.on("error", reject);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Perform OAuth loopback login. Opens browser for authorization,
|
|
87
|
+
* starts a local HTTP server for the callback.
|
|
88
|
+
* Returns the DID and handle of the authenticated user.
|
|
89
|
+
*/
|
|
90
|
+
export async function login(handle: string): Promise<{ did: string; handle: string }> {
|
|
91
|
+
const port = await findFreePort();
|
|
92
|
+
const client = createOAuthClient(port);
|
|
93
|
+
|
|
94
|
+
const authUrl = await client.authorize(handle, {
|
|
95
|
+
scope: OAUTH_SCOPE,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Start callback server
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
const timeout = setTimeout(() => {
|
|
101
|
+
server.close();
|
|
102
|
+
reject(new Error("Login timed out after 120 seconds"));
|
|
103
|
+
}, 120_000);
|
|
104
|
+
|
|
105
|
+
const server = createServer(async (req, res) => {
|
|
106
|
+
if (!req.url?.startsWith("/callback")) {
|
|
107
|
+
res.writeHead(404);
|
|
108
|
+
res.end("Not found");
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
const url = new URL(req.url, `http://127.0.0.1:${port}`);
|
|
114
|
+
const { session } = await client.callback(url.searchParams);
|
|
115
|
+
|
|
116
|
+
const did = session.did;
|
|
117
|
+
// Resolve handle from DID
|
|
118
|
+
const agent = new Agent(session);
|
|
119
|
+
let resolvedHandle = handle;
|
|
120
|
+
try {
|
|
121
|
+
const profile = await agent.getProfile({ actor: did });
|
|
122
|
+
resolvedHandle = profile.data.handle;
|
|
123
|
+
} catch {
|
|
124
|
+
// Use the provided handle as fallback
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Find the PDS endpoint for this DID
|
|
128
|
+
let service = "https://bsky.social";
|
|
129
|
+
try {
|
|
130
|
+
const didDoc = await resolveDIDDocument(did);
|
|
131
|
+
if (didDoc) {
|
|
132
|
+
const services = (didDoc as Record<string, unknown>).service as
|
|
133
|
+
| Array<{ id: string; type: string; serviceEndpoint: string }>
|
|
134
|
+
| undefined;
|
|
135
|
+
const pds = services?.find(
|
|
136
|
+
(s) => s.id === "#atproto_pds" || s.type === "AtprotoPersonalDataServer",
|
|
137
|
+
);
|
|
138
|
+
if (pds?.serviceEndpoint) {
|
|
139
|
+
service = pds.serviceEndpoint;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} catch {
|
|
143
|
+
// fallback to default
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
saveAuthInfo({ did, handle: resolvedHandle, service });
|
|
147
|
+
|
|
148
|
+
res.writeHead(200, { "Content-Type": "text/html" });
|
|
149
|
+
res.end(`
|
|
150
|
+
<html><body style="font-family: system-ui; text-align: center; padding: 40px;">
|
|
151
|
+
<h2>Logged in to Skyboard CLI</h2>
|
|
152
|
+
<p>You can close this tab and return to your terminal.</p>
|
|
153
|
+
</body></html>
|
|
154
|
+
`);
|
|
155
|
+
|
|
156
|
+
clearTimeout(timeout);
|
|
157
|
+
server.close();
|
|
158
|
+
resolve({ did, handle: resolvedHandle });
|
|
159
|
+
} catch (err) {
|
|
160
|
+
res.writeHead(500, { "Content-Type": "text/html" });
|
|
161
|
+
res.end(`
|
|
162
|
+
<html><body style="font-family: system-ui; text-align: center; padding: 40px;">
|
|
163
|
+
<h2>Login failed</h2>
|
|
164
|
+
<p>${err instanceof Error ? err.message : "Unknown error"}</p>
|
|
165
|
+
</body></html>
|
|
166
|
+
`);
|
|
167
|
+
clearTimeout(timeout);
|
|
168
|
+
server.close();
|
|
169
|
+
reject(err);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
server.listen(port, "127.0.0.1", async () => {
|
|
174
|
+
// Open browser
|
|
175
|
+
try {
|
|
176
|
+
const open = (await import("open")).default;
|
|
177
|
+
await open(authUrl.toString());
|
|
178
|
+
console.log(`\nOpened browser for login. Waiting for authorization...`);
|
|
179
|
+
console.log(`If the browser didn't open, visit:\n${authUrl.toString()}\n`);
|
|
180
|
+
} catch {
|
|
181
|
+
console.log(`\nOpen this URL in your browser to log in:\n${authUrl.toString()}\n`);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Get an authenticated Agent for the currently logged-in user.
|
|
189
|
+
* Restores the OAuth session and returns an Agent that auto-refreshes tokens.
|
|
190
|
+
*/
|
|
191
|
+
export async function getAgent(): Promise<{ agent: Agent; did: string; handle: string } | null> {
|
|
192
|
+
const authInfo = loadAuthInfo();
|
|
193
|
+
if (!authInfo) return null;
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
// We need to create a client to restore the session.
|
|
197
|
+
// Since we don't know the original port, use a dummy port — session
|
|
198
|
+
// restoration doesn't need the redirect_uri to match.
|
|
199
|
+
const client = createOAuthClient(0);
|
|
200
|
+
const session = await client.restore(authInfo.did);
|
|
201
|
+
const agent = new Agent(session);
|
|
202
|
+
return { agent, did: authInfo.did, handle: authInfo.handle };
|
|
203
|
+
} catch {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Require authentication — exit with error if not logged in.
|
|
210
|
+
*/
|
|
211
|
+
export async function requireAgent(): Promise<{ agent: Agent; did: string; handle: string }> {
|
|
212
|
+
const result = await getAgent();
|
|
213
|
+
if (!result) {
|
|
214
|
+
console.error("Not logged in. Run `sb login <handle>` first.");
|
|
215
|
+
process.exit(1);
|
|
216
|
+
}
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function logout(): void {
|
|
221
|
+
const authInfo = loadAuthInfo();
|
|
222
|
+
if (authInfo) {
|
|
223
|
+
deleteSessionFile(authInfo.did);
|
|
224
|
+
}
|
|
225
|
+
clearAuthInfo();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function resolveDIDDocument(did: string): Promise<unknown | null> {
|
|
229
|
+
try {
|
|
230
|
+
if (did.startsWith("did:plc:")) {
|
|
231
|
+
const res = await fetch(`https://plc.directory/${did}`);
|
|
232
|
+
if (!res.ok) return null;
|
|
233
|
+
return await res.json();
|
|
234
|
+
} else if (did.startsWith("did:web:")) {
|
|
235
|
+
const host = did.slice("did:web:".length).replaceAll(":", "/");
|
|
236
|
+
const res = await fetch(`https://${host}/.well-known/did.json`);
|
|
237
|
+
if (!res.ok) return null;
|
|
238
|
+
return await res.json();
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
} catch {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { MaterializedTask } from "./types.js";
|
|
2
|
+
|
|
3
|
+
const MIN_PREFIX_LEN = 4;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Resolve a card reference (TID rkey prefix) to a unique task.
|
|
7
|
+
* Returns the matching task, or throws with a helpful message if
|
|
8
|
+
* ambiguous or not found.
|
|
9
|
+
*/
|
|
10
|
+
export function resolveCardRef(
|
|
11
|
+
ref: string,
|
|
12
|
+
tasks: MaterializedTask[],
|
|
13
|
+
): MaterializedTask {
|
|
14
|
+
if (ref.length < MIN_PREFIX_LEN) {
|
|
15
|
+
throw new Error(`Card reference too short (min ${MIN_PREFIX_LEN} chars): ${ref}`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const matches = tasks.filter((t) => t.rkey.startsWith(ref));
|
|
19
|
+
|
|
20
|
+
if (matches.length === 0) {
|
|
21
|
+
throw new Error(`No card found matching "${ref}"`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (matches.length > 1) {
|
|
25
|
+
const list = matches
|
|
26
|
+
.map((t) => ` ${t.rkey.slice(0, 7)} ${t.effectiveTitle}`)
|
|
27
|
+
.join("\n");
|
|
28
|
+
throw new Error(`Ambiguous reference "${ref}". Matches:\n${list}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return matches[0];
|
|
32
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Column } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Match a column reference (name, prefix, or 1-based index) to a column.
|
|
5
|
+
* Throws with a helpful message if no match found.
|
|
6
|
+
*/
|
|
7
|
+
export function resolveColumn(
|
|
8
|
+
ref: string,
|
|
9
|
+
columns: Column[],
|
|
10
|
+
): Column {
|
|
11
|
+
const sorted = [...columns].sort((a, b) => a.order - b.order);
|
|
12
|
+
|
|
13
|
+
// Try numeric index (1-based)
|
|
14
|
+
const idx = parseInt(ref, 10);
|
|
15
|
+
if (!isNaN(idx) && idx >= 1 && idx <= sorted.length) {
|
|
16
|
+
return sorted[idx - 1];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Try exact match (case-insensitive)
|
|
20
|
+
const exact = sorted.find((c) => c.name.toLowerCase() === ref.toLowerCase());
|
|
21
|
+
if (exact) return exact;
|
|
22
|
+
|
|
23
|
+
// Try prefix match
|
|
24
|
+
const prefixMatches = sorted.filter((c) =>
|
|
25
|
+
c.name.toLowerCase().startsWith(ref.toLowerCase()),
|
|
26
|
+
);
|
|
27
|
+
if (prefixMatches.length === 1) return prefixMatches[0];
|
|
28
|
+
if (prefixMatches.length > 1) {
|
|
29
|
+
const list = prefixMatches.map((c, i) => ` ${i + 1}. ${c.name}`).join("\n");
|
|
30
|
+
throw new Error(`Ambiguous column "${ref}". Matches:\n${list}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Try substring match
|
|
34
|
+
const subMatches = sorted.filter((c) =>
|
|
35
|
+
c.name.toLowerCase().includes(ref.toLowerCase()),
|
|
36
|
+
);
|
|
37
|
+
if (subMatches.length === 1) return subMatches[0];
|
|
38
|
+
if (subMatches.length > 1) {
|
|
39
|
+
const list = subMatches.map((c, i) => ` ${i + 1}. ${c.name}`).join("\n");
|
|
40
|
+
throw new Error(`Ambiguous column "${ref}". Matches:\n${list}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const allCols = sorted.map((c, i) => ` ${i + 1}. ${c.name}`).join("\n");
|
|
44
|
+
throw new Error(`No column matching "${ref}". Available columns:\n${allCols}`);
|
|
45
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync, unlinkSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
|
|
5
|
+
export interface BoardRef {
|
|
6
|
+
did: string;
|
|
7
|
+
rkey: string;
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Config {
|
|
12
|
+
defaultBoard?: BoardRef;
|
|
13
|
+
knownBoards: BoardRef[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const CONFIG_DIR = join(homedir(), ".config", "skyboard");
|
|
17
|
+
const CONFIG_PATH = join(CONFIG_DIR, "config.json");
|
|
18
|
+
const AUTH_DIR = join(CONFIG_DIR, "auth");
|
|
19
|
+
const STATE_DIR = join(CONFIG_DIR, "state");
|
|
20
|
+
|
|
21
|
+
export function getConfigDir(): string {
|
|
22
|
+
return CONFIG_DIR;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getAuthDir(): string {
|
|
26
|
+
return AUTH_DIR;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getStateDir(): string {
|
|
30
|
+
return STATE_DIR;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function ensureConfigDir(): void {
|
|
34
|
+
mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function ensureAuthDir(): void {
|
|
38
|
+
ensureConfigDir();
|
|
39
|
+
mkdirSync(AUTH_DIR, { recursive: true, mode: 0o700 });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function ensureStateDir(): void {
|
|
43
|
+
ensureConfigDir();
|
|
44
|
+
mkdirSync(STATE_DIR, { recursive: true, mode: 0o700 });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function loadConfig(): Config {
|
|
48
|
+
ensureConfigDir();
|
|
49
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
50
|
+
return { knownBoards: [] };
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const raw = readFileSync(CONFIG_PATH, "utf-8");
|
|
54
|
+
return JSON.parse(raw) as Config;
|
|
55
|
+
} catch {
|
|
56
|
+
return { knownBoards: [] };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function saveConfig(config: Config): void {
|
|
61
|
+
ensureConfigDir();
|
|
62
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), {
|
|
63
|
+
mode: 0o600,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function setDefaultBoard(board: BoardRef): void {
|
|
68
|
+
const config = loadConfig();
|
|
69
|
+
config.defaultBoard = board;
|
|
70
|
+
if (!config.knownBoards.some((b) => b.did === board.did && b.rkey === board.rkey)) {
|
|
71
|
+
config.knownBoards.push(board);
|
|
72
|
+
}
|
|
73
|
+
saveConfig(config);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function getDefaultBoard(): BoardRef | undefined {
|
|
77
|
+
return loadConfig().defaultBoard;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function clearDefaultBoard(): void {
|
|
81
|
+
const config = loadConfig();
|
|
82
|
+
delete config.defaultBoard;
|
|
83
|
+
saveConfig(config);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function addKnownBoard(board: BoardRef): void {
|
|
87
|
+
const config = loadConfig();
|
|
88
|
+
if (!config.knownBoards.some((b) => b.did === board.did && b.rkey === board.rkey)) {
|
|
89
|
+
config.knownBoards.push(board);
|
|
90
|
+
}
|
|
91
|
+
saveConfig(config);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// File-based state store for OAuth CSRF tokens
|
|
95
|
+
export function writeStateFile(key: string, data: unknown): void {
|
|
96
|
+
ensureStateDir();
|
|
97
|
+
writeFileSync(join(STATE_DIR, `${key}.json`), JSON.stringify(data), {
|
|
98
|
+
mode: 0o600,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function readStateFile(key: string): unknown | undefined {
|
|
103
|
+
const path = join(STATE_DIR, `${key}.json`);
|
|
104
|
+
if (!existsSync(path)) return undefined;
|
|
105
|
+
try {
|
|
106
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
107
|
+
} catch {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function deleteStateFile(key: string): void {
|
|
113
|
+
const path = join(STATE_DIR, `${key}.json`);
|
|
114
|
+
try {
|
|
115
|
+
unlinkSync(path);
|
|
116
|
+
} catch {
|
|
117
|
+
// ignore
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// File-based session store for OAuth sessions
|
|
122
|
+
export function writeSessionFile(sub: string, data: unknown): void {
|
|
123
|
+
ensureAuthDir();
|
|
124
|
+
const filename = Buffer.from(sub).toString("base64url");
|
|
125
|
+
writeFileSync(join(AUTH_DIR, `${filename}.json`), JSON.stringify(data), {
|
|
126
|
+
mode: 0o600,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function readSessionFile(sub: string): unknown | undefined {
|
|
131
|
+
const filename = Buffer.from(sub).toString("base64url");
|
|
132
|
+
const path = join(AUTH_DIR, `${filename}.json`);
|
|
133
|
+
if (!existsSync(path)) return undefined;
|
|
134
|
+
try {
|
|
135
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
136
|
+
} catch {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function deleteSessionFile(sub: string): void {
|
|
142
|
+
const filename = Buffer.from(sub).toString("base64url");
|
|
143
|
+
const path = join(AUTH_DIR, `${filename}.json`);
|
|
144
|
+
try {
|
|
145
|
+
unlinkSync(path);
|
|
146
|
+
} catch {
|
|
147
|
+
// ignore
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Simple auth info storage (which DID is logged in)
|
|
152
|
+
const AUTH_INFO_PATH = join(CONFIG_DIR, "session.json");
|
|
153
|
+
|
|
154
|
+
export interface AuthInfo {
|
|
155
|
+
did: string;
|
|
156
|
+
handle: string;
|
|
157
|
+
service: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function loadAuthInfo(): AuthInfo | null {
|
|
161
|
+
if (!existsSync(AUTH_INFO_PATH)) return null;
|
|
162
|
+
try {
|
|
163
|
+
return JSON.parse(readFileSync(AUTH_INFO_PATH, "utf-8")) as AuthInfo;
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function saveAuthInfo(info: AuthInfo): void {
|
|
170
|
+
ensureConfigDir();
|
|
171
|
+
writeFileSync(AUTH_INFO_PATH, JSON.stringify(info, null, 2), {
|
|
172
|
+
mode: 0o600,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function clearAuthInfo(): void {
|
|
177
|
+
try {
|
|
178
|
+
unlinkSync(AUTH_INFO_PATH);
|
|
179
|
+
} catch {
|
|
180
|
+
// ignore
|
|
181
|
+
}
|
|
182
|
+
}
|