diff --git a/src/cli.js b/src/cli.js index 1d454cd..b787997 100755 --- a/src/cli.js +++ b/src/cli.js @@ -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() diff --git a/src/rpc-requests/acl-remote.js b/src/rpc-requests/acl-remote.js index 91b3c29..7d87425 100644 --- a/src/rpc-requests/acl-remote.js +++ b/src/rpc-requests/acl-remote.js @@ -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) } diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..c187c1e --- /dev/null +++ b/src/utils.js @@ -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, +}