diff --git a/app.ts b/app.ts index 1129338..ce9b10c 100644 --- a/app.ts +++ b/app.ts @@ -89,7 +89,8 @@ async function main() { console.log("Logout successful! Credentials removed."); }) .command("list, ls", "List all OAuth2 applications") - .action(async () => { + .option("--pretty, -p", "Pretty print the output") + .action(async (opt) => { const credentials = await credentialManager.getCredentials(); if (!credentials) return; @@ -102,14 +103,29 @@ async function main() { console.log("No OAuth2 applications found."); return; } - - console.log("OAuth2 applications:"); - console.log(apps.map(prettyOauth2Application).join("\n\n")); + if (opt.pretty) { + console.log("OAuth2 applications:"); + console.log(apps.map(prettyOauth2Application).join("\n\n")); + return; + } + else { + const maxNameLength = Math.max(...apps.map(app => app.name.length), 6); + console.log(apps.map(app => [ + app.id.toString().padEnd(5), + app.confidential_client ? "c" : "o", + app.name.padStart(maxNameLength), + app.client_id, + app.redirect_uris.join(", "), + ].join(" ").trim()).join("\n")); + } }) .command("create", "Create new OAuth2 application") .action(async () => { const credentials = await credentialManager.getCredentials(); - if (!credentials) return; + if (!credentials) { + console.error("Credentials not found. Please login first."); + return; + } // Create OAuth2Api with token const oauth2Api = new OAuth2Api(URL_BASE, credentials.token); @@ -142,33 +158,28 @@ async function main() { await saveSecretKeys(path, app.client_id, app.client_secret); } }) - .command("delete", "Delete OAuth2 application") - .action(async () => { + .command("remove, rm", "Remove OAuth2 application") + .arguments("") + .action(async (_opt, appId) => { const credentials = await credentialManager.getCredentials(); - if (!credentials) return; - + if (!credentials) { + console.error("Credentials not found. Please login first."); + return; + } + // Create OAuth2Api with token const oauth2Api = new OAuth2Api(URL_BASE, credentials.token); - - const id = await prompt("Enter the ID of the application to delete: "); - if (!id) { - console.error("ID is required."); - return; - } - - const appId = parseInt(id); - if (isNaN(appId)) { - console.error("ID must be a number."); - return; - } - + await oauth2Api.deleteOauth2Application(appId); console.log("OAuth2 application deleted successfully!"); }) .command("update", "Update OAuth2 application") .action(async () => { const credentials = await credentialManager.getCredentials(); - if (!credentials) return; + if (!credentials) { + console.error("Credentials not found. Please login first."); + return; + } // Create OAuth2Api with token const oauth2Api = new OAuth2Api(URL_BASE, credentials.token);