Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing all domains added (teardownAll) #329

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/middleware/confirmTeardownAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var helpers = require("./util/helpers.js");

module.exports = function(req, next) {
helpers.trunc(
"\nAll the above domains will be removed. Do you want to continue? (y/n) \n"
);
helpers.read(
{
silent: false,
default: "",
edit: true,
output: req.argv._[0],
input: req.argv._[0]
},
function(err, option) {
if (["y", "yes"].includes(option.toLowerCase())) {
next();
} else {
helpers.trunc("Aborted".yellow + " - Unable to remove.".grey);
process.exit(1);
}
}
);
};
62 changes: 62 additions & 0 deletions lib/middleware/teardownAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var request = require("request");
var url = require("url");
var helpers = require("./util/helpers");
var path = require("path");
var parseUrl = require("url-parse-as-address");

module.exports = function(req, next, abort) {
var options = {
url: url.resolve(req.endpoint.format(), "/list"),
method: "get",
auth: {
user: "token",
pass: req.creds.token,
sendImmediately: true
}
};

request(options, function(e, r, obj) {
if (e) throw e;
var list = JSON.parse(obj);
return list.map(item => {
remove(parseUrl(item.domain).host);
});
});

var remove = function(domain) {
var options = {
url: url.resolve(req.endpoint, domain),
method: "delete",
auth: {
user: "token",
pass: req.creds.token,
sendImmediately: true
}
};

request(options, function(e, r, obj) {
if (e) throw e;

if (r.statusCode == 200 || r.statusCode == 204 || r.statusCode == 210) {
helpers.space();
helpers.trunc(
"Success".green +
(" - " + domain.underline + " has been removed.").grey
);
helpers.space();
} else if (r.statusCode == 403) {
helpers.space();
helpers.trunc(
"Aborted".yellow +
(" - Unable to remove " + domain.underline + ".").grey
);
helpers.space();
} else {
helpers.space();
helpers.log(obj);
helpers.space();
process.exit();
}
});
};
};
27 changes: 24 additions & 3 deletions lib/surge.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var token = require("./middleware/token")
var teardown = require("./middleware/teardown")
var discovery = require("./middleware/discovery")
var plus = require("./middleware/plus")
var confirmTeardownAll = require("./middleware/confirmTeardownAll")
var teardownAll = require("./middleware/teardownAll")

var subscription= require("./middleware/subscription")
var plans = require("./middleware/plans")
Expand Down Expand Up @@ -60,9 +62,9 @@ var exitifcurrentplan = function(req, next){
}


var space = function(req, next){
var space = function(req, next){
helpers.space()
next()
next()
}

var parse = function(arg){
Expand Down Expand Up @@ -112,7 +114,7 @@ module.exports = function(config){
var argv = minimist(args, options)
var cmd = argv._[0]

var commands = ["login", "logout", "whoami", "list", "publish", "teardown", "token", "plus", "ssl", "plan", "card"]
var commands = ["login", "logout", "whoami", "list", "publish", "teardown", "token", "plus", "ssl", "plan", "card", "teardownAll"]

if (commands.indexOf(cmd) !== -1) {
argv._.shift()
Expand Down Expand Up @@ -345,6 +347,25 @@ module.exports = function(config){
}
}

surge.teardownAll = function(hooks) {
var hooks = hooks || {};
var preAuth = hooks.preAuth || stub;
var postAuth = hooks.postAuth || stub;
var onion = [
whitelist, endpoint, pkg, help, version, space,
preAuth, creds, auth, postAuth,
shorthand, list, confirmTeardownAll, teardownAll, space
]
return function() {
var argv = parse(arguments[arguments.length - 1]);
skin({
config: config,
argv: argv,
read: read
}, onion)
};
};

return surge

}