2019-01-29 14:43:00 -08:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const eu = encodeURIComponent
|
|
|
|
const npmFetch = require('npm-registry-fetch')
|
|
|
|
const validate = require('aproba')
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
const cmd = module.exports
|
2019-01-29 14:43:00 -08:00
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
cmd.create = (entity, opts = {}) => {
|
|
|
|
return Promise.resolve().then(() => {
|
2019-07-24 23:00:03 -07:00
|
|
|
const { scope, team } = splitEntity(entity)
|
2019-01-29 14:43:00 -08:00
|
|
|
validate('SSO', [scope, team, opts])
|
2020-10-02 17:52:19 -04:00
|
|
|
const uri = `/-/org/${eu(scope)}/team`
|
|
|
|
return npmFetch.json(uri, {
|
|
|
|
...opts,
|
2019-01-29 14:43:00 -08:00
|
|
|
method: 'PUT',
|
|
|
|
scope,
|
2022-01-14 19:42:48 +02:00
|
|
|
body: { name: team, description: opts.description },
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
2019-01-29 14:43:00 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-18 17:09:39 -05:00
|
|
|
cmd.destroy = async (entity, opts = {}) => {
|
2020-10-02 17:52:19 -04:00
|
|
|
const { scope, team } = splitEntity(entity)
|
|
|
|
validate('SSO', [scope, team, opts])
|
|
|
|
const uri = `/-/team/${eu(scope)}/${eu(team)}`
|
2023-02-18 17:09:39 -05:00
|
|
|
await npmFetch(uri, {
|
2020-10-02 17:52:19 -04:00
|
|
|
...opts,
|
|
|
|
method: 'DELETE',
|
2022-01-14 19:42:48 +02:00
|
|
|
scope,
|
2023-02-18 17:09:39 -05:00
|
|
|
ignoreBody: true,
|
2019-01-29 14:43:00 -08:00
|
|
|
})
|
2023-02-18 17:09:39 -05:00
|
|
|
return true
|
2019-01-29 14:43:00 -08:00
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
cmd.add = (user, entity, opts = {}) => {
|
|
|
|
const { scope, team } = splitEntity(entity)
|
|
|
|
validate('SSO', [scope, team, opts])
|
|
|
|
const uri = `/-/team/${eu(scope)}/${eu(team)}/user`
|
|
|
|
return npmFetch.json(uri, {
|
|
|
|
...opts,
|
|
|
|
method: 'PUT',
|
|
|
|
scope,
|
2022-01-14 19:42:48 +02:00
|
|
|
body: { user },
|
2019-01-29 14:43:00 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-18 17:09:39 -05:00
|
|
|
cmd.rm = async (user, entity, opts = {}) => {
|
2020-10-02 17:52:19 -04:00
|
|
|
const { scope, team } = splitEntity(entity)
|
|
|
|
validate('SSO', [scope, team, opts])
|
|
|
|
const uri = `/-/team/${eu(scope)}/${eu(team)}/user`
|
2023-02-18 17:09:39 -05:00
|
|
|
await npmFetch(uri, {
|
2020-10-02 17:52:19 -04:00
|
|
|
...opts,
|
|
|
|
method: 'DELETE',
|
|
|
|
scope,
|
2022-01-14 19:42:48 +02:00
|
|
|
body: { user },
|
2023-02-18 17:09:39 -05:00
|
|
|
ignoreBody: true,
|
2019-01-29 14:43:00 -08:00
|
|
|
})
|
2023-02-18 17:09:39 -05:00
|
|
|
return true
|
2019-01-29 14:43:00 -08:00
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
cmd.lsTeams = (...args) => cmd.lsTeams.stream(...args).collect()
|
|
|
|
|
|
|
|
cmd.lsTeams.stream = (scope, opts = {}) => {
|
2019-01-29 14:43:00 -08:00
|
|
|
validate('SO', [scope, opts])
|
2020-10-02 17:52:19 -04:00
|
|
|
const uri = `/-/org/${eu(scope)}/team`
|
|
|
|
return npmFetch.json.stream(uri, '.*', {
|
|
|
|
...opts,
|
2022-01-14 19:42:48 +02:00
|
|
|
query: { format: 'cli' },
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
2019-01-29 14:43:00 -08:00
|
|
|
}
|
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
cmd.lsUsers = (...args) => cmd.lsUsers.stream(...args).collect()
|
|
|
|
|
|
|
|
cmd.lsUsers.stream = (entity, opts = {}) => {
|
2019-07-24 23:00:03 -07:00
|
|
|
const { scope, team } = splitEntity(entity)
|
2019-01-29 14:43:00 -08:00
|
|
|
validate('SSO', [scope, team, opts])
|
|
|
|
const uri = `/-/team/${eu(scope)}/${eu(team)}/user`
|
2020-10-02 17:52:19 -04:00
|
|
|
return npmFetch.json.stream(uri, '.*', {
|
|
|
|
...opts,
|
2022-01-14 19:42:48 +02:00
|
|
|
query: { format: 'cli' },
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
2019-01-29 14:43:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.edit = () => {
|
|
|
|
throw new Error('edit is not implemented yet')
|
|
|
|
}
|
|
|
|
|
|
|
|
function splitEntity (entity = '') {
|
2020-10-02 17:52:19 -04:00
|
|
|
const [, scope, team] = entity.match(/^@?([^:]+):(.*)$/) || []
|
2019-07-24 23:00:03 -07:00
|
|
|
return { scope, team }
|
2019-01-29 14:43:00 -08:00
|
|
|
}
|