Skip to content

Commit

Permalink
1st commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Oct 5, 2018
0 parents commit 7d4d033
Show file tree
Hide file tree
Showing 16 changed files with 2,237 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "mklabs",

"rules": {
"no-unused-vars": ["error", { "argsIgnorePattern": "env" }],
"no-else-return": "off",
"consistent-return": "off"
}
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
46 changes: 46 additions & 0 deletions bin/now-completions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node
process.env.DEBUG = process.env.DEBUG || 'now-completions*';

const tabtab = require('tabtab');
const opts = require('minimist')(process.argv.slice(2), {
boolean: ['help', 'version']
});

const completion = require('../lib/completion');

const args = opts._;
const run = async () => {
const cmd = args[0];

if (opts.help || opts.h) {
return console.log(`
now-completions [options] <command>
Commands:
install-completions | install Enables the completion on user system
`);
}

// Write your CLI there

// Here we install for the program `tabtab-test` (this file), with
// completer being the same program. Sometimes, you want to complete
// another program that's where the `completer` option might come handy.
if (cmd === 'install-completion' || cmd === 'install') {
await tabtab.install({
name: 'now',
completer: 'now-completions'
});
return;
}

// The completion command is added automatically by tabtab when the program
// is completed.
if (cmd === 'completion') {
const env = tabtab.parseEnv(process.env);
return completion(env);
}
};

run();
10 changes: 10 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const request = require('./request');

const deployments = async () => {
const { body } = await request('/now/deployments');
return body.deployments;
};

module.exports = {
deployments
};
19 changes: 19 additions & 0 deletions lib/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const untilidfy = require('untildify');

/* eslint-disable import/no-dynamic-require, global-require, consistent-return, no-console */
const auth = () => {
try {
const authJSON = require(untilidfy('~/.now/auth.json'));
return authJSON.credentials[0].token;
} catch (err) {
console.error(
new Error(`
Cannot load token from now auth.json file.
Make sure to run "now login" once before using this completion.
`)
);
}
};

module.exports = auth;
52 changes: 52 additions & 0 deletions lib/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const commands = [
{ name: 'deploy', description: 'Performs a deployment (default)' },
{ name: 'help', description: 'Displays complete help for [cmd]' },
{ name: 'list', description: 'Lists deployments' },
{ name: 'remove', description: 'Removes a deployment' },
{ name: 'alias', description: 'Configures aliases for deployments' },
{ name: 'domains', description: 'Manages your domain names' },
{ name: 'certs', description: 'Manages your SSL certificates' },
{ name: 'dns', description: 'Manages your DNS records' },
{ name: 'secrets', description: 'Manages your secret environment variables' },
{
name: 'billing',
description: 'Manages your credit carts and billing methods'
},
{ name: 'upgrade', description: 'Upgrades or downgrades your plan' },
{ name: 'teams', description: 'Manages your teams' },
{ name: 'logs', description: 'Displays the logs for a deployment' },
{ name: 'scale', description: 'Scales the instance count of a deployment' },
{ name: 'login', description: 'Logs into your account or creates a new one' },
{ name: 'logout', description: 'Logs out of your account' },
{ name: 'whoami', description: 'Displays the currently logged in username' },
{
name: 'inspect',
description: 'Displays information related to a deployment'
}
];

const aliases = {
list: ['ls'],
remove: ['rm'],
alias: ['ln', 'aliases'],
domains: ['domain'],
certs: ['cert'],
secrets: ['secret'],
billing: ['cc'],
upgrade: ['downgrade'],
teams: ['team', 'switch'],
logs: ['log']
};

// Add aliases to available sub commands
/* eslint-disable guard-for-in */
for (const alias in aliases) {
const items = aliases[alias];

for (const item of items) {
const { description } = commands.find(cmd => cmd.name === alias);
commands.push({ name: item, description });
}
}

module.exports = commands;
21 changes: 21 additions & 0 deletions lib/completion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const tabtab = require('tabtab');

const commands = require('./commands');
const options = require('./options');
const subcommands = require('./subcommands');

const completion = async env => {
/* eslint-disable consistent-return */
if (!env.complete) return;

const subcmd = subcommands[env.prev];

if (!subcmd) {
return tabtab.log([...commands, ...options]);
}

// We have a completion handler for this previous word, simply execute
await subcmd(env);
};

module.exports = completion;
39 changes: 39 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const options = [
'-h',
'--help',
'-v',
'--version',
'-n',
'--name',
'-A',
'--local-config',
'-Q',
'--global-config',
'-d',
'--debug',
'-f',
'--force',
'-t',
'--token',
'-l',
'--links',
'-p',
'--public',
'-e',
'--env',
'-b',
'--build-env',
'-E',
'--dotenv',
'-C',
'--no-clipboard',
'-N',
'--forward-npm',
'--session-affinity',
'-T',
'--team',
'--regions',
'--no-verify'
];

module.exports = options;
21 changes: 21 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const got = require('got');
const pkg = require('../package.json');
const auth = require('./auth');

const request = url => {
const token = auth();
if (!token) return Promise.resolve();

const headers = {
'user-agent': `${pkg.name}/${pkg.version} (${pkg.homepage})`,
Authorization: `Bearer ${token}`
};

return got(url, {
baseUrl: 'https://api.zeit.co/v2',
json: true,
headers
});
};

module.exports = request;
10 changes: 10 additions & 0 deletions lib/subcommands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const ls = require('./ls');
const logs = require('./logs');

module.exports = {
ls,
list: ls,
logs,
log: logs,
inspect: logs
};
57 changes: 57 additions & 0 deletions lib/subcommands/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const tabtab = require('tabtab');
const { deployments } = require('../api');

const logs = async env => {
if (env.prev === 'logs' || env.prev === 'log') {
if (env.last === '-') {
return tabtab.log([
'-h',
'-a',
'-A',
'-Q',
'-d',
'-f',
'-n',
'-q',
'-t',
'-T',
'-o'
]);
} else if (env.last === '--') {
return tabtab.log([
'--help',
'--all',
'--local-config=',
'--global-config=',
'--debug',
'--follow',
'--query=',
'--token=',
'--since=',
'--until=',
'--team',
'--output='
]);
}
} else if (env.prev === 'inspect') {
if (env.last === '-' || env.last === '--') {
return tabtab.log([
'-h',
'--help',
'-A',
'--local-config=',
'-Q',
'--global-config=',
'-d',
'--debug',
'-T',
'--team'
]);
}
}

const urls = (await deployments()).map(d => d.url);
tabtab.log(urls);
};

module.exports = logs;
9 changes: 9 additions & 0 deletions lib/subcommands/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const tabtab = require('tabtab');
const { deployments } = require('../api');

const ls = async env => {
const names = (await deployments()).map(d => d.name);
tabtab.log(names);
};

module.exports = ls;

0 comments on commit 7d4d033

Please sign in to comment.