mirror of
https://github.com/aljazceru/gitpear.git
synced 2025-12-17 14:14:22 +01:00
remote-acl: list (cleaner)
Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
49
src/cli.js
49
src/cli.js
@@ -14,6 +14,8 @@ const git = require('./git')
|
|||||||
|
|
||||||
const { listRemote, aclRemote } = require('./rpc-requests')
|
const { listRemote, aclRemote } = require('./rpc-requests')
|
||||||
|
|
||||||
|
const { printACL, printACLForUser, checkIfGitRepo, logBranches } = require('./utils')
|
||||||
|
|
||||||
const pkg = require('../package.json')
|
const pkg = require('../package.json')
|
||||||
program
|
program
|
||||||
.name('gitpear')
|
.name('gitpear')
|
||||||
@@ -106,12 +108,14 @@ program
|
|||||||
|
|
||||||
if (options.user) {
|
if (options.user) {
|
||||||
if (p.startsWith('pear://')) {
|
if (p.startsWith('pear://')) {
|
||||||
|
if (n === '.') n = ''
|
||||||
await remoteACL(a, n, p, options)
|
await remoteACL(a, n, p, options)
|
||||||
} else {
|
} else {
|
||||||
localACL(a, n, p, options)
|
localACL(a, n, p, options)
|
||||||
}
|
}
|
||||||
} else if (options.branch) {
|
} else if (options.branch) {
|
||||||
if (p.startsWith('pear://')) {
|
if (p.startsWith('pear://')) {
|
||||||
|
if (n === '.') n = ''
|
||||||
await remoteBranchProtectionRules(a, n, p, options)
|
await remoteBranchProtectionRules(a, n, p, options)
|
||||||
} else {
|
} else {
|
||||||
localBranchProtectionRules(a, n, p, options)
|
localBranchProtectionRules(a, n, p, options)
|
||||||
@@ -224,14 +228,20 @@ function localBranchProtectionRules(a, b, p, options) {
|
|||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a === 'list' && !b) { logBranches(name) }
|
if (a === 'list' && !b) {
|
||||||
|
const repoACL = acl.getACL(name)
|
||||||
|
logBranches(repoACL)
|
||||||
|
}
|
||||||
|
|
||||||
if (a === 'add') {
|
if (a === 'add') {
|
||||||
acl.addProtectedBranch(name, b)
|
acl.addProtectedBranch(name, b)
|
||||||
logBranches(name)
|
const repoACL = acl.getACL(name)
|
||||||
|
logBranches(repoACL)
|
||||||
}
|
}
|
||||||
if (a === 'remove') {
|
if (a === 'remove') {
|
||||||
acl.removeProtectedBranch(name, b)
|
acl.removeProtectedBranch(name, b)
|
||||||
logBranches(name)
|
const repoACL = acl.getACL(name)
|
||||||
|
logBranches(repoACL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,19 +257,12 @@ function localACL(a, u, p, options) {
|
|||||||
const repoACL = acl.getACL(name)
|
const repoACL = acl.getACL(name)
|
||||||
|
|
||||||
if (a === 'list' && !u) {
|
if (a === 'list' && !u) {
|
||||||
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
printACL(repoACL)
|
||||||
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
|
||||||
console.log('User:', '\t', 'Role:')
|
|
||||||
for (const user in repoACL.ACL) {
|
|
||||||
console.log(user, '\t', repoACL.ACL[user])
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a === 'list') {
|
if (a === 'list') {
|
||||||
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
printACLForUser(repoACL, u)
|
||||||
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
|
||||||
console.log('User:', u, '\t', repoACL.ACL[u])
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,11 +301,12 @@ function localACL(a, u, p, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function remoteBranchProtectionRules(a, b, p, options) {
|
async function remoteBranchProtectionRules(a, b, p, options) {
|
||||||
// TODO
|
|
||||||
if (a === 'list') {
|
if (a === 'list') {
|
||||||
await aclRemote.list(p)
|
await aclRemote.list(p, b, { branch: true })
|
||||||
} else if (a === 'add') {
|
} else if (a === 'add') {
|
||||||
|
await aclRemote.add(p, b, { branch: true })
|
||||||
} else if (a === 'remove') {
|
} else if (a === 'remove') {
|
||||||
|
await aclRemote.remove(p, b, { branch: true })
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid action')
|
throw new Error('Invalid action')
|
||||||
}
|
}
|
||||||
@@ -310,21 +314,16 @@ async function remoteBranchProtectionRules(a, b, p, options) {
|
|||||||
|
|
||||||
async function remoteACL(a, b, p, options) {
|
async function remoteACL(a, b, p, options) {
|
||||||
if (a === 'list') {
|
if (a === 'list') {
|
||||||
await aclRemote.list(p)
|
await aclRemote.list(p, b)
|
||||||
} else if (a === 'add') {
|
} else if (a === 'add') {
|
||||||
|
await aclRemote.add(p, b)
|
||||||
} else if (a === 'remove') {
|
} else if (a === 'remove') {
|
||||||
|
await aclRemote.remove(p, b)
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid action')
|
throw new Error('Invalid action')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkIfGitRepo(p) {
|
|
||||||
if (!fs.existsSync(path.join(p, '.git'))) {
|
|
||||||
console.error('Not a git repo')
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function share(name, branchToShare, options) {
|
async function share(name, branchToShare, options) {
|
||||||
let aclOptions
|
let aclOptions
|
||||||
let message = `Shared "${name}" project, ${branchToShare} branch`
|
let message = `Shared "${name}" project, ${branchToShare} branch`
|
||||||
@@ -339,10 +338,4 @@ async function share(name, branchToShare, options) {
|
|||||||
console.log(message)
|
console.log(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
function logBranches(name) {
|
|
||||||
const repoACL = acl.getACL(name)
|
|
||||||
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
|
||||||
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
|
||||||
}
|
|
||||||
|
|
||||||
program.parse()
|
program.parse()
|
||||||
|
|||||||
@@ -6,15 +6,43 @@ const crypto = require('hypercore-crypto')
|
|||||||
const home = require('../home')
|
const home = require('../home')
|
||||||
const auth = require('../auth')
|
const auth = require('../auth')
|
||||||
|
|
||||||
async function list (url, name) {
|
const { printACL, printACLForUser, logBranches } = require('../utils')
|
||||||
const matches = url.match(/pear:\/\/([a-f0-9]{64})/)
|
|
||||||
|
|
||||||
if (!matches || matches.length < 2) {
|
async function list(url, name, rpc, opts) {
|
||||||
|
const payload = { body: { url, method: 'get-acl' } }
|
||||||
|
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
|
||||||
|
payload.header = await auth.getToken(payload.body)
|
||||||
|
}
|
||||||
|
const repoACLres = await rpc.request('get-acl', Buffer.from(JSON.stringify(payload)))
|
||||||
|
const repoACL = JSON.parse(repoACLres.toString())
|
||||||
|
|
||||||
|
opts.branch ? listACLBranch(repoACL) : listACLUser(repoACL, name)
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
function listACLUser(repoACL, u) {
|
||||||
|
u ? printACLForUser(repoACL, u) : printACL(repoACL)
|
||||||
|
}
|
||||||
|
|
||||||
|
function listACLBranch(repoACL) {
|
||||||
|
logBranches(repoACL)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function wrapper (url, name, opts = {}, cb) {
|
||||||
|
if (typeof opts === 'function') {
|
||||||
|
cb = opts
|
||||||
|
opts = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const matches = url.match(/pear:\/\/([a-f0-9]{64})\/(.*)/)
|
||||||
|
|
||||||
|
if (!matches || matches.length < 3) {
|
||||||
console.error('Invalid URL')
|
console.error('Invalid URL')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetKey = matches[1]
|
const targetKey = matches[1]
|
||||||
|
const repoName = matches[2]
|
||||||
console.log('Connecting to:', targetKey)
|
console.log('Connecting to:', targetKey)
|
||||||
|
|
||||||
const swarmOpts = {}
|
const swarmOpts = {}
|
||||||
@@ -44,30 +72,15 @@ async function list (url, name) {
|
|||||||
console.error('Failed to retrieve repositories')
|
console.error('Failed to retrieve repositories')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
if (!repositories[repoName]) {
|
||||||
paylod = { body: { url, method: 'get-acl' } }
|
console.error('Repository not found')
|
||||||
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
|
process.exit(1)
|
||||||
payload.header = await auth.getToken(payload.body)
|
|
||||||
}
|
|
||||||
const repoACLres = await rpc.request('get-acl', Buffer.from(JSON.stringify(payload)))
|
|
||||||
const repoACL = JSON.parse(repoACLres.toString())
|
|
||||||
|
|
||||||
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
|
||||||
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
|
||||||
console.log('User:', '\t', 'Role:')
|
|
||||||
if (name) {
|
|
||||||
console.log(name, '\t', repoACL.ACL[name])
|
|
||||||
process.exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const user in repoACL.ACL) {
|
await cb(url, name, rpc, opts)
|
||||||
console.log(user, '\t', repoACL.ACL[user])
|
|
||||||
}
|
|
||||||
|
|
||||||
process.exit(0)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
list,
|
list: (url, name, opts) => wrapper(url, name, opts, list)
|
||||||
}
|
}
|
||||||
|
|||||||
35
src/utils.js
Normal file
35
src/utils.js
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
function printACL(repoACL) {
|
||||||
|
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
||||||
|
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
||||||
|
console.log('User:', '\t', 'Role:')
|
||||||
|
for (const user in repoACL.ACL) {
|
||||||
|
console.log(user, '\t', repoACL.ACL[user])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function printACLForUser(repoACL, u) {
|
||||||
|
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
||||||
|
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
||||||
|
console.log('User:', u, '\t', repoACL.ACL[u])
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkIfGitRepo(p) {
|
||||||
|
if (!fs.existsSync(path.join(p, '.git'))) {
|
||||||
|
console.error('Not a git repo')
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function logBranches(repoACL) {
|
||||||
|
console.log('Repo Visibility:', '\t', repoACL.visibility)
|
||||||
|
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
printACL,
|
||||||
|
checkIfGitRepo,
|
||||||
|
logBranches,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user