cli: path as an option

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-15 17:46:27 +00:00
parent cd6177d4bf
commit d8feb25756

View File

@@ -25,10 +25,10 @@ program
program program
.command('init') .command('init')
.description('initialize a gitpear repo') .description('initialize a gitpear repo')
.addArgument(new commander.Argument('[p]', 'path to the repo').default('.')) .option('-p, --path [path]', 'paht to the git repo', '.')
.option('-s, --share [branch]', 'share the repo as public, default false, default branch is current', '') .option('-s, --share [branch]', 'share the repo as public, default false, default branch is current', '')
.action(async (p, options) => { .action(async (options) => {
const fullPath = path.resolve(p) const fullPath = path.resolve(options.path)
checkIfGitRepo(fullPath) checkIfGitRepo(fullPath)
const name = fullPath.split(path.sep).pop() const name = fullPath.split(path.sep).pop()
@@ -67,7 +67,7 @@ program
.description('share a gitpear repo') .description('share a gitpear repo')
.option('-b, --branch [b]', 'branch to share, default is current branch', '') .option('-b, --branch [b]', 'branch to share, default is current branch', '')
.option('-v, --visibility [v]', 'visibility of the repo', 'public') .option('-v, --visibility [v]', 'visibility of the repo', 'public')
.option('-p, --path [p]', 'path to the repo', '.') .option('-p, --path [path]', 'path to the repo', '.')
.action(async (options) => { .action(async (options) => {
const fullPath = path.resolve(options.path) const fullPath = path.resolve(options.path)
checkIfGitRepo(fullPath) checkIfGitRepo(fullPath)
@@ -88,10 +88,10 @@ program
.description('manage acl of a gitpear repo') .description('manage acl of a gitpear repo')
.option('-u, --user', 'user to add/remove/list') .option('-u, --user', 'user to add/remove/list')
.option('-b, --branch', 'branch to add/remove/list in protected branches') .option('-b, --branch', 'branch to add/remove/list in protected branches')
.option('-p, --path [path]', 'path to the repo', '.')
.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('[n]', 'user or branch 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('.')) .action(async (a, n, options) => {
.action(async (a, n, p, options) => {
if (options.user && options.branch) { if (options.user && options.branch) {
throw new Error('Cannot perform both user and branch action at the same time') throw new Error('Cannot perform both user and branch action at the same time')
} }
@@ -100,25 +100,18 @@ program
throw new Error('Either user or branch option is required') throw new Error('Either user or branch option is required')
} }
if (n.startsWith('pear://')) {
let swap = n
n = p
p = swap
}
if (options.user) { if (options.user) {
if (p.startsWith('pear://')) { if (options.path.startsWith('pear://')) {
if (n === '.') n = '' if (n === '.') n = ''
await remoteACL(a, n, p, options) await remoteACL(a, n, options.path, options)
} else { } else {
localACL(a, n, p, options) localACL(a, n, options.path, options)
} }
} else if (options.branch) { } else if (options.branch) {
if (p.startsWith('pear://')) { if (options.path.startsWith('pear://')) {
if (n === '.') n = '' await remoteBranchProtectionRules(a, n, options.path, options)
await remoteBranchProtectionRules(a, n, p, options)
} else { } else {
localBranchProtectionRules(a, n, p, options) localBranchProtectionRules(a, n, options.path, options)
} }
} }
}) })
@@ -127,9 +120,9 @@ program
program program
.command('unshare') .command('unshare')
.description('unshare a gitpear repo') .description('unshare a gitpear repo')
.addArgument(new commander.Argument('[p]', 'path to the repo').default('.')) .option('-p, --path [path]', 'path to the repo', '.')
.action((p, options) => { .action((options) => {
const fullPath = path.resolve(p) const fullPath = path.resolve(options.path)
checkIfGitRepo(fullPath) checkIfGitRepo(fullPath)
const name = fullPath.split(path.sep).pop() const name = fullPath.split(path.sep).pop()
@@ -246,6 +239,7 @@ function localBranchProtectionRules(a, b, p, options) {
} }
function localACL(a, u, p, options) { function localACL(a, u, p, options) {
console.log('localACL', { a, u, p, options })
const fullPath = path.resolve(p) const fullPath = path.resolve(p)
checkIfGitRepo(fullPath) checkIfGitRepo(fullPath)