-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudcms-delete.js
More file actions
executable file
·230 lines (196 loc) · 6.7 KB
/
cloudcms-delete.js
File metadata and controls
executable file
·230 lines (196 loc) · 6.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env node
/*jshint -W069 */
/*jshint -W104*/
var Gitana = require("gitana");
var async = require("async");
var request = require("request");
var cliArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage')
var fs = require("fs");
var util = require("./lib/util");
var Logger = require('basic-logger');
var log = new Logger({
showMillis: false,
showTimestamp: true
});
// debug only when using charles proxy ssl proxy when intercepting cloudcms api calls:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var options = handleOptions();
if (!options) {
return;
}
if (options["verbose"]) {
Logger.setLevel('debug', true);
} else {
Logger.setLevel('info', true);
}
var option_prompt = options["prompt"];
var option_useCredentialsFile = options["use-credentials-file"];
var option_gitanaFilePath = options["gitana-file-path"] || "./gitana.json";
var option_branchId = options["branch"] || "master";
var option_queryFilePath = options["query-file-path"];
var option_refresh = options["refresh"];
//
// load gitana.json config and override credentials
//
var gitanaConfig = JSON.parse("" + fs.readFileSync(option_gitanaFilePath));
if (option_useCredentialsFile) {
// override gitana.json credentials with username and password properties defined in the cloudcms-cli tool local db
var rootCredentials = JSON.parse("" + fs.readFileSync(path.join(util.homeDirectory(), ".cloudcms", "credentials.json")));
gitanaConfig.username = rootCredentials.username;
gitanaConfig.password = rootCredentials.password;
} else if (option_prompt) {
// override gitana.json credentials with username and password properties entered at command prompt
var prompt = require('prompt-sync')({
sigint: true
});
gitanaConfig.username = prompt('name: ');
gitanaConfig.password = prompt.hide('password: ');
} // else don't override credentials
var handled = false;
if (option_queryFilePath)
{
handled = true;
handleDelete();
}
if (!handled) {
printHelp(getOptions());
}
return;
//
// functions
//
function handleDelete() {
log.debug("handleDelete()");
util.getBranch(gitanaConfig, option_branchId, function(err, branch, platform, stack, domain, primaryDomain, project) {
if (err)
{
log.debug("Error connecting to Cloud CMS branch: " + err);
return;
}
log.info("connected to project: \"" + project.title + "\" and branch: \"" + (branch.title || branch._doc) + "\".");
var context = {
branchId: option_branchId,
branch: branch,
refresh: option_refresh,
queryFilePath: option_queryFilePath,
query: require(option_queryFilePath),
nodes: []
};
async.waterfall([
async.ensureAsync(async.apply(getNodesFromQuery, context)),
async.ensureAsync(refreshNodes),
async.ensureAsync(deleteNodes)
], function (err, context) {
if (err)
{
log.error("Error: " + err);
return;
}
log.info("Delete complete");
return;
});
});
}
function getNodesFromQuery(context, callback) {
log.info("getNodesFromQuery()");
var query = context.query;
query._fields = {};
context.branch.queryNodes(query,{
limit: -1
// }).each(function() {
// var node = this;
// util.enhanceNode(node);
// nodes.push(node);
}).then(function() {
context.nodes = this.asArray();
callback(null, context);
});
}
function refreshNodes(context, callback) {
log.info("refreshNodes()");
if (!context.refresh) {
return callback(null, context);
}
var nodes = context.nodes;
async.eachLimit(nodes, 5, function(node, cb) {
log.info("refreshing " + node._doc);
Chain(node).refresh().then(function() {
cb();
});
}, function (err) {
if(err)
{
log.error("Error: " + err);
callback(err);
return;
}
log.debug("done");
callback(null, context);
return;
});
}
function deleteNodes(context, callback) {
log.info("deleteNodes()");
var nodes = context.nodes;
async.eachLimit(nodes, 5, function(node, cb) {
log.info("deleting " + node._doc);
Chain(node).del().then(function() {
cb();
});
}, function (err) {
if(err)
{
log.error("Error: " + err);
callback(err);
return;
}
log.debug("done");
callback(null, context);
return;
});
}
function getOptions() {
return [
{name: 'help', alias: 'h', type: Boolean},
{name: 'verbose', alias: 'v', type: Boolean, description: 'verbose logging'},
{name: 'prompt', alias: 'p', type: Boolean, description: 'prompt for username and password. overrides gitana.json credentials'},
{name: 'use-credentials-file', alias: 'c', type: Boolean, description: 'use credentials file ~/.cloudcms/credentials.json. overrides gitana.json credentials'},
{name: 'refresh', alias: 'r', type: Boolean, description: 'perform a refresh api call on each node before deleting. this can cleanup any bad associations which would prevent the delete'},
{name: 'gitana-file-path', alias: 'g', type: String, description: 'path to gitana.json file to use when connecting. defaults to ./gitana.json'},
{name: 'branch', alias: 'b', type: String, description: 'branch id (not branch name!) to write content to. branch id or "master". Default is "master"'},
{name: 'query-file-path', alias: 'y', type: String, description: 'path to a json file defining the query'}
];
}
function handleOptions() {
var options = cliArgs(getOptions());
if (options.help)
{
printHelp(getOptions());
return null;
}
return options;
}
function printHelp(optionsList) {
console.log(commandLineUsage([
{
header: 'Cloud CMS Bulk Node Delete',
content: 'Delete nodes from a Cloud CMS project branch.'
},
{
header: 'Options',
optionList: optionsList
},
{
header: 'Examples',
content: [
{
desc: '1. bulk delete of nodes matched by query',
},
{
desc: 'npx cloudcms-util delete --query-file-path ./my-query.json'
}
]
}
]));
}