Skip to content

Commit a681ab2

Browse files
committed
merge both commands
1 parent a09aa47 commit a681ab2

1 file changed

Lines changed: 21 additions & 31 deletions

File tree

src/index.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,41 @@
11
#!/usr/bin/env node
2+
/* eslint-disable max-params */
23
import * as fs from 'fs'
3-
import { program } from 'commander'
4+
import { Argument, program } from 'commander'
45
import { SSHAgentClient } from './lib/ssh_agent_client.js'
56

6-
program.name('ssh-crypt').description('Encryption through SSH Agent')
7-
87
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']))
1111
.argument('<source>', 'file to encrypt')
1212
.argument('[destination]', 'output path (default to stdout)')
1313
.requiredOption('-k, --key <string>', 'select the first matching pubkey in the ssh-agent')
1414
.requiredOption('-s, --seed <string>', 'is used to generate the secret')
15-
.action(async (source, destination, options) => {
15+
.action(async (action, source, destination, options) => {
1616
const agent = new SSHAgentClient()
1717
const key = await agent.getIdentity(options.key)
1818
if (!key) {
1919
throw new Error(`No SSH key found for "${options.key}"!`)
2020
}
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+
}
4234
}
43-
const data = fs.readFileSync(source, { encoding: 'ascii' })
44-
const output = await agent.decrypt(key, options.seed, data)
4535
if (destination) {
46-
fs.writeFileSync(destination, output)
36+
fs.writeFileSync(destination, await output())
4737
} else {
48-
process.stdout.write(output)
38+
process.stdout.write(await output())
4939
}
5040
})
5141

0 commit comments

Comments
 (0)