From d5f4abeeb0740fc7f1b19bcad065905d29afde4a Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Thu, 27 Jul 2023 09:13:44 +0700 Subject: [PATCH] Add list profiles --- CHANGELOG.md | 6 +++++- package.json | 2 +- src/cli.ts | 34 ++++++++++++++++++++++++---------- src/manage.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 src/manage.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 016fd48..c373ec4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -### 14/07/2023 (1.0.8) +### 27/07/2023 (1.0.10) + +- Add list profiles + +### 14/07/2023 (1.0.9) - Fixed the start with port unable to start diff --git a/package.json b/package.json index f1acd31..e543a98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cubetiq/hlt", - "version": "0.1.9", + "version": "0.1.10", "description": "A lightweight http tunnel client using nodejs and socket.io client", "main": "dist/cli.js", "bin": { diff --git a/src/cli.ts b/src/cli.ts index 78c5717..0ac4740 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,6 +4,7 @@ import * as os from "os"; import * as path from "path"; import { initConfigFileClient, startClient } from "./api"; import { PROFILE_DEFAULT, PROFILE_PATH, SERVER_DEFAULT_URL, TOKEN_FREE } from "./constant"; +import { listProfile } from "./manage"; import { createProxyServer } from "./proxy"; import { createProxyServer as createProxyTCPServer } from "./proxy_tcp"; import { getTokenFree } from './sdk'; @@ -22,20 +23,20 @@ program program .command("init") .description("generate a new client and token with free access") - .option("-s --server ", "setting server url", SERVER_DEFAULT_URL) + .option("-s, --server ", "setting server url", SERVER_DEFAULT_URL) .option( - "-t --token ", + "-t, --token ", "setting token (default generate FREE access token)", "" ) - .option("-a --access ", "setting token access type", TOKEN_FREE) - .option("-c --client ", "setting client (auto generate uuid)") + .option("-a, --access ", "setting token access type", TOKEN_FREE) + .option("-c, --client ", "setting client (auto generate uuid)") .option( - "-k --key ", + "-k, --key ", "setting client api key for authentication access" ) - .option("-p --profile ", "setting profile name", PROFILE_DEFAULT) - .option("-f --force", "force to generate new client and token", false) + .option("-p, --profile ", "setting profile name", PROFILE_DEFAULT) + .option("-f, --force", "force to generate new client and token", false) .action(async (options) => { initConfigFileClient(options); }); @@ -91,7 +92,7 @@ program ]) ) .argument("", "config value") - .option("-p --profile ", "setting profile name", PROFILE_DEFAULT) + .option("-p, --profile ", "setting profile name", PROFILE_DEFAULT) .action(async (type, value, options) => { if (!type) { console.error("type config is required!"); @@ -179,7 +180,7 @@ program "key", ]) ) - .option("-p --profile ", "setting profile name", PROFILE_DEFAULT) + .option("-p, --profile ", "setting profile name", PROFILE_DEFAULT) .action(async (type, options) => { if (!type) { console.error("type config is required!"); @@ -271,7 +272,7 @@ program throw new InvalidArgumentError("Target is not a url or host with port."); }) - .option("-p --profile ", "setting profile name for connect with hlt server (proxy with current local port)") + .option("-p, --profile ", "setting profile name for connect with hlt server (proxy with current local port)") .action((port, target, options) => { const isTcp = target.indexOf("tcp") === 0; if (isTcp) { @@ -322,4 +323,17 @@ const onConnectProxy = (port: number, options: any) => { } } +// profile +program + .command("profile") + .description("manage profile") + .option("-l, --list", "list all profiles", false) + .action((options) => { + if (options.list) { + listProfile(); + } else { + console.log("profile command is required"); + } + }); + program.parse(); diff --git a/src/manage.ts b/src/manage.ts new file mode 100644 index 0000000..e6b65c9 --- /dev/null +++ b/src/manage.ts @@ -0,0 +1,28 @@ +import * as fs from "fs"; +import * as os from "os"; +import * as path from "path"; +import { PROFILE_DEFAULT, PROFILE_PATH } from "./constant"; + +export const listProfile = () => { + const configDir = path.resolve(os.homedir(), PROFILE_PATH); + if (!fs.existsSync(configDir)) { + console.log(`config file ${configDir} not found`); + return; + } + + const configFiles = fs.readdirSync(configDir); + if (configFiles.length === 0) { + console.log(`config file ${configDir} not found`); + return; + } + + console.log("List of profile:"); + configFiles.forEach((file) => { + const configFilePath = path.resolve(configDir, file); + const config = JSON.parse(fs.readFileSync(configFilePath, "utf8")); + const name = file.replace(".json", ""); + console.log(`- ${name} (${config.clientId})`); + }); + + console.log(`\nCurrent profile: ${PROFILE_DEFAULT}`); +} \ No newline at end of file