|
1 | 1 | #!/usr/bin/env node |
| 2 | +/* eslint-disable max-params */ |
2 | 3 | import * as fs from 'fs' |
3 | | -import { program } from 'commander' |
| 4 | +import { Argument, program } from 'commander' |
4 | 5 | import { SSHAgentClient } from './lib/ssh_agent_client.js' |
5 | 6 |
|
6 | | -program.name('ssh-crypt').description('Encryption through SSH Agent') |
7 | | - |
8 | 7 | program |
9 | | - .command('encrypt') |
10 | | - .description('Encrypt a file with your ssh-agent private key') |
| 8 | + .name('ssh-crypt') |
| 9 | + .description('Encrypt/Decrypt a file with your ssh-agent private key') |
| 10 | + .addArgument(new Argument('<command>').choices(['encrypt', 'decrypt'])) |
11 | 11 | .argument('<source>', 'file to encrypt') |
12 | 12 | .argument('[destination]', 'output path (default to stdout)') |
13 | 13 | .requiredOption('-k, --key <string>', 'select the first matching pubkey in the ssh-agent') |
14 | 14 | .requiredOption('-s, --seed <string>', 'is used to generate the secret') |
15 | | - .action(async (source, destination, options) => { |
| 15 | + .action(async (action, source, destination, options) => { |
16 | 16 | const agent = new SSHAgentClient() |
17 | 17 | const key = await agent.getIdentity(options.key) |
18 | 18 | if (!key) { |
19 | 19 | throw new Error(`No SSH key found for "${options.key}"!`) |
20 | 20 | } |
21 | | - const data = fs.readFileSync(source) |
22 | | - const output = await agent.encrypt(key, options.seed, data) |
23 | | - if (destination) { |
24 | | - fs.writeFileSync(destination, output) |
25 | | - } else { |
26 | | - process.stdout.write(output) |
27 | | - } |
28 | | - }) |
29 | | - |
30 | | -program |
31 | | - .command('decrypt') |
32 | | - .description('Decrypt a file with your ssh-agent private key') |
33 | | - .argument('<source>', 'file to decrypt') |
34 | | - .argument('[destination]', 'output path (default to stdout)') |
35 | | - .requiredOption('-k, --key <string>', 'select the first matching pubkey in the ssh-agent') |
36 | | - .requiredOption('-s, --seed <string>', 'is used to generate the secret') |
37 | | - .action(async (source, destination, options) => { |
38 | | - const agent = new SSHAgentClient() |
39 | | - const key = await agent.getIdentity(options.key) |
40 | | - if (!key) { |
41 | | - throw new Error(`No SSH key found for "${options.key}"!`) |
| 21 | + const output = async () => { |
| 22 | + switch (action) { |
| 23 | + case 'encrypt': { |
| 24 | + const data = fs.readFileSync(source) |
| 25 | + return agent.encrypt(key, options.seed, data).then(encrypted => Buffer.from(encrypted, 'ascii')) |
| 26 | + } |
| 27 | + case 'decrypt': { |
| 28 | + const data = fs.readFileSync(source, { encoding: 'ascii' }) |
| 29 | + return agent.decrypt(key, options.seed, data) |
| 30 | + } |
| 31 | + default: |
| 32 | + throw new Error('Unknwon action!') |
| 33 | + } |
42 | 34 | } |
43 | | - const data = fs.readFileSync(source, { encoding: 'ascii' }) |
44 | | - const output = await agent.decrypt(key, options.seed, data) |
45 | 35 | if (destination) { |
46 | | - fs.writeFileSync(destination, output) |
| 36 | + fs.writeFileSync(destination, await output()) |
47 | 37 | } else { |
48 | | - process.stdout.write(output) |
| 38 | + process.stdout.write(await output()) |
49 | 39 | } |
50 | 40 | }) |
51 | 41 |
|
|
0 commit comments