forked from cybergis/cybergis-compute-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
94 lines (90 loc) · 2.68 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
import { Command } from "commander";
import { Git } from "./src/models/Git";
import DB from "./src/DB";
var pkg = require("../package.json");
var cmd = new Command();
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) => {
const db = new DB(false);
switch (operation) {
case "add":
var 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;
var connection = await db.connect();
var gitRepo = connection.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;
}
var connection = await db.connect();
var i = {};
if (cmd.address) i["address"] = cmd.address;
if (cmd.sha) i["sha"] = cmd.sha;
await connection
.createQueryBuilder()
.update(Git)
.where("id = :id", { id: cmd.id })
.set(i)
.execute();
console.log("git successfully updated:");
var gitRepo = connection.getRepository(Git);
console.log(await gitRepo.findOne(cmd.id));
break;
case "approve":
if (!cmd.id) {
console.error("-i, --id <id> flag is required");
return;
}
var connection = await db.connect();
await connection
.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;
}
var gitRepo = connection.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 db.close();
});
cmd.parse(process.argv);