Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
feat(org): add tink org and subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Nov 7, 2018
1 parent 6bc856d commit 038b9aa
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 21 deletions.
1 change: 1 addition & 0 deletions bin/tink.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('../lib/node/index.js')
const CMDS = new Map([
['sh', require('../lib/commands/shell.js')],
['shell', require('../lib/commands/shell.js')],
['org', require('../lib/commands/org.jsx')],
['prep', require('../lib/commands/prepare.js')],
['prepare', require('../lib/commands/prepare.js')],
['ping', require('../lib/commands/ping.js')]
Expand Down
131 changes: 131 additions & 0 deletions lib/commands/org.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
'use strict'

const Org = module.exports = {
command: 'org',
describe: 'org-related subcommands',
builder (y) {
return y.help().alias('help', 'h')
.options(Org.options)
.demandCommand(1, 'Org subcommand is required')
.recommendCommands()
.command(
['add <orgname> <username> [role]', 'set'],
'Add someone to an org',
Org.options,
async argv => orgAdd(argv)
)
.command(
'rm <org> <user>',
'Remove someone from an org',
Org.options,
async argv => orgRm(argv)
)
.command(
'ls <org>',
'List org members',
Org.options,
async argv => orgLs(argv)
)
},
options: Object.assign(require('../common-opts.js', {}))
}

async function orgAdd (argv) {
const figgyPudding = require('figgy-pudding')
const libnpm = require('libnpm')
const npmConfig = require('../config.js')

const OrgConfig = figgyPudding({
json: {},
loglevel: {},
parseable: {},
silent: {}
})

const opts = OrgConfig(npmConfig().concat(argv).concat({
log: require('npmlog')
}))
const memDeets = await libnpm.org.set(argv.org, argv.user, argv.role, opts)
if (opts.json) {
console.log(JSON.stringify(memDeets, null, 2))
} else if (opts.parseable) {
console.log(['org', 'orgsize', 'user', 'role'].join('\t'))
console.log([
memDeets.org.name,
memDeets.org.size,
memDeets.user,
memDeets.role
])
} else if (!opts.silent && opts.loglevel !== 'silent') {
console.log(`Added ${memDeets.user} as ${memDeets.role} to ${memDeets.org.name}. You now ${memDeets.org.size} member${memDeets.org.size === 1 ? '' : 's'} in this org.`)
}
return memDeets
}

async function orgRm (argv) {
const figgyPudding = require('figgy-pudding')
const libnpm = require('libnpm')
const npmConfig = require('../config.js')

const OrgConfig = figgyPudding({
json: {},
loglevel: {},
parseable: {},
silent: {}
})

const opts = OrgConfig(npmConfig().concat(argv).concat({
log: require('npmlog')
}))
await libnpm.org.rm(argv.org, argv.user, opts)
const roster = libnpm.org.ls(argv.org, opts)
const user = argv.user.replace(/^[~@]?/, '')
const org = argv.org.replace(/^[~@]?/, '')
const userCount = Object.keys(roster).length
if (opts.json) {
console.log(JSON.stringify({
user,
org,
userCount,
deleted: true
}))
} else if (opts.parseable) {
console.log(['user', 'org', 'userCount', 'deleted'].join('\t'))
console.log([user, org, userCount, true].join('\t'))
} else if (!opts.silent && opts.loglevel !== 'silent') {
console.log(`Successfully removed ${user} from ${org}. You now have ${userCount} member${userCount === 1 ? '' : 's'} in this org.`)
}
}

async function orgLs (argv) {
const figgyPudding = require('figgy-pudding')
const { h, renderToString } = require('ink')
const libnpm = require('libnpm')
const npmConfig = require('../config.js')
const Table = require('ink-table').default

const OrgConfig = figgyPudding({
json: {},
loglevel: {},
parseable: {},
silent: {}
})

const opts = OrgConfig(npmConfig().concat(argv).concat({
log: require('npmlog')
}))
const roster = await libnpm.org.ls(argv.org, opts)
if (opts.json) {
console.log(JSON.stringify(roster, null, 2))
} else if (opts.parseable) {
console.log(['user', 'role'].join('\t'))
Object.keys(roster).forEach(user => {
console.log([user, roster[user]].join('\t'))
})
} else if (!opts.silent && opts.loglevel !== 'silent') {
const data = Object.keys(roster).map(user => {
return {user, role: roster[user]}
})
console.log(renderToString(<Table data={data}/>))
}
}
89 changes: 68 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"glob": "^7.1.3",
"graceful-fs": "^4.1.11",
"ini": "^1.3.5",
"ink": "^0.5.1",
"ink-table": "^1.0.3",
"jsx-transform": "^2.4.0",
"libnpm": "^1.3.0",
"lock-verify": "^2.0.2",
Expand Down

0 comments on commit 038b9aa

Please sign in to comment.