-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·101 lines (88 loc) · 3.05 KB
/
Copy pathcli.js
File metadata and controls
executable file
·101 lines (88 loc) · 3.05 KB
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
#!/usr/bin/env node
import c from 'chalk'
import program from 'commander'
import util from 'util'
import helper from './dgraph_helper.js'
import utilities from './utils/cli_utils.js'
const host_option = [
'-H, --host <address>', 'The host address of your Dgraph DB',
]
const path_option = [
'-P, --path <schema_path>', 'The path to your schema.',
]
const print_diff = differences => {
const { conflicts, added } = differences
if (conflicts.length) {
console.log(c.redBright('Can\'t alter schema, there are conflicts.'))
conflicts.forEach(element => {
console.log(element)
})
} else if (added.length) {
console.log(c.greenBright('New changes only.'))
added.forEach(element => {
console.log(element)
})
} else
console.log(c.greenBright('Same schema, no alteration required.'))
}
program
.version('1.0.0')
.description('A CLI to manage your Dgraph schemas with ease.')
program
.command('get_schema')
.description('Get the current Dgraph schema.')
.option(host_option[0], host_option[1])
.action(async cmd => {
const host = cmd.host ? cmd.host : process.env.DB_URL
const client = utilities.create_client(host)
const fetched_schema = await helper.get_schema(client)
console.log(util.inspect(fetched_schema, false, 'undefined', true))
})
program
.command('get_diff')
.description('Prints the differences between the new & current schema.')
.option(host_option[0], host_option[1])
.option(path_option[0], path_option[1])
.action(async cmd => {
const host = cmd.host ? cmd.host : process.env.DB_URL
const schema_file = await utilities.get_schema_from_path(cmd.path)
if (!schema_file) {
console.log(c.bgRedBright('Schema file does not exists !'))
process.exit(1)
}
const client = utilities.create_client(host)
const differences = await helper.diff_checker(
client,
schema_file,
)
print_diff(differences)
})
program
.command('alter')
.description('Alter your Dgraph schema if there are no conflicts')
.option(host_option[0], host_option[1])
.option(path_option[0], path_option[1])
.option('-F, --force', 'Forcing schema alteration.')
.action(async cmd => {
const host = cmd.host ? cmd.host : process.env.DB_URL
const schema_file = await utilities.get_schema_from_path(cmd.path)
if (!schema_file) {
console.log(c.bgRedBright('Schema file does not exists !'))
process.exit(1)
}
const force_flag = !!cmd.force
const client = utilities.create_client(host)
const differences = await helper.diff_checker(
client,
schema_file,
)
print_diff(differences)
if (force_flag) {
console.log(c.bgRedBright(' Forcing schema alteration. '))
await helper.alter_schema(client, schema_file)
} else if (differences.added.length > 0) {
await helper.alter_schema(client, schema_file)
console.log(c.greenBright('Schema altered.'))
}
})
program.parseAsync(process.argv)