From 6e9764667663b39e9e97c6212e213788e4f1d1a6 Mon Sep 17 00:00:00 2001 From: dzdidi Date: Thu, 15 Feb 2024 19:39:21 +0000 Subject: [PATCH] cli: helpers Signed-off-by: dzdidi --- src/cli-helpers.js | 156 +++++++++++++++++++++++++++++++++++++++++++++ src/cli.js | 145 +---------------------------------------- 2 files changed, 159 insertions(+), 142 deletions(-) create mode 100644 src/cli-helpers.js diff --git a/src/cli-helpers.js b/src/cli-helpers.js new file mode 100644 index 0000000..1b5af91 --- /dev/null +++ b/src/cli-helpers.js @@ -0,0 +1,156 @@ +const path = require('path') + +const home = require('./home') +const acl = require('./acl') +const git = require('./git') + +const { aclRemote } = require('./rpc-requests') +const { printACL, printACLForUser, checkIfGitRepo, logBranches } = require('./utils') + +async function remoteACL (a, b, p, options) { + if (a === 'list') { + await aclRemote.list(p, b) + } else if (a === 'add') { + if (!b) { + console.error('User not provided') + process.exit(1) + } + if (b.split(':').length !== 2) { + console.error('Invalid role') + process.exit(1) + } + await aclRemote.add(p, b) + } else if (a === 'remove') { + if (!b) { + console.error('User not provided') + process.exit(1) + } + await aclRemote.remove(p, b) + } else { + throw new Error('Invalid action') + } +} + +async function share (name, branchToShare, options) { + let aclOptions + let message = `Shared "${name}" project, ${branchToShare} branch` + if (options?.visibility) { + aclOptions = { visibility: options.visibility } + message = `${message}, as ${options.visibility} repo` + } + + try { home.shareAppFolder(name) } catch (e) { } + try { acl.setACL(name, aclOptions) } catch (e) { } + try { await git.push(branchToShare) } catch (e) { } + console.log(message) +} + +async function remoteBranchProtectionRules (a, b, p, options) { + if (a === 'list') { + await aclRemote.list(p, b, { branch: true }) + } else if (a === 'add') { + await aclRemote.add(p, b, { branch: true }) + if (!b) { + console.error('branch is not provided') + process.exit(1) + } + } else if (a === 'remove') { + if (!b) { + console.error('branch is not provided') + process.exit(1) + } + await aclRemote.remove(p, b, { branch: true }) + } else { + throw new Error('Invalid action') + } +} + +function localACL (a, u, p, options) { + 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) + } + const repoACL = acl.getACL(name) + + if (a === 'list' && !u) { + printACL(repoACL) + return + } + + if (a === 'list') { + printACLForUser(repoACL, u) + return + } + + if (a === 'add') { + if (!u) { + console.error('User not provided') + process.exit(1) + } + + const [userId, role] = u.split(':') + if (repoACL.ACL[userId]) { + console.error(`${userId} already has access to ${name} as ${repoACL.ACL[userId]}`) + process.exit(1) + } + + acl.grantAccessToUser(name, userId, role) + console.log(`Added ${userId} to ${name} as ${role}`) + return + } + + if (a === 'remove') { + if (!u) { + console.error('User not provided') + process.exit(1) + } + + if (!repoACL.ACL[u]) { + console.error(`${u} does not have access to ${name}`) + process.exit(1) + } + + acl.revokeAccessFromUser(name, u) + console.log(`Removed ${u} from ${name}`) + } +} + +function localBranchProtectionRules (a, b, p, options) { + 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) + } + + if (a === 'list' && !b) { + const repoACL = acl.getACL(name) + logBranches(repoACL) + } + + if (a === 'add') { + acl.addProtectedBranch(name, b) + const repoACL = acl.getACL(name) + logBranches(repoACL) + } + if (a === 'remove') { + acl.removeProtectedBranch(name, b) + const repoACL = acl.getACL(name) + logBranches(repoACL) + } +} + +module.exports = { + remoteACL, + share, + remoteBranchProtectionRules, + localACL, + localBranchProtectionRules + +} diff --git a/src/cli.js b/src/cli.js index 5d9d9a1..2157036 100755 --- a/src/cli.js +++ b/src/cli.js @@ -8,12 +8,12 @@ const program = new commander.Command() const path = require('path') const home = require('./home') -const acl = require('./acl') const git = require('./git') -const { listRemote, aclRemote } = require('./rpc-requests') +const { listRemote } = require('./rpc-requests') -const { printACL, printACLForUser, checkIfGitRepo, logBranches } = require('./utils') +const { checkIfGitRepo } = require('./utils') +const { remoteACL, share, remoteBranchProtectionRules, localACL, localBranchProtectionRules } = require('./cli-helpers') const pkg = require('../package.json') program @@ -207,143 +207,4 @@ program } }) -function localBranchProtectionRules (a, b, p, options) { - 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) - } - - if (a === 'list' && !b) { - const repoACL = acl.getACL(name) - logBranches(repoACL) - } - - if (a === 'add') { - acl.addProtectedBranch(name, b) - const repoACL = acl.getACL(name) - logBranches(repoACL) - } - if (a === 'remove') { - acl.removeProtectedBranch(name, b) - const repoACL = acl.getACL(name) - logBranches(repoACL) - } -} - -function localACL (a, u, p, options) { - 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) - } - const repoACL = acl.getACL(name) - - if (a === 'list' && !u) { - printACL(repoACL) - return - } - - if (a === 'list') { - printACLForUser(repoACL, u) - return - } - - if (a === 'add') { - if (!u) { - console.error('User not provided') - process.exit(1) - } - - const [userId, role] = u.split(':') - if (repoACL.ACL[userId]) { - console.error(`${userId} already has access to ${name} as ${repoACL.ACL[userId]}`) - process.exit(1) - } - - acl.grantAccessToUser(name, userId, role) - console.log(`Added ${userId} to ${name} as ${role}`) - return - } - - if (a === 'remove') { - if (!u) { - console.error('User not provided') - process.exit(1) - } - - if (!repoACL.ACL[u]) { - console.error(`${u} does not have access to ${name}`) - process.exit(1) - } - - acl.revokeAccessFromUser(name, u) - console.log(`Removed ${u} from ${name}`) - } -} - -async function remoteBranchProtectionRules (a, b, p, options) { - if (a === 'list') { - await aclRemote.list(p, b, { branch: true }) - } else if (a === 'add') { - await aclRemote.add(p, b, { branch: true }) - if (!b) { - console.error('branch is not provided') - process.exit(1) - } - } else if (a === 'remove') { - if (!b) { - console.error('branch is not provided') - process.exit(1) - } - await aclRemote.remove(p, b, { branch: true }) - } else { - throw new Error('Invalid action') - } -} - -async function remoteACL (a, b, p, options) { - if (a === 'list') { - await aclRemote.list(p, b) - } else if (a === 'add') { - if (!b) { - console.error('User not provided') - process.exit(1) - } - if (b.split(':').length !== 2) { - console.error('Invalid role') - process.exit(1) - } - await aclRemote.add(p, b) - } else if (a === 'remove') { - if (!b) { - console.error('User not provided') - process.exit(1) - } - await aclRemote.remove(p, b) - } else { - throw new Error('Invalid action') - } -} - -async function share (name, branchToShare, options) { - let aclOptions - let message = `Shared "${name}" project, ${branchToShare} branch` - if (options?.visibility) { - aclOptions = { visibility: options.visibility } - message = `${message}, as ${options.visibility} repo` - } - - try { home.shareAppFolder(name) } catch (e) { } - try { acl.setACL(name, aclOptions) } catch (e) { } - try { await git.push(branchToShare) } catch (e) { } - console.log(message) -} - program.parse()