Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added file support to cli #79

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { Command } from 'commander';
import { readFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { Client } from '@crypt.fyi/core';
import chalk from 'chalk';
import ora from 'ora';
Expand All @@ -25,13 +25,24 @@ program
program
.command('encrypt')
.description('Encrypt and share a secret')
.argument('<content>', 'Content to encrypt')
.argument('[content]', 'Content to encrypt')
.option('-f, --file <file>', 'Path to file to encrypt')
.option('-p, --password <password>', 'Password to encrypt with')
.option('-t, --ttl <duration>', 'Time to live (e.g., 1h, 1d)', '1h')
.option('-b, --burn', 'Burn after reading', true)
.option('--ip <ip>', 'Restrict access to specific IP address')
.option('-r, --reads <count>', 'Number of times the secret can be read', undefined)
.action(async (content, options) => {

if (options.file && !content) {
try {
content = await readFileSync(options.file, 'utf-8');
} catch (error) {
console.log(chalk.red('Failed to read file'));
process.exit(1);
}
}

const spinner = ora('Encrypting content...').start();
try {
spinner.text = 'Creating vault...';
Expand Down Expand Up @@ -70,6 +81,7 @@ program
.command('decrypt')
.description('Decrypt a secret from the URL')
.argument('<url>', 'Secret URL')
.option('-o --output <file>', 'Output file to save the decrypted content')
.option('-p, --password <password>', 'Password (if secret is password protected)')
.action(async (urlString, options) => {
const spinner = ora('Fetching secret...').start();
Expand Down Expand Up @@ -98,13 +110,21 @@ program
console.log(chalk.blue('File name:'), parsed.name);
console.log(chalk.yellow('File content (base64):'), parsed.content);
} else {
console.log('\nDecrypted content:');
console.log(result.c);
if (options.output) {
writeIntoFile(result.c, options.output);
} else {
console.log('\nDecrypted content:');
console.log(result.c);
}
}
} catch {
// Not JSON, print as regular text
console.log('\nDecrypted content:');
console.log(result.c);
if (options.output) {
writeIntoFile(result.c, options.output);
} else {
console.log('\nDecrypted content:');
console.log(result.c);
}
}

if (result.burned) {
Expand Down Expand Up @@ -137,3 +157,13 @@ function parseDuration(duration: string): number {
const [, value, unit] = match;
return parseInt(value, 10) * units[unit];
}

function writeIntoFile(content: string, path: string) {
try {
writeFileSync(path, content);
console.log(chalk.green(`\nContent saved to ${path}`));
} catch (error) {
console.log(chalk.red('Failed to save content'));
process.exit(1);
}
}