remote-acl: list (cleaner)

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-14 18:32:33 +00:00
parent 20599d854f
commit 711d717606
3 changed files with 92 additions and 51 deletions

View File

@@ -14,6 +14,8 @@ const git = require('./git')
const { listRemote, aclRemote } = require('./rpc-requests')
const { printACL, printACLForUser, checkIfGitRepo, logBranches } = require('./utils')
const pkg = require('../package.json')
program
.name('gitpear')
@@ -106,12 +108,14 @@ program
if (options.user) {
if (p.startsWith('pear://')) {
if (n === '.') n = ''
await remoteACL(a, n, p, options)
} else {
localACL(a, n, p, options)
}
} else if (options.branch) {
if (p.startsWith('pear://')) {
if (n === '.') n = ''
await remoteBranchProtectionRules(a, n, p, options)
} else {
localBranchProtectionRules(a, n, p, options)
@@ -224,14 +228,20 @@ function localBranchProtectionRules(a, b, p, options) {
process.exit(1)
}
if (a === 'list' && !b) { logBranches(name) }
if (a === 'list' && !b) {
const repoACL = acl.getACL(name)
logBranches(repoACL)
}
if (a === 'add') {
acl.addProtectedBranch(name, b)
logBranches(name)
const repoACL = acl.getACL(name)
logBranches(repoACL)
}
if (a === 'remove') {
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)
if (a === 'list' && !u) {
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])
}
printACL(repoACL)
return
}
if (a === 'list') {
console.log('Repo Visibility:', '\t', repoACL.visibility)
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
console.log('User:', u, '\t', repoACL.ACL[u])
printACLForUser(repoACL, u)
return
}
@@ -298,11 +301,12 @@ function localACL(a, u, p, options) {
}
async function remoteBranchProtectionRules(a, b, p, options) {
// TODO
if (a === 'list') {
await aclRemote.list(p)
await aclRemote.list(p, b, { branch: true })
} else if (a === 'add') {
await aclRemote.add(p, b, { branch: true })
} else if (a === 'remove') {
await aclRemote.remove(p, b, { branch: true })
} else {
throw new Error('Invalid action')
}
@@ -310,21 +314,16 @@ async function remoteBranchProtectionRules(a, b, p, options) {
async function remoteACL(a, b, p, options) {
if (a === 'list') {
await aclRemote.list(p)
await aclRemote.list(p, b)
} else if (a === 'add') {
await aclRemote.add(p, b)
} else if (a === 'remove') {
await aclRemote.remove(p, b)
} else {
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) {
let aclOptions
let message = `Shared "${name}" project, ${branchToShare} branch`
@@ -339,10 +338,4 @@ async function share(name, branchToShare, options) {
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()

View File

@@ -6,15 +6,43 @@ const crypto = require('hypercore-crypto')
const home = require('../home')
const auth = require('../auth')
async function list (url, name) {
const matches = url.match(/pear:\/\/([a-f0-9]{64})/)
const { printACL, printACLForUser, logBranches } = require('../utils')
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')
process.exit(1)
}
const targetKey = matches[1]
const repoName = matches[2]
console.log('Connecting to:', targetKey)
const swarmOpts = {}
@@ -44,30 +72,15 @@ async function list (url, name) {
console.error('Failed to retrieve repositories')
process.exit(1)
}
paylod = { 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())
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)
if (!repositories[repoName]) {
console.error('Repository not found')
process.exit(1)
}
for (const user in repoACL.ACL) {
console.log(user, '\t', repoACL.ACL[user])
}
process.exit(0)
await cb(url, name, rpc, opts)
})
}
module.exports = {
list,
list: (url, name, opts) => wrapper(url, name, opts, list)
}

35
src/utils.js Normal file
View 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,
}