cli: path as option; lint; quit option

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-15 18:58:07 +00:00
parent 7e53bfb9bb
commit ffaecc0382
15 changed files with 104 additions and 118 deletions

View File

@@ -3,17 +3,17 @@ const fs = require('fs')
const ROLES = {
owner: {
description: 'Read and write to all branches, and ACL management',
description: 'Read and write to all branches, and ACL management'
},
admin: {
description: 'Read and write to all branches',
description: 'Read and write to all branches'
},
contributor: {
description: 'Read and write to all branches except protected ones',
description: 'Read and write to all branches except protected ones'
},
viewer: {
description: 'Read all branches',
},
description: 'Read all branches'
}
}
const DEFAULT_ACL = {
visibility: 'public', // public|private
@@ -127,5 +127,5 @@ module.exports = {
getAdmins,
getContributors,
getViewers,
revokeAccessFromUser,
revokeAccessFromUser
}

View File

@@ -1,5 +1,5 @@
function getId (data) {
if (!process.env.GIT_PEAR_AUTH) return payload
if (!process.env.GIT_PEAR_AUTH) return data
if (process.env.GIT_PEAR_AUTH === 'nip98') {
const nip98 = require('./nip98')
return nip98.getId(data)
@@ -7,7 +7,7 @@ function getId(data) {
}
async function getToken (payload) {
if (!process.env.GIT_PEAR_AUTH) return userId
if (!process.env.GIT_PEAR_AUTH) return payload
if (process.env.GIT_PEAR_AUTH === 'nip98') {
const nip98 = require('./nip98')
return nip98.getToken(payload)

View File

@@ -6,7 +6,6 @@ const commander = require('commander')
const program = new commander.Command()
const path = require('path')
const fs = require('fs')
const home = require('./home')
const acl = require('./acl')
@@ -25,10 +24,9 @@ program
program
.command('init')
.description('initialize a gitpear repo')
.option('-p, --path [path]', 'paht to the git repo', '.')
.option('-s, --share [branch]', 'share the repo as public, default false, default branch is current', '')
.action(async (options) => {
const fullPath = path.resolve(options.path)
const fullPath = path.resolve('.')
checkIfGitRepo(fullPath)
const name = fullPath.split(path.sep).pop()
@@ -67,9 +65,8 @@ program
.description('share a gitpear repo')
.option('-b, --branch [b]', 'branch to share, default is current branch', '')
.option('-v, --visibility [v]', 'visibility of the repo', 'public')
.option('-p, --path [path]', 'path to the repo', '.')
.action(async (options) => {
const fullPath = path.resolve(options.path)
const fullPath = path.resolve('.')
checkIfGitRepo(fullPath)
const name = fullPath.split(path.sep).pop()
@@ -116,7 +113,6 @@ program
}
})
program
.command('unshare')
.description('unshare a gitpear repo')
@@ -239,7 +235,6 @@ function localBranchProtectionRules(a, b, p, options) {
}
function localACL (a, u, p, options) {
console.log('localACL', { a, u, p, options })
const fullPath = path.resolve(p)
checkIfGitRepo(fullPath)
@@ -290,7 +285,6 @@ function localACL(a, u, p, options) {
acl.revokeAccessFromUser(name, u)
console.log(`Removed ${u} from ${name}`)
return
}
}

View File

@@ -1,6 +1,5 @@
#!/usr/bin/env node
const { spawn } = require('child_process')
const ProtomuxRPC = require('protomux-rpc')
const RAM = require('random-access-memory')
@@ -14,8 +13,6 @@ const home = require('./home')
const auth = require('./auth')
const acl = require('./acl')
const fs = require('fs')
const url = process.argv[3]
const matches = url.match(/pear:\/\/([a-f0-9]{64})\/(.*)/)
@@ -99,7 +96,7 @@ async function talkToGit (refs, drive, repoName, rpc, commit) {
process.stdout.write('push\n')
process.stdout.write('fetch\n\n')
} else if (chunk && chunk.search(/^push/) !== -1) {
const [_command, path] = chunk.split(' ')
const path = chunk.split(' ')[1]
let [src, dst] = path.split(':')
const isDelete = !src
@@ -107,13 +104,6 @@ async function talkToGit (refs, drive, repoName, rpc, commit) {
dst = dst.replace('refs/heads/', '').replace('\n\n', '')
try { home.createAppFolder(repoName) } catch (e) { }
try { await git.createBareRepo(repoName) } catch (e) { }
try { await git.addRemote(repoName) } catch (e) { }
try { await git.push(dst) } catch (e) { }
try { home.shareAppFolder(repoName) } catch (e) { }
try { acl.setACL(repoName, acl.getACL(repoName)) } catch (e) { }
let method
if (isDelete) {
method = 'd-branch'
@@ -128,16 +118,25 @@ async function talkToGit (refs, drive, repoName, rpc, commit) {
method = 'push'
}
try { home.createAppFolder(repoName) } catch (e) { }
try { await git.createBareRepo(repoName) } catch (e) { }
try { await git.addRemote(repoName, { quite: true }) } catch (e) { }
try { await git.push(dst, isForce) } catch (e) { }
try { home.shareAppFolder(repoName) } catch (e) { }
try { acl.setACL(repoName, acl.getACL(repoName)) } catch (e) { }
const publicKey = home.readPk()
let payload = { body: {
const payload = {
body: {
url: `pear://${publicKey}/${repoName}`,
data: `${dst}#${commit}`,
method
} }
}
}
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body)
}
const res = await rpc.request(method, Buffer.from(JSON.stringify(payload)))
await rpc.request(method, Buffer.from(JSON.stringify(payload)))
process.stdout.write('\n\n')
process.exit(0)

View File

@@ -64,9 +64,9 @@ async function createBareRepo (name) {
return await doGit(init)
}
async function addRemote (name) {
async function addRemote (name, opts = { quiet: false }) {
const init = spawn('git', ['remote', 'add', 'pear', getCodePath(name)])
return await doGit(init)
return await doGit(init, opts)
}
async function push (branch = 'master', force = false) {
@@ -76,8 +76,8 @@ async function push (branch = 'master', force = false) {
return await doGit(push)
}
async function doGit (child) {
child.stderr.pipe(process.stderr)
async function doGit (child, opts = { quiet: false }) {
if (!opts.quiet) child.stderr.pipe(process.stderr)
return new Promise((resolve, reject) => {
child.on('close', (code) => {
if (code) {
@@ -229,5 +229,5 @@ module.exports = {
addRemote,
push,
getCommit,
getCurrentBranch,
getCurrentBranch
}

View File

@@ -2,14 +2,14 @@ const ACL = require('../acl')
const home = require('../home')
async function getACLHandler (publicKey, req) {
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req)
const { repoName } = await parseACLRequest.bind(this)(publicKey, req)
const repoACL = ACL.getACL(repoName)
return Buffer.from(JSON.stringify(repoACL))
}
async function addACLHandler (publicKey, req) {
const { repoName, userId, acl, isBranch, name } = await parseACLRequest.bind(this)(publicKey, req)
const { repoName, isBranch, name } = await parseACLRequest.bind(this)(publicKey, req)
isBranch ? ACL.addProtectedBranch(repoName, name) : ACL.grantAccessToUser(repoName, ...name.split(':'))
@@ -18,7 +18,7 @@ async function addACLHandler (publicKey, req) {
}
async function delACLHandler (publicKey, req) {
const { repoName, userId, acl, isBranch, name } = await parseACLRequest.bind(this)(publicKey, req)
const { repoName, isBranch, name } = await parseACLRequest.bind(this)(publicKey, req)
isBranch ? ACL.removeProtectedBranch(repoName, name) : ACL.revokeAccessFromUser(repoName, name)
@@ -42,12 +42,12 @@ async function parseACLRequest(publicKey, req) {
name: request.body.name,
userId,
acl: request.body.acl,
isBranch: !!request.body.branch,
isBranch: !!request.body.branch
}
}
module.exports = {
getACLHandler,
addACLHandler,
delACLHandler,
delACLHandler
}

View File

@@ -3,12 +3,10 @@ const home = require('../home')
const { spawn } = require('child_process')
async function getReposHandler (publicKey, req) {
const { branch, url, userId } = await parseReq.bind(this)(publicKey, req)
const { userId } = await parseReq.bind(this)(publicKey, req)
const res = {}
for (const repoName in this.repositories) {
// TODO: add only public repos and those which are shared with the peer
// Alternatively return only requested repo
const isPublic = (ACL.getACL(repoName).visibility === 'public')
if (isPublic || ACL.getViewers(repoName).includes(userId)) {
res[repoName] = this.drives[repoName].key.toString('hex')
@@ -18,7 +16,7 @@ async function getReposHandler (publicKey, req) {
}
async function getRefsHandler (publicKey, req) {
const { repoName, branch, url, userId } = await parseReq.bind(this)(publicKey, req)
const { repoName, userId } = await parseReq.bind(this)(publicKey, req)
const res = this.repositories[repoName]
const isPublic = (ACL.getACL(repoName).visibility === 'public')
@@ -43,14 +41,7 @@ async function pushHandler (publicKey, req) {
return await new Promise((resolve, reject) => {
const env = { ...process.env, GIT_DIR: home.getCodePath(repoName) }
const child = spawn('git', ['fetch', url, `${branch}:${branch}`], { env })
let errBuffer = Buffer.from('')
child.stderr.on('data', data => {
errBuffer = Buffer.concat([errBuffer, data])
})
child.on('close', code => {
return code === 0 ? resolve(errBuffer) : reject(errBuffer)
})
return doGit(child, resolve, reject)
})
}
@@ -68,19 +59,12 @@ async function forcePushHandler (publicKey, req) {
return await new Promise((resolve, reject) => {
const env = { ...process.env, GIT_DIR: home.getCodePath(repoName) }
const child = spawn('git', ['fetch', url, `${branch}:${branch}`, '--force'], { env })
let errBuffer = Buffer.from('')
child.stderr.on('data', data => {
errBuffer = Buffer.concat([errBuffer, data])
})
child.on('close', code => {
return code === 0 ? resolve(errBuffer) : reject(errBuffer)
})
return doGit(child, resolve, reject)
})
}
async function deleteBranchHandler (publicKey, req) {
const { url, repoName, branch, userId } = await parseReq.bind(this)(publicKey, req)
const { repoName, branch, userId } = await parseReq.bind(this)(publicKey, req)
const isContributor = ACL.getContributors(repoName).includes(userId)
if (!isContributor) throw new Error('You are not allowed to push to this repo')
@@ -93,14 +77,7 @@ async function deleteBranchHandler (publicKey, req) {
return await new Promise((resolve, reject) => {
const env = { ...process.env, GIT_DIR: home.getCodePath(repoName) }
const child = spawn('git', ['branch', '-D', branch], { env })
let errBuffer = Buffer.from('')
child.stderr.on('data', data => {
errBuffer = Buffer.concat([errBuffer, data])
})
child.on('close', code => {
return code === 0 ? resolve(errBuffer) : reject(errBuffer)
})
return doGit(child, resolve, reject)
})
}
@@ -111,16 +88,35 @@ async function parseReq(publicKey, req) {
repoName: request.body.url?.split('/')?.pop(),
branch: request.body.data?.split('#')[0],
url: request.body.url,
userId: await this.authenticate(publicKey, request),
userId: await this.authenticate(publicKey, request)
}
return parsed
}
function doGit (child, resolve, reject) {
let errBuffer = Buffer.from('')
let outBuffer = Buffer.from('')
child.stdout.on('data', data => {
outBuffer = Buffer.concat([outBuffer, data])
})
child.stderr.on('data', data => {
errBuffer = Buffer.concat([errBuffer, data])
})
child.on('close', code => {
console.error('errBuffer', errBuffer.toString())
console.log('outBuffer', outBuffer.toString())
return code === 0 ? resolve(outBuffer) : reject(errBuffer)
})
}
module.exports = {
getReposHandler,
getRefsHandler,
pushHandler,
forcePushHandler,
deleteBranchHandler,
deleteBranchHandler
}

View File

@@ -3,5 +3,5 @@ const acl = require('./acl')
module.exports = {
git,
acl,
acl
}

View File

@@ -84,7 +84,7 @@ async function wrapper (url, name, opts = {}, cb) {
swarm.on('connection', async (socket) => {
const rpc = new ProtomuxRPC(socket)
let payload = { body: { url, method: 'get-repos' } }
const payload = { body: { url, method: 'get-repos' } }
if (!process.env.GIT_PEAR_AUTH) {
console.debug('Retreiving data using un-authenticated access')
} else {
@@ -112,5 +112,5 @@ async function wrapper (url, name, opts = {}, cb) {
module.exports = {
list: (url, name, opts) => wrapper(url, name, opts, list),
add: (url, name, opts) => wrapper(url, name, opts, add),
remove: (url, name, opts) => wrapper(url, name, opts, del),
remove: (url, name, opts) => wrapper(url, name, opts, del)
}

View File

@@ -3,5 +3,5 @@ const aclRemote = require('./acl-remote')
module.exports = {
listRemote,
aclRemote,
aclRemote
}

View File

@@ -28,7 +28,7 @@ module.exports = async function listRemote (url) {
swarm.on('connection', async (socket) => {
const rpc = new ProtomuxRPC(socket)
let payload = { body: { url, method: 'get-repos' } }
const payload = { body: { url, method: 'get-repos' } }
if (!process.env.GIT_PEAR_AUTH) {
console.debug('Retreiving data using un-authenticated access')
} else {

View File

@@ -1,7 +1,4 @@
const ProtomuxRPC = require('protomux-rpc')
const SecretStream = require('@hyperswarm/secret-stream')
const { spawn } = require('child_process')
const home = require('./home')
const auth = require('./auth')
const { git, acl } = require('./rpc-handlers')

View File

@@ -32,5 +32,5 @@ module.exports = {
printACL,
printACLForUser,
checkIfGitRepo,
logBranches,
logBranches
}