Browse Source

Add `teams ls` command (#563)

* Add `teams ls` command

* Fix Prettier

* Add `teams list` command alias

* Add table output primitive

* Use table output primitive to list team

* Fix name/email for user

* Change indicator for current team
master
CHaBou 8 years ago
committed by Leo Lamprecht
parent
commit
567210270a
  1. 8
      bin/now-teams.js
  2. 62
      bin/teams/list.js
  3. 27
      lib/utils/output/table.js

8
bin/now-teams.js

@ -111,6 +111,14 @@ async function run({ token, config: { currentTeam } }) {
const args = argv._.slice(1)
switch (subcommand) {
case 'list':
case 'ls': {
await require(resolve(__dirname, 'teams', 'list.js'))({
teams,
token
})
break
}
case 'switch':
case 'change': {
await require(resolve(__dirname, 'teams', 'switch.js'))({

62
bin/teams/list.js

@ -0,0 +1,62 @@
const chalk = require('chalk')
const wait = require('../../lib/utils/output/wait')
const cfg = require('../../lib/cfg')
const info = require('../../lib/utils/output/info')
const error = require('../../lib/utils/output/error')
const { tick: tickChar } = require('../../lib/utils/output/chars')
const table = require('../../lib/utils/output/table')
module.exports = async function({ teams, token }) {
const stopSpinner = wait('Fetching teams')
const list = (await teams.ls()).teams
let { user, currentTeam } = await cfg.read({ token })
const accountIsCurrent = !currentTeam
stopSpinner()
if (accountIsCurrent) {
currentTeam = {
slug: user.username || user.email
}
}
const teamList = list.map(({ slug, name }) => {
return {
name,
value: slug,
current: slug === currentTeam.slug ? tickChar : ''
}
})
teamList.unshift({
name: user.email,
value: user.username || user.email,
current: (accountIsCurrent && tickChar) || ''
})
// Let's bring the current team to the beginning of the list
if (!accountIsCurrent) {
const index = teamList.findIndex(
choice => choice.value === currentTeam.slug
)
const choice = teamList.splice(index, 1)[0]
teamList.unshift(choice)
}
// Printing
const count = teamList.length
if (!count) {
// Maybe should not happen
error(`No team found`)
return
}
info(`${chalk.bold(count)} team${count > 1 ? 's' : ''} found`)
console.log()
table(
['', 'id', 'email / name'],
teamList.map(team => [team.current, team.value, team.name]),
[1, 5]
)
}

27
lib/utils/output/table.js

@ -0,0 +1,27 @@
const chalk = require('chalk')
const printf = require('printf')
const printLine = (data, sizes) =>
data.reduce((line, col, i) => {
return line + printf(`%-${sizes[i]}s`, col)
}, '')
// Print a table
module.exports = (fieldNames = [], data = [], margins = []) => {
// Compute size of each column
const sizes = data
.reduce((acc, row) => {
return row.map((col, i) => {
const currentMaxColSize = acc[i] || 0
const colSize = (col && col.length) || 0
return Math.max(currentMaxColSize, colSize)
})
}, fieldNames.map(col => col.length))
// Add margin to all columns except the last
.map((size, i) => (i < margins.length && size + margins[i]) || size)
// Print header
console.log(chalk.grey(printLine(fieldNames, sizes)))
// Print content
data.forEach(row => console.log(printLine(row, sizes)))
}
Loading…
Cancel
Save