This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
itemsapi-collections.js
108 lines (88 loc) · 2.52 KB
/
itemsapi-collections.js
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
#!/usr/bin/env node
var program = require('commander');
var service = require('./src/collections')
var colors = require('colors');
var Table = require('cli-table');
var _ = require('lodash')
program
.option('-a, --api [api]', 'ItemsAPI URL i.e. http://localhost:4000/api/v1')
//.usage('test')
program
.command('get')
.description('Get collection')
.option('-c, --collection [collection]', 'Collection name')
.action(function(options){
if (!program.api) {
console.log(`The --api parameter is required`.red)
process.exit()
}
if (!options.collection) {
console.log(`The --collection parameter is required`.red);
process.exit()
}
options.api = program.api
service.get(options)
.then(function(res) {
console.log(JSON.stringify(res, null, 2))
})
.catch(function(err) {
console.log(err)
console.log('Unexpected error..'.red)
})
})
program
.command('reindex')
.description('Reindex collection')
.option('-c, --collection [collection]', 'Collection name')
.option('--new_index [new_index]', 'New index name')
.option('--new_type [new_type]', 'New type name')
.action(function(options){
if (!program.api) {
console.log(`The --api parameter is required`.red)
process.exit()
}
if (!options.collection) {
console.log(`The --collection parameter is required`.red);
process.exit()
}
options.api = program.api
console.log('Started reindexing.. Please wait..');
service.reindex(options)
.then(function(res) {
console.log('Same values: name: %s. New values: index: %s, type: %s', res.name, res.index, res.type);
console.log('Reindexing finished');
})
.catch(function(err) {
console.log(err)
console.log('Unexpected error..'.red)
})
})
program
.command('list')
.description('List all collections')
.action(function(options){
if (!program.api) {
console.log(`The --api parameter is required`.red)
process.exit()
}
options.api = program.api
service.list(options)
.then(function(res) {
var table = new Table({
head: ['Name', 'Url']
})
var rows = _.map(res, function(val) {
table.push([val.name, program.api + '/items/' + val.name])
//return [val.name, 'fff']
})
console.log(table.toString());
})
.catch(function(err) {
console.log(err)
console.log('Unexpected error..'.red)
})
})
if (process.argv.length < 3) {
program.outputHelp()
}
program.parse(process.argv);