Skip to content

Commit

Permalink
Merge pull request #61 from orbitdb/pinner/new-refactor
Browse files Browse the repository at this point in the history
refactor: CLI and export core libs for public use.
  • Loading branch information
haydenyoung authored Jul 16, 2024
2 parents 9d0efe5 + f2bd034 commit 93161be
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 99 deletions.
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
"type": "module",
"author": "OrbitDB",
"license": "MIT",
"exports": {
"./orbiter": "./src/orbiter.js",
"./lander": "./src/lander.js"
},
"bin": {
"voyager": "./src/index.js"
"voyager": "./src/bin/cli.js"
},
"dependencies": {
"@chainsafe/libp2p-gossipsub": "^12.0.0",
Expand Down
95 changes: 95 additions & 0 deletions src/bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env node

import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import daemon from './daemon.js'
import RPC from './rpc-client.js'
import { Responses } from './lib/messages/index.js'

yargs(hideBin(process.argv))
.scriptName('voyager')
.command(
'daemon',
'Launch Voyager',
() => {},
async (argv) => {
await daemon({ options: argv })
})
.command('auth', 'Add/remove authorized addresses', yargs => {
yargs
.command(
'add <id>',
'Add an authorized address',
yargs => {
yargs.positional('id', {
describe: 'The id of the user who is allowed to pin one or more databases (or denied depending on default access settings).',
type: 'string'
})
},
async argv => {
const { authAdd } = await RPC(argv)
const res = await authAdd(argv)
if (res.type === Responses.OK) {
console.log('ok')
process.exit(0)
} else {
console.error(res)
process.exit(1)
}
})
.command(
'del <id>',
'Remove an authorized address',
yargs => {
yargs.positional('id', {
describe: 'The id of the user who will no longer be allowed to pin one or more databases (or denied depending on default access settings).',
type: 'string'
})
},
async argv => {
const { authDel } = await RPC(argv)
const res = await authDel(argv)
if (res.type === Responses.OK) {
console.log('ok')
process.exit(0)
} else {
console.error(res)
process.exit(1)
}
})
.command(
'list',
'List authorized addresses',
() => {},
async argv => {
const { authList } = await RPC(argv)
const res = await authList()
if (res.type === Responses.OK) {
for (const id of res.message) {
console.log(id)
}
process.exit(0)
} else {
console.error(res)
process.exit(1)
}
})
.demandCommand(1, 'Error: use add or remove')
})
.option('verbose', {
alias: 'v',
description: 'Be more verbose. Outputs errors and other connection messages. Use multiple -vvv for more verbose logging.',
type: 'count'
})
.option('directory', {
alias: 'd',
type: 'string',
description: 'Specify a directory to store IPFS and OrbitDB data.'
}).option('allow', {
alias: 'a',
type: 'boolean',
description: 'Allow anyone to pin a database. The default is false.'
})
.demandCommand(1, 'Error: specify a command.')
.help()
.parse()
100 changes: 6 additions & 94 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,7 @@
#!/usr/bin/env node
import Orbiter from './lib/orbiter.js'
import Lander from './lib/lander.js'

import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import daemon from './daemon.js'
import RPC from './rpc-client.js'
import { Responses } from './lib/messages/index.js'

yargs(hideBin(process.argv))
.scriptName('voyager')
.command(
'daemon',
'Launch Voyager',
() => {},
async (argv) => {
await daemon({ options: argv })
})
.command('auth', 'Add/remove authorized addresses', yargs => {
yargs
.command(
'add <id>',
'Add an authorized address',
yargs => {
yargs.positional('id', {
describe: 'The id of the user who is allowed to pin one or more databases (or denied depending on default access settings).',
type: 'string'
})
},
async argv => {
const { authAdd } = await RPC(argv)
const res = await authAdd(argv)
if (res.type === Responses.OK) {
console.log('ok')
process.exit(0)
} else {
console.error(res)
process.exit(1)
}
})
.command(
'del <id>',
'Remove an authorized address',
yargs => {
yargs.positional('id', {
describe: 'The id of the user who will no longer be allowed to pin one or more databases (or denied depending on default access settings).',
type: 'string'
})
},
async argv => {
const { authDel } = await RPC(argv)
const res = await authDel(argv)
if (res.type === Responses.OK) {
console.log('ok')
process.exit(0)
} else {
console.error(res)
process.exit(1)
}
})
.command(
'list',
'List authorized addresses',
() => {},
async argv => {
const { authList } = await RPC(argv)
const res = await authList()
if (res.type === Responses.OK) {
for (const id of res.message) {
console.log(id)
}
process.exit(0)
} else {
console.error(res)
process.exit(1)
}
})
.demandCommand(1, 'Error: use add or remove')
})
.option('verbose', {
alias: 'v',
description: 'Be more verbose. Outputs errors and other connection messages. Use multiple -vvv for more verbose logging.',
type: 'count'
})
.option('directory', {
alias: 'd',
type: 'string',
description: 'Specify a directory to store IPFS and OrbitDB data.'
}).option('allow', {
alias: 'a',
type: 'boolean',
description: 'Allow anyone to pin a database. The default is false.'
})
.demandCommand(1, 'Error: specify a command.')
.help()
.parse()
export {
Orbiter,
Lander
}

0 comments on commit 93161be

Please sign in to comment.