Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI pear data reset #596

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
76eacb6
CLI: `pear data` shows PLATFORM_DIR
AndreiRegiani Feb 5, 2025
cf0fac7
Revert changes
AndreiRegiani Feb 5, 2025
396968a
Add cmd/data reset
AndreiRegiani Feb 6, 2025
89b0903
cmd/data reset function
AndreiRegiani Feb 10, 2025
449f09a
add npm holepunchto/pear-ipc#data-reset
AndreiRegiani Feb 12, 2025
29df8b0
ops/data/reset.js
AndreiRegiani Feb 13, 2025
5e851ce
Handle failure
AndreiRegiani Feb 13, 2025
8e5aff8
Merge branch 'main' into cli-data-platform-dir
AndreiRegiani Feb 13, 2025
13bb909
dataReset IPC
AndreiRegiani Feb 13, 2025
5b41bb9
Refinements
AndreiRegiani Feb 13, 2025
342b378
Fix import issue
AndreiRegiani Feb 13, 2025
0ab15e6
remove lock.flush()
AndreiRegiani Feb 13, 2025
5ccd4d6
Alignments
AndreiRegiani Feb 16, 2025
2d86ea5
fix fs.rm() with fs.promises.rm
AndreiRegiani Feb 17, 2025
3b4826a
fix Failure status
AndreiRegiani Feb 18, 2025
042c837
Simplify
AndreiRegiani Feb 18, 2025
794ac34
--yes
AndreiRegiani Feb 18, 2025
3caca34
info constants, pick() function
AndreiRegiani Feb 18, 2025
9707ffc
pear info --constants
AndreiRegiani Feb 18, 2025
eb825b3
Merge branch 'main' into cli-data-platform-dir
AndreiRegiani Feb 18, 2025
a7a258e
[ No results ]
AndreiRegiani Feb 18, 2025
542e808
Merge branch 'cli-data-platform-dir' of https://github.com/holepuncht…
AndreiRegiani Feb 18, 2025
16d6928
noResults const
AndreiRegiani Feb 18, 2025
08b280d
cmd/ PLATFORM_HYPERDB
AndreiRegiani Feb 19, 2025
f67efa5
scrap out --constants
AndreiRegiani Feb 19, 2025
f878cfb
remove showConstants
AndreiRegiani Feb 19, 2025
14d1e1c
linter
AndreiRegiani Feb 19, 2025
8b14f71
placeholder
AndreiRegiani Feb 19, 2025
5ced115
trace CLOSE
AndreiRegiani Feb 19, 2025
f521228
.length is safe
AndreiRegiani Feb 19, 2025
133a957
"pear-ipc": "^3.3.0"
AndreiRegiani Feb 19, 2025
fecffcb
remove await from this.ipc.data
AndreiRegiani Feb 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions cmd/data.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict'
const parseLink = require('../lib/parse-link')
const { outputter, ansi } = require('./iface')
const { outputter, ansi, confirm, status } = require('./iface')
const { ERR_INVALID_INPUT } = require('../errors')
const { PLATFORM_HYPERDB } = require('../constants')

const padding = ' '
const placeholder = '[ No results ]\n'

const appsOutput = (bundles) => {
if (!bundles.length) return placeholder
let out = ''
for (const bundle of bundles) {
out += `- ${ansi.bold(bundle.link)}\n`
Expand All @@ -20,6 +23,7 @@ const appsOutput = (bundles) => {
}

const dhtOutput = (nodes) => {
if (!nodes.length) return placeholder
let out = ''
for (const node of nodes) {
out += `${node.host}${ansi.dim(`:${node.port}`)}\n`
Expand All @@ -28,6 +32,7 @@ const dhtOutput = (nodes) => {
}

const gcOutput = (records) => {
if (!records.length) return placeholder
let out = ''
for (const gc of records) {
out += `- ${ansi.bold(gc.path)}\n`
Expand Down Expand Up @@ -56,25 +61,54 @@ class Data {
if (link) {
const parsed = parseLink(link)
if (!parsed) throw ERR_INVALID_INPUT(`Link "${link}" is not a valid key`)
const result = await this.ipc.data({ resource: 'link', secrets, link })
const result = this.ipc.data({ resource: 'link', secrets, link })
await output(json, result, { tag: 'link' }, this.ipc)
} else {
const result = await this.ipc.data({ resource: 'apps', secrets })
const result = this.ipc.data({ resource: 'apps', secrets })
await output(json, result, { tag: 'apps' }, this.ipc)
}
}

async dht (cmd) {
const { command } = cmd
const { json } = command.parent.flags
const result = await this.ipc.data({ resource: 'dht' })
const result = this.ipc.data({ resource: 'dht' })
await output(json, result, { tag: 'dht' }, this.ipc)
}

async gc (cmd) {
const { command } = cmd
const { json } = command.parent.flags
const result = await this.ipc.data({ resource: 'gc' })
const result = this.ipc.data({ resource: 'gc' })
await output(json, result, { tag: 'gc' }, this.ipc)
}

async reset (cmd) {
const { command } = cmd
const { yes } = command.flags

if (!yes) {
const dialog = `${ansi.warning} Clearing database ${ansi.bold(PLATFORM_HYPERDB)}\n\n`
const ask = 'Type DELETE to confirm'
const delim = '?'
const validation = (val) => val === 'DELETE'
const msg = '\n' + ansi.cross + ' uppercase DELETE to confirm\n'
await confirm(dialog, ask, delim, validation, msg)
}

const complete = await pick(this.ipc.dataReset(), (tag) => tag === 'complete')
complete ? status('Success\n', true) : status('Failure (ipc.dataReset)\n', false)
}
}

function pick (stream, predicate) {
return new Promise((resolve, reject) => {
stream.on('error', reject)
const listener = ({ tag, data }) => {
if (!predicate(tag)) return
resolve(data)
stream.off('data', listener)
}
stream.on('data', listener)
})
}
3 changes: 3 additions & 0 deletions cmd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ module.exports = async (ipc, argv = Bare.argv.slice(1)) => {
command('apps', summary('Installed apps'), arg('[link]', 'Filter by Pear link'), (cmd) => runners.data(ipc).apps(cmd)),
command('dht', summary('DHT known-nodes cache'), (cmd) => runners.data(ipc).dht(cmd)),
command('gc', summary('Garbage collection records'), (cmd) => runners.data(ipc).gc(cmd)),
command('reset', summary('Advanced. Clear local database'),
flag('--yes', 'Skip confirmation prompt'), (cmd) => runners.data(ipc).reset(cmd)
),
flag('--secrets', 'Show sensitive information, i.e. encryption-keys'),
flag('--json', 'Newline delimited JSON output'),
() => { console.log(data.help()) }
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"paparam": "^1.6.0",
"pear-changelog": "^1.0.1",
"pear-interface": "^1.0.0",
"pear-ipc": "^3.1.0",
"pear-ipc": "^3.3.0",
"pear-link": "^2.1.1",
"pear-updater": "^3.4.3",
"pear-updater-bootstrap": "^1.2.0",
Expand Down
5 changes: 4 additions & 1 deletion subsystems/sidecar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const ops = {
Shift: require('./ops/shift'),
Reset: require('./ops/reset'),
Touch: require('./ops/touch'),
Data: require('./ops/data')
Data: require('./ops/data'),
DataReset: require('./ops/data/reset')
}

// ensure that we are registered as a link handler
Expand Down Expand Up @@ -368,6 +369,8 @@ class Sidecar extends ReadyResource {

data (params, client) { return new ops.Data(params, client, this) }

dataReset (params, client) { return new ops.DataReset(params, client, this) }

shift (params, client) { return new ops.Shift(params, client, this) }

reset (params, client) { return new ops.Reset(params, client, this) }
Expand Down
13 changes: 13 additions & 0 deletions subsystems/sidecar/lib/model.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
'use strict'
const fs = require('bare-fs')
const HyperDB = require('hyperdb')
const DBLock = require('db-lock')
const dbSpec = require('../../../spec/db')
const { PLATFORM_HYPERDB } = require('../../../constants')

module.exports = class Model {
constructor () {
this.init()
}

init () {
LOG.trace('db', `ROCKS ('${PLATFORM_HYPERDB}')`)
this.db = HyperDB.rocks(PLATFORM_HYPERDB, dbSpec)

this.lock = new DBLock({
Expand Down Expand Up @@ -145,6 +151,13 @@ module.exports = class Model {
}

async close () {
LOG.trace('db', 'CLOSE')
await this.db.close()
}

async reset () {
await this.close()
await fs.promises.rm(PLATFORM_HYPERDB, { recursive: true, force: true })
this.init()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
const { pathToFileURL } = require('url-file-url')
const Opstream = require('../lib/opstream')
const Opstream = require('../../lib/opstream')

module.exports = class Data extends Opstream {
constructor (...args) {
Expand Down
13 changes: 13 additions & 0 deletions subsystems/sidecar/ops/data/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'
const Opstream = require('../../lib/opstream')

module.exports = class DataReset extends Opstream {
constructor (...args) {
super((...args) => this.#op(...args), ...args)
}

async #op () {
await this.sidecar.model.reset()
this.push({ tag: 'complete', data: true })
}
}