WIP: A simple cli for daily tangled use cases and AI integration. This is for my personal use right now, but happy if others get mileage from it! :)
1import { execSync } from 'node:child_process';
2import { Command } from 'commander';
3import { getCurrentSessionMetadata } from '../lib/session.js';
4
5/**
6 * Create the ssh-key command with subcommands for managing SSH keys
7 */
8export function createSshKeyCommand(): Command {
9 const sshKey = new Command('ssh-key');
10 sshKey.description('Verify SSH key setup for Git authentication');
11
12 // Verify command
13 sshKey
14 .command('verify')
15 .description('Verify SSH key authentication with git@tangled.org')
16 .action(async () => {
17 try {
18 console.log('Testing SSH connection to git@tangled.org...\n');
19
20 // Execute ssh -T git@tangled.org to test authentication
21 let output: string;
22 try {
23 output = execSync('ssh -T git@tangled.org', {
24 encoding: 'utf-8',
25 stdio: 'pipe',
26 });
27 } catch (error) {
28 // ssh -T returns non-zero exit code even on success
29 // Capture stderr which contains the authentication message
30 if (error instanceof Error && 'stderr' in error) {
31 output = (error as { stderr: string }).stderr;
32 } else {
33 throw error;
34 }
35 }
36
37 // Parse the DID from the output
38 // Expected format: "Hi @did:plc:...! You've successfully authenticated."
39 const didMatch = output.match(/@(did:plc:[a-z0-9]+)/i);
40
41 if (!didMatch) {
42 console.error('✗ SSH authentication failed');
43 console.error('Could not find authenticated DID in response');
44 console.error('\nPlease ensure you have:');
45 console.error('1. Generated an SSH key (ssh-keygen -t ed25519)');
46 console.error('2. Added your public key to https://tangled.org/settings/keys');
47 console.error('3. Your SSH agent is running (ssh-add -l)');
48 process.exit(1);
49 }
50
51 const did = didMatch[1];
52 console.log('✓ SSH authentication successful');
53 console.log(` Authenticated as: ${did}`);
54
55 // Check if this matches the logged-in user
56 const session = await getCurrentSessionMetadata();
57 if (session && session.did === did) {
58 console.log(` Handle: @${session.handle}`);
59 }
60
61 console.log('\n✓ Your SSH setup is working correctly!');
62 } catch (error) {
63 console.error(
64 `\n✗ Failed to verify SSH setup: ${error instanceof Error ? error.message : 'Unknown error'}`
65 );
66 console.error('\nPlease ensure you have:');
67 console.error('1. Generated an SSH key (ssh-keygen -t ed25519)');
68 console.error('2. Added your public key to https://tangled.org/settings/keys');
69 console.error('3. Your SSH agent is running (ssh-add -l)');
70 process.exit(1);
71 }
72 });
73
74 return sshKey;
75}