Skip to content

Commit 9fa372d

Browse files
committed
Use commander's command-specific options
1 parent 469531d commit 9fa372d

File tree

2 files changed

+78
-54
lines changed

2 files changed

+78
-54
lines changed

src/index.ts

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,73 @@ import { createKey } from "./command/create";
99
import { deleteKey } from "./command/delete";
1010
import { listKeys } from "./command/list";
1111
import { CLIError, CLIErrorType } from "./error";
12-
import { AccountType, Option } from "./types";
12+
import { AccountType, CreateOption, DeleteOption, ListOption } from "./types";
13+
14+
program.version("0.1.1");
1315

1416
program
15-
.version("0.1.1")
16-
.arguments("[action]")
17+
.command("list")
18+
.description("list keys")
1719
.option(
18-
"-t --account-type <accountType>",
20+
"-t, --account-type <accountType>",
1921
"'platform' or 'asset'. The type of the key"
2022
)
21-
.option("-p --passphrase <passphrase>", "passphrase")
22-
.option("-a --address <address>", "address")
23-
.action(main);
23+
.action(handleError(listCommand));
2424

25-
program.on("--help", () => {
26-
console.log(` Action:
25+
program
26+
.command("create")
27+
.description("create a new key")
28+
.option(
29+
"-t, --account-type <accountType>",
30+
"'platform' or 'asset'. The type of the key"
31+
)
32+
.option("-p, --passphrase <passphrase>", "passphrase")
33+
.action(handleError(createCommand));
34+
35+
program
36+
.command("delete")
37+
.description("delete the key")
38+
.option(
39+
"-t, --account-type <accountType>",
40+
"'platform' or 'asset'. The type of the key"
41+
)
42+
.option("-a, --address <address>", "address")
43+
.action(handleError(deleteCommand));
44+
45+
function handleError(
46+
f: (option: any) => Promise<void>
47+
): (option: any) => Promise<void> {
48+
return async (option: any) => {
49+
try {
50+
await f(option);
51+
} catch (err) {
52+
console.error(err.toString());
53+
process.exit(1);
54+
}
55+
};
56+
}
57+
58+
async function listCommand(option: ListOption) {
59+
const cckey = await CCKey.create();
60+
const accountType = parseAccountType(option.accountType);
61+
await listKeys(cckey, accountType);
62+
}
63+
64+
async function createCommand(option: CreateOption) {
65+
const cckey = await CCKey.create();
66+
const accountType = parseAccountType(option.accountType);
67+
const passphrase = parsePassphrase(option.passphrase);
68+
await createKey(cckey, accountType, passphrase);
69+
}
2770

28-
list : List all the saved addresses
29-
create : Create new key with passphrase
30-
delete : Delete the key of the given address
31-
`);
71+
async function deleteCommand(option: DeleteOption) {
72+
const cckey = await CCKey.create();
73+
const accountType = parseAccountType(option.accountType);
74+
const address = parseAddress(option.address);
75+
await deleteKey(cckey, accountType, address);
76+
}
3277

78+
program.on("--help", () => {
3379
console.log(` Examples:
3480
3581
cckey create -t platform --passphrase "my password"
@@ -40,41 +86,13 @@ program.on("--help", () => {
4086
`);
4187
});
4288

43-
async function main(action: string, option: Option) {
44-
if (!action) {
45-
program.outputHelp();
46-
process.exit(1);
47-
return;
48-
}
49-
const cckey = await CCKey.create();
50-
try {
51-
const accountType = getAccountType(option);
52-
53-
switch (action) {
54-
case "list":
55-
await listKeys(cckey, accountType);
56-
break;
57-
case "create":
58-
const passphrase = getPassphrase(option);
59-
await createKey(cckey, accountType, passphrase);
60-
break;
61-
case "delete":
62-
const address = getAddress(option);
63-
await deleteKey(cckey, accountType, address);
64-
break;
65-
default:
66-
throw new CLIError(CLIErrorType.InvalidAction);
67-
}
68-
} catch (err) {
69-
console.log(err.toString());
70-
process.exit(1);
71-
}
72-
}
73-
7489
program.parse(process.argv);
90+
if (program.args.length === 0) {
91+
program.outputHelp();
92+
process.exit(1);
93+
}
7594

76-
function getAccountType(option: Option): AccountType {
77-
const accountType = option.accountType;
95+
function parseAccountType(accountType: string): AccountType {
7896
if (_.isUndefined(accountType)) {
7997
throw new CLIError(CLIErrorType.OptionRequired, {
8098
optionName: "account-type"
@@ -86,8 +104,7 @@ function getAccountType(option: Option): AccountType {
86104
return accountType as AccountType;
87105
}
88106

89-
function getAddress(option: Option): string {
90-
const address = option.address;
107+
function parseAddress(address: string): string {
91108
if (_.isUndefined(address)) {
92109
throw new CLIError(CLIErrorType.OptionRequired, {
93110
optionName: "address"
@@ -97,8 +114,7 @@ function getAddress(option: Option): string {
97114
return address;
98115
}
99116

100-
function getPassphrase(option: Option): string {
101-
const passphrase = option.passphrase;
117+
function parsePassphrase(passphrase: string): string {
102118
if (_.isUndefined(passphrase)) {
103119
throw new CLIError(CLIErrorType.OptionRequired, {
104120
optionName: "passphrase"

src/types.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
export type AccountType = "platform" | "asset";
22
export type Action = "list" | "create" | "delete";
33

4-
export interface Option {
5-
accountType?: string;
6-
passphrase?: string;
7-
address?: string;
4+
export interface ListOption {
5+
accountType: string;
6+
}
7+
8+
export interface CreateOption {
9+
accountType: string;
10+
passphrase: string;
11+
}
12+
13+
export interface DeleteOption {
14+
accountType: string;
15+
address: string;
816
}

0 commit comments

Comments
 (0)