From af1bf00a8ee229f6bd9ac0f9108d055ad9921dad Mon Sep 17 00:00:00 2001 From: x-ray Date: Tue, 14 May 2024 12:41:58 +0800 Subject: [PATCH] feat: rename a otp name --- package.json | 2 +- readme.md | 11 +++++++---- source/cli.tsx | 33 +++++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index abd7f79..6af40cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "2fa-cmd", - "version": "0.0.3", + "version": "0.0.4", "description": "A command tool for Google Authenticator", "author": "x-ray-s", "email": "princesswar721@gmail.com", diff --git a/readme.md b/readme.md index 0c2a0b2..5180fd4 100644 --- a/readme.md +++ b/readme.md @@ -32,6 +32,7 @@ $ 2fa --help verify - verify a token import - Import a secret from url rename - Rename + get - get one otp Options --name @@ -41,11 +42,13 @@ $ 2fa --help Examples $ 2fa - $ 2fa add --name github --secret FCRJQZSGFD3VMZDE - $ 2fa remove --name github - $ 2fa verify --name github --token 643223 + $ 2fa add --name --secret FCRJQZSGFD3VMZDE + $ 2fa remove --name + $ 2fa verify --name --token 643223 $ 2fa import --url 'otpauth://totp/...' - $ 2fa rename --name + $ 2fa rename --name + $ 2fa get --name + $ 2fa --name // same as get ``` ## TODO diff --git a/source/cli.tsx b/source/cli.tsx index aae81db..41bb3b7 100644 --- a/source/cli.tsx +++ b/source/cli.tsx @@ -6,7 +6,7 @@ import App from './app.js'; import chalk from 'chalk'; import {decode} from './import.js'; import {add, remove, has, find, importGA, rename} from './storage.js'; -import {verify} from './2fa.js'; +import {verify, generate} from './2fa.js'; const cli = meow( ` Usage @@ -26,11 +26,13 @@ const cli = meow( Examples $ 2fa - $ 2fa add --name github --secret FCRJQZSGFD3VMZDE - $ 2fa remove --name github - $ 2fa verify --name github --token 643223 + $ 2fa add --name --secret FCRJQZSGFD3VMZDE + $ 2fa remove --name + $ 2fa verify --name --token 643223 $ 2fa import --url 'otpauth://totp/...' $ 2fa rename --name + $ 2fa find --name + $ 2fa --name `, { importMeta: import.meta, @@ -123,6 +125,29 @@ const setError = (msg: string) => { await rename(old, name); return process.exit(0); } + + if (cli.input.includes('get')) { + if (!name) { + setError('When get a OTP, name is required'); + return process.exit(1); + } + const item = await find(name); + if (!item) { + setError('This name is not found'); + return process.exit(1); + } + console.log(generate(item.secret).token); + return process.exit(0); + } + if (cli.input.length === 0 && name) { + const item = await find(name); + if (!item) { + setError('This name is not found'); + return process.exit(1); + } + console.log(generate(item.secret).token); + return process.exit(0); + } })(); const {clear} = render(, {