From 24ce1c2b983e0372a3d0b2f7ab5d2acdb716c5d8 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 26 May 2017 14:40:31 -0700 Subject: [PATCH] Add `now whoami` command This version simply prints out the logged in username, for consistency. It no longer prints out the current team name. Squashed commit of the following: commit 1d4ef6af9507a1180fd9e84b7cc64c7fe3e815fa Author: Nathan Rajlich Date: Fri May 26 14:40:00 2017 -0700 add "whoami" to valid command names commit 6ea37e49eb1ea2d9b5b12befe26a6a15972e1f6c Author: Nathan Rajlich Date: Fri May 26 14:37:56 2017 -0700 don't log current team name in `now whoami` Instead, consistently return the logged in username. --- bin/now-whoami.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++ bin/now.js | 3 +- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 bin/now-whoami.js diff --git a/bin/now-whoami.js b/bin/now-whoami.js new file mode 100644 index 0000000..94497a4 --- /dev/null +++ b/bin/now-whoami.js @@ -0,0 +1,70 @@ +#!/usr/bin/env node + +// Packages +const minimist = require('minimist') +const chalk = require('chalk') + +// Ours +const cfg = require('../lib/cfg') +const exit = require('../lib/utils/exit') +const cmd = require('../lib/utils/output/cmd') +const logo = require('../lib/utils/output/logo') + +const argv = minimist(process.argv.slice(2), { + string: ['config', 'token'], + boolean: ['help', 'debug', 'all'], + alias: { + help: 'h', + config: 'c', + debug: 'd', + token: 't' + } +}) + +const help = () => { + console.log(` + ${chalk.bold(`${logo} now whoami`)} + + ${chalk.dim('Options:')} + + -h, --help Output usage information + -c ${chalk.bold.underline('FILE')}, --config=${chalk.bold.underline('FILE')} Config file + -d, --debug Debug mode [off] + -t ${chalk.bold.underline('TOKEN')}, --token=${chalk.bold.underline('TOKEN')} Login token + + ${chalk.dim('Examples:')} + + ${chalk.gray('–')} Show the current team context + + ${chalk.cyan('$ now whoami')} +`) +} + +if (argv.help) { + help() + process.exit(0) +} + +if (argv.config) { + cfg.setConfigFile(argv.config) +} + +async function whoami() { + const config = await cfg.read({ token: argv.token }) + if (!config || !config.token) { + console.log( + `> Not currently logged in! Please run ${cmd('now --login')}.\n` + ) + return exit(1) + } + + if (process.stdout.isTTY) { + process.stdout.write('> ') + } + + const { user } = config + const name = user.username || user.email + console.log(name) +} + +whoami() diff --git a/bin/now.js b/bin/now.js index fefabe4..eb07b66 100755 --- a/bin/now.js +++ b/bin/now.js @@ -53,7 +53,8 @@ const commands = new Set([ 'log', 'logs', 'scale', - 'logout' + 'logout', + 'whoami' ]) const aliases = new Map([