forked from cybergis/cybergis-compute-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
120 lines (96 loc) · 2.67 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Command } from "commander";
import { Git } from "./src/models/Git";
import dataSource from "./src/utils/DB";
const pkg: {version: string} = require("../package.json"); // eslint-disable-line
const cmd = new Command();
interface CommandOptions {
id?: string;
address?: string;
sha?: string;
}
cmd.version(pkg.version);
cmd
.command("git <operation>")
.option(
"-i, --id <id>",
"[operation=add/update/delete/approve] git repository's id"
)
.option(
"-a, --address <address>",
"[operation=add/update] git repository's address"
)
.option("-s, --sha <sha>", "[operation=add/update] git repository's sha hash")
.action(async (operation: string, cmd: CommandOptions) => {
switch (operation) {
case "add": {
const git = new Git();
if (cmd.address && cmd.id) {
git.address = cmd.address;
git.id = cmd.id;
} else {
console.error(
"-a, --address <address> and -i, --id <id> flags is required"
);
return;
}
git.isApproved = true;
if (cmd.sha) git.sha = cmd.sha;
const gitRepo = dataSource.getRepository(Git);
await gitRepo.save(git);
console.log("git successfully added:");
console.log(git);
break;
}
case "update": {
if (!cmd.id) {
console.error("-i, --id <id> flag is required");
return;
}
const i: { address?: string, sha?: string } = {};
if (cmd.address) i.address = cmd.address;
if (cmd.sha) i.sha = cmd.sha;
await dataSource
.createQueryBuilder()
.update(Git)
.where("id = :id", { id: cmd.id })
.set(i)
.execute();
console.log("git successfully updated:");
const gitRepo = dataSource.getRepository(Git);
console.log(await gitRepo.findOneBy({ id: cmd.id }));
break;
}
case "approve": {
if (!cmd.id) {
console.error("-i, --id <id> flag is required");
return;
}
await dataSource
.createQueryBuilder()
.update(Git)
.where("id = :id", { id: cmd.id })
.set({ isApproved: true })
.execute();
console.log("git approved");
break;
}
case "delete": {
if (!cmd.id) {
console.error("-i, --id <id> flag is required");
return;
}
const gitRepo = dataSource.getRepository(Git);
await gitRepo.delete(cmd.id);
console.log("git successfully deleted");
break;
}
default: {
console.error(
"<operation> invalid operation, only support [add/update/delete/approve]"
);
break;
}
}
await dataSource.destroy();
});
cmd.parse(process.argv);