mirror of
https://github.com/aljazceru/gitpear.git
synced 2025-12-18 06:34:28 +01:00
cli: unify acl branch and user
Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
87
src/cli.js
87
src/cli.js
@@ -12,7 +12,7 @@ const home = require('./home')
|
|||||||
const acl = require('./acl')
|
const acl = require('./acl')
|
||||||
const git = require('./git')
|
const git = require('./git')
|
||||||
|
|
||||||
const { listRemote } = require('./rpc-requests')
|
const { listRemote, bpr } = require('./rpc-requests')
|
||||||
|
|
||||||
const pkg = require('../package.json')
|
const pkg = require('../package.json')
|
||||||
program
|
program
|
||||||
@@ -81,33 +81,48 @@ program
|
|||||||
await share(name, branchToShare, options)
|
await share(name, branchToShare, options)
|
||||||
})
|
})
|
||||||
|
|
||||||
program
|
|
||||||
.command('branch')
|
|
||||||
.description('branch protection rules')
|
|
||||||
.addArgument(new commander.Argument('[a]', 'actiont to perform').choices(['add', 'remove', 'list']).default('list'))
|
|
||||||
.addArgument(new commander.Argument('[b]', 'branch name').default(''))
|
|
||||||
.addArgument(new commander.Argument('[p]', 'path to the repo').default('.'))
|
|
||||||
.action(async (a, b, p, options) => {
|
|
||||||
if (p.startsWith('pear://')) {
|
|
||||||
await remoteBranchProtectionRules(a, b, p, options)
|
|
||||||
} else {
|
|
||||||
localBranchProtectionRules(a, b, p, options)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('acl')
|
.command('acl')
|
||||||
.description('set acl of a gitpear repo')
|
.description('manage acl of a gitpear repo')
|
||||||
|
.option('-u, --user', 'user to add/remove/list')
|
||||||
|
.option('-b, --branch', 'branch to add/remove/list in protected branches')
|
||||||
.addArgument(new commander.Argument('[a]', 'actiont to perform').choices(['add', 'remove', 'list']).default('list'))
|
.addArgument(new commander.Argument('[a]', 'actiont to perform').choices(['add', 'remove', 'list']).default('list'))
|
||||||
.addArgument(new commander.Argument('[u]', 'user to add/remove/list').default(''))
|
.addArgument(new commander.Argument('[n]', 'user or branch to add/remove/list').default(''))
|
||||||
.addArgument(new commander.Argument('[p]', 'path to the repo').default('.'))
|
.addArgument(new commander.Argument('[p]', 'path to the repo').default('.'))
|
||||||
.action(async (a, u, p, options) => {
|
.action(async (a, n, p, options) => {
|
||||||
|
if (options.user && options.branch) {
|
||||||
|
throw new Error('Cannot perform both user and branch action at the same time')
|
||||||
|
}
|
||||||
|
|
||||||
if (p.startsWith('pear://')) {
|
console.log(a, n, p, options)
|
||||||
await remoteACL(a, u, p, options)
|
|
||||||
|
if (!options.user && !options.branch) {
|
||||||
|
if (a !== 'list') throw new Error('Need either user or branch option')
|
||||||
|
const repoACL = await getACL(p)
|
||||||
|
|
||||||
|
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
||||||
|
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
||||||
|
for (const u in repoACL.ACL) {
|
||||||
|
console.log('User:', u, '\t', repoACL.ACL[u])
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (options.user) {
|
||||||
|
if (p.startsWith('pear://') || n.startsWith('pear://')) {
|
||||||
|
// XXX
|
||||||
|
await remoteACL(a, n, p, options)
|
||||||
} else {
|
} else {
|
||||||
localACL(a, u, p, options)
|
localACL(a, n, p, options)
|
||||||
|
}
|
||||||
|
} else if (options.branch) {
|
||||||
|
if (p.startsWith('pear://') || n.startsWith('pear://')) {
|
||||||
|
// XXX
|
||||||
|
await remoteBranchProtectionRules(a, n, p, options)
|
||||||
|
} else {
|
||||||
|
localBranchProtectionRules(a, n, p, options)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -249,6 +264,7 @@ function localACL(a, u, p, options) {
|
|||||||
|
|
||||||
if (a === 'list') {
|
if (a === 'list') {
|
||||||
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
||||||
|
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
||||||
console.log('User:', u, '\t', repoACL.ACL[u])
|
console.log('User:', u, '\t', repoACL.ACL[u])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -288,6 +304,9 @@ function localACL(a, u, p, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function remoteBranchProtectionRules(a, b, p, options) {
|
async function remoteBranchProtectionRules(a, b, p, options) {
|
||||||
|
if (a === 'list') {
|
||||||
|
return await bpr.list(p)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function remoteACL(a, b, p, options) {
|
async function remoteACL(a, b, p, options) {
|
||||||
@@ -317,7 +336,27 @@ async function share(name, branchToShare, options) {
|
|||||||
function logBranches(name) {
|
function logBranches(name) {
|
||||||
const repoACL = acl.getACL(name)
|
const repoACL = acl.getACL(name)
|
||||||
console.log('Visibility:', '\t', repoACL.visibility)
|
console.log('Visibility:', '\t', repoACL.visibility)
|
||||||
console.log('Protected Branch(s):')
|
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
||||||
for (const branch of repoACL.protectedBranches) { console.log(branch) }
|
}
|
||||||
|
|
||||||
|
async function getACL(p) {
|
||||||
|
return (p.startsWith('pear://')) ? (await getRemoteACL(p)) : (await getLocalACL(p))
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getLocalACL(p) {
|
||||||
|
const fullPath = path.resolve(p)
|
||||||
|
checkIfGitRepo(fullPath)
|
||||||
|
|
||||||
|
const name = fullPath.split(path.sep).pop()
|
||||||
|
if (!home.isInitialized(name)) {
|
||||||
|
console.error(`${name} is not initialized`)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return acl.getACL(name)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getRemoteACL(p) {
|
||||||
}
|
}
|
||||||
program.parse()
|
program.parse()
|
||||||
|
|||||||
Reference in New Issue
Block a user