Skip to content

Commit

Permalink
Cleanup cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Apr 2, 2024
1 parent f854655 commit 62c5575
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 109 deletions.
17 changes: 7 additions & 10 deletions packages/cli/src/commands/get-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ export const handler = createHandler<typeof builder>(async function * (argv) {
return
}

yield * data.map(d => {
const from = `${d.from}`.padEnd(15)
const to = `${d.to}`.padEnd(15)

const context = Object.entries(d.context)
.map(([key, value]) => `${key}: ${value}`)
.join(', ')

return `${from}${to}{${context}}`
})
for (const item of data) {
yield [
`${item.from}`.padEnd(15),
`${item.to}`.padEnd(15),
`{${Object.entries(item.context).map(([key, value]) => `${key}: ${value}`).join(', ')}}`
].join('')
}
})
24 changes: 10 additions & 14 deletions packages/cli/src/commands/list-groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ export const handler = createHandler<typeof builder>(async function * (argv) {

const getItemCount = (group: string): number => items.find(i => i.group === group)?.items?.length ?? 0

let out = `${'Name'.padEnd(34)}${'Items'.padEnd(10)}${'Peers'.padEnd(10)}${'CID'.padEnd(62)}\n`

out += groups.map(({ group, name }) => {
let str = ''

str += name.slice(0, 32).padEnd(34)
str += `${getItemCount(group)}`.slice(0, 8).padEnd(10)
str += `${getPeerCount(group)}`.padEnd(10)
str += group.padEnd(62)

return str
}).join('\n')

yield out
yield `${'Name'.padEnd(34)}${'Items'.padEnd(10)}${'Peers'.padEnd(10)}${'CID'.padEnd(62)}`

for (const { group, name } of groups) {
yield [
name.slice(0, 32).padEnd(34),
`${getItemCount(group)}`.slice(0, 8).padEnd(10),
`${getPeerCount(group)}`.padEnd(10),
group.padEnd(62)
].join('')
}
})
77 changes: 37 additions & 40 deletions packages/cli/src/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,61 +117,58 @@ export const handler = createHandler<typeof builder>(async function * (argv) {
header += 'R-Strategy'.padEnd(12)
header += 'CID'.padEnd(62)

let response = `${header}\n`
yield header

const printTree = (tree: JStruct, depth: number = 0): void => {
const printTree = function * (tree: JStruct, depth: number = 0): Generator<string> {
if (depth === 0) {
response += '/\n'
printTree(tree, 1)
yield '/'
yield * printTree(tree, 1)
return
}

for (const [key, subtree] of Object.entries(tree)) {
try {
const [item] = List.Return.parse([subtree])
const timeRemaining = Math.ceil((item.size - getStatus(item).size) / (getSpeed(item) * 1000))
let str = ''

str += `${' '.repeat(depth)}${key}`.slice(0, 18).padEnd(20)
str += `${formatSize(getStatus(item).size)}/${formatSize(item.size)} (${formatPercent(getStatus(item).size / item.size)})`.slice(0, 25).padEnd(27)
str += `${formatSize(getSpeed(item) * 1000)}/s ${isNaN(timeRemaining) ? '' : `(${timeRemaining} s)`}`.slice(0, 25).padEnd(27)
str += `${getStatus(item).blocks}/${item.blocks} (${formatPercent(getStatus(item).blocks / item.blocks)})`.slice(0, 18).padEnd(20)
str += getStatus(item).state.slice(0, 13).padEnd(15)
str += `${item.priority}`.slice(0, 8).padEnd(10)
str += `${revisionCounter[item.path] ?? 0}`.slice(0, 8).padEnd(10)
str += `${getPeers(item)}`.slice(0, 8).padEnd(10)
str += `${item.group}`.slice(0, 8).padEnd(10)
str += `${item.encrypted}`.slice(0, 8).padEnd(10)
str += `${item.revisionStrategy}`.slice(0, 8).padEnd(12)
str += item.cid.padEnd(62)

response += `${str}\n`

yield [
`${' '.repeat(depth)}${key}`.slice(0, 18).padEnd(20),
`${formatSize(getStatus(item).size)}/${formatSize(item.size)} (${formatPercent(getStatus(item).size / item.size)})`.slice(0, 25).padEnd(27),
`${formatSize(getSpeed(item) * 1000)}/s ${isNaN(timeRemaining) ? '' : `(${timeRemaining} s)`}`.slice(0, 25).padEnd(27),
`${getStatus(item).blocks}/${item.blocks} (${formatPercent(getStatus(item).blocks / item.blocks)})`.slice(0, 18).padEnd(20),
getStatus(item).state.slice(0, 13).padEnd(15),
`${item.priority}`.slice(0, 8).padEnd(10),
`${revisionCounter[item.path] ?? 0}`.slice(0, 8).padEnd(10),
`${getPeers(item)}`.slice(0, 8).padEnd(10),
`${item.group}`.slice(0, 8).padEnd(10),
`${item.encrypted}`.slice(0, 8).padEnd(10),
`${item.revisionStrategy}`.slice(0, 8).padEnd(12),
item.cid.padEnd(62)
].join('')

continue
} catch (error) {
// Ignore
}

response += `${' '.repeat(depth)}${key}/\n`

printTree(subtree as JStruct, depth + 1)
yield `${' '.repeat(depth)}${key}/`
yield * printTree(subtree as JStruct, depth + 1)
}
}

printTree(createJSON(items))

let footer = '\n'

footer += 'Total'.padEnd(15)
footer += 'Size'.padEnd(25)
footer += 'Blocks'.padEnd(20)
footer += 'Speed'.padEnd(20)
footer += '\n'
footer += `${completed.count}/${total.count} (${formatPercent(completed.count / total.count)})`.slice(0, 13).padEnd(15)
footer += `${formatSize(completed.size)}/${formatSize(total.size)} (${formatPercent(completed.size / total.size)})`.slice(0, 23).padEnd(25)
footer += `${completed.blocks}/${total.blocks} (${formatPercent(completed.blocks / total.blocks)})`.slice(0, 18).padEnd(20)
footer += `${formatSize(speeds.reduce((a, c) => a + c.speed, 0) * 1000)}s`.slice(0, 18).padEnd(20)

response += `${footer}`

yield response
yield * printTree(createJSON(items))
yield ''
yield [
'Total'.padEnd(15),
'Size'.padEnd(25),
'Blocks'.padEnd(20),
'Speed'.padEnd(20)
].join('')

yield [
`${completed.count}/${total.count} (${formatPercent(completed.count / total.count)})`.slice(0, 13).padEnd(15),
`${formatSize(completed.size)}/${formatSize(total.size)} (${formatPercent(completed.size / total.size)})`.slice(0, 23).padEnd(25),
`${completed.blocks}/${total.blocks} (${formatPercent(completed.blocks / total.blocks)})`.slice(0, 18).padEnd(20),
`${formatSize(speeds.reduce((a, c) => a + c.speed, 0) * 1000)}s`.slice(0, 18).padEnd(20)
].join('')
})
4 changes: 2 additions & 2 deletions packages/cli/test/addresses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ describe('addresses', () => {
const params = mockParams({ addresses })
const response = await all(handler(params))

assert.equal(response.join('\n'), addresses.join('\n'))
assert.deepEqual(response, addresses)
})

it('json', async () => {
const params = mockParams({ addresses }, { json: true })
const response = await all(handler(params))

assert.deepEqual(response.join('\n'), JSON.stringify(addresses))
assert.equal(response.join('\n'), JSON.stringify(addresses))
})
})
2 changes: 1 addition & 1 deletion packages/cli/test/connections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('connections', () => {
const params = mockParams({ connections })
const response = await all(handler(params))

assert.equal(response.join('\n'), connections.join('\n'))
assert.deepEqual(response, connections)
})

it('json', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/delete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('delete', () => {
const params = mockParams({ delete: items }, { path: 'name-abc', group: 'group-abc' })
const response = await all(handler(params))

assert.equal(response.join('\n'), items.map(i => i.path).join('\n'))
assert.deepEqual(response, items.map(i => i.path))
})

it('json', async () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/test/get-schedule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('get-schedule', () => {

const response = await all(handler(params))

assert.equal(response.join('\n'), items.map(d => {
const expected = items.map(d => {
const from = `${d.from}`.padEnd(15)
const to = `${d.to}`.padEnd(15)

Expand All @@ -33,7 +33,9 @@ describe('get-schedule', () => {
.join(', ')

return `${from}${to}{${context}}`
}).join('\n'))
})

assert.deepEqual(response, expected)
})

it('json', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('import', () => {

const response = await all(handler(params))

assert.equal(response.join('\n'), imports.map(i => `${i.path} ${i.cid}`).join('\n'))
assert.deepEqual(response, imports.map(i => `${i.path} ${i.cid}`))
})

it('json', async () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/cli/test/list-groups.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ describe('list-groups', () => {
it('text', async () => {
const params = mockParams({ listGroups: groups, countPeers: [], list: [] }, { group: 'group-abc' })

const response = await all(handler(params))
const [header, ...response] = await all(handler(params))

assert.equal(
header,
`${'Name'.padEnd(34)}${'Items'.padEnd(10)}${'Peers'.padEnd(10)}${'CID'.padEnd(62)}`
)

assert.equal(response.join('\n'), `${'Name'.padEnd(34)}${'Items'.padEnd(10)}${'Peers'.padEnd(10)}${'CID'.padEnd(62)}\n${groups.map(({ name, group }) => `${name.padEnd(34)}${'0'.padEnd(10)}${'0'.padEnd(10)}${group.padEnd(62)}`).join('\n')}`)
assert.deepEqual(
response,
groups.map(({ name, group }) => `${name.padEnd(34)}${'0'.padEnd(10)}${'0'.padEnd(10)}${group.padEnd(62)}`)
)
})

it('json', async () => {
Expand Down
77 changes: 42 additions & 35 deletions packages/cli/test/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,49 @@ describe('list', () => {

const response = await all(handler(params))

let expected = 'Name'.padEnd(20)
const expected = [
[
'Name'.padEnd(20),
'Size'.padEnd(27),
'Speed'.padEnd(27),
'Blocks'.padEnd(20),
'State'.padEnd(15),
'Priority'.padEnd(10),
'Revisions'.padEnd(10),
'Peers'.padEnd(10),
'Group'.padEnd(10),
'Encrypted'.padEnd(10),
'R-Strategy'.padEnd(12),
'CID'.padEnd(62)
].join(''),
'/',
' my-dir/',
[
' file ',
'50 B/500 B (10%) ',
'1000 KB/s (1 s) ',
'5/50 (10%) ',
'COMPLETED ',
'1 0 1 ',
'QmaCpDMG false all ',
'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN '
].join(''),
'',
[
'Total ',
'Size ',
'Blocks ',
'Speed '
].join(''),
[
'1/1 (100%) ',
'50 B/500 B (10%) ',
'5/50 (10%) ',
'1000 KBs '
].join('')
]

expected += 'Size'.padEnd(27)
expected += 'Speed'.padEnd(27)
expected += 'Blocks'.padEnd(20)
expected += 'State'.padEnd(15)
expected += 'Priority'.padEnd(10)
expected += 'Revisions'.padEnd(10)
expected += 'Peers'.padEnd(10)
expected += 'Group'.padEnd(10)
expected += 'Encrypted'.padEnd(10)
expected += 'R-Strategy'.padEnd(12)
expected += 'CID'.padEnd(62)
expected += '\n'
expected += '/\n'
expected += ' my-dir/\n'
expected += ' file'
expected += ' 50 B/500 B (10%)'
expected += ' 1000 KB/s (1 s)'
expected += ' 5/50 (10%)'
expected += ' COMPLETED'
expected += ' 1 0 1'
expected += ' QmaCpDMG false all'
expected += ' QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN'
expected += ' \n\n'
expected += 'Total'
expected += ' Size'
expected += ' Blocks'
expected += ' Speed'
expected += ' \n'
expected += '1/1 (100%)'
expected += ' 50 B/500 B (10%)'
expected += ' 5/50 (10%)'
expected += ' 1000 KBs '

assert.equal(response.join('\n'), expected)
assert.deepEqual(response, expected)
})

it('json', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/revisions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('revisions', () => {
const params = mockParams({ listRevisions: items }, { path: 'name-abc', group: 'group-abc' })
const response = await all(handler(params))

assert.equal(response.join('\n'), items.map(i => `${i.sequence}: ${i.cid}`).join('\n'))
assert.deepEqual(response, items.map(i => `${i.sequence}: ${i.cid}`))
})

it('json', async () => {
Expand Down

0 comments on commit 62c5575

Please sign in to comment.