Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: add size-only flag to cli repo stat command (#3143)
Browse files Browse the repository at this point in the history
Makes `stats repo` an alias for `repo stat` to make formatting the same across commmands in line with go-ipfs.

Also adds `-s` flag to both commands to print just the size info.
  • Loading branch information
achingbrain authored Jul 7, 2020
1 parent 62c1422 commit b4d3bf8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
22 changes: 16 additions & 6 deletions packages/ipfs/src/cli/commands/repo/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ module.exports = {
alias: 'H',
default: false
},
sizeOnly: {
type: 'boolean',
alias: 's',
default: false
},
timeout: {
type: 'string',
coerce: parseDuration
}
},

async handler ({ ctx: { ipfs, print }, human, timeout }) {
async handler ({ ctx: { ipfs, print }, human, sizeOnly, timeout }) {
const stats = await ipfs.repo.stat({
timeout
})
Expand All @@ -31,11 +36,16 @@ module.exports = {
stats.storageMax = prettyBytes(stats.storageMax.toNumber()).toUpperCase()
}

print(
`NumObjects: ${stats.numObjects}
RepoSize: ${stats.repoSize}
if (sizeOnly) {
print(
`RepoSize: ${stats.repoSize}
StorageMax: ${stats.storageMax}`)
} else {
print(`NumObjects: ${stats.numObjects}
RepoSize: ${stats.repoSize}
StorageMax: ${stats.storageMax}
RepoPath: ${stats.repoPath}
Version: ${stats.version}`)
RepoPath: ${stats.repoPath}
Version: ${stats.version}`)
}
}
}
30 changes: 6 additions & 24 deletions packages/ipfs/src/cli/commands/stats/repo.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
'use strict'

const parseDuration = require('parse-duration').default
// This is an alias for `repo stat`.
const repoStats = require('../repo/stat.js')

module.exports = {
command: 'repo',
...repoStats,

describe: 'Get stats for the currently used repo',

builder: {
human: {
type: 'boolean',
default: false
},
timeout: {
type: 'string',
coerce: parseDuration
}
},

async handler ({ ctx: { ipfs, print } }, human, timeout) {
const stats = await ipfs.stats.repo({ human, timeout })
print(`repo status
number of objects: ${stats.numObjects}
repo size: ${stats.repoSize}
repo path: ${stats.repoPath}
version: ${stats.version}
maximum storage: ${stats.storageMax}`)
}
// The command needs to be renamed, else it would be `stats stat` instead of
// `stats repo`
command: 'repo'
}
37 changes: 27 additions & 10 deletions packages/ipfs/test/cli/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,28 @@ describe('repo', () => {
})

const stats = await cli('repo stat', { ipfs })
expect(stats).to.match(/^NumObjects:\s\d+$/m)
expect(stats).to.match(/^RepoSize:\s\d+$/m)
expect(stats).to.match(/^StorageMax:\s\d+$/m)
expect(stats).to.match(/^NumObjects:\s+\d+$/m)
expect(stats).to.match(/^RepoSize:\s+\d+$/m)
expect(stats).to.match(/^StorageMax:\s+\d+$/m)
expect(stats).to.match(/^RepoPath:\s.+$/m)
expect(stats).to.match(/^Version:\s\d+$/m)
expect(stats).to.match(/^Version:\s+\d+$/m)
})

it('get repo stats with just size', async () => {
ipfs.repo.stat.withArgs(defaultOptions).resolves({
numObjects: BigNumber(10),
repoSize: BigNumber(10),
storageMax: BigNumber(10),
repoPath: '/foo',
version: 5
})

const stats = await cli('repo stat -s', { ipfs })
expect(stats).to.not.match(/^NumObjects:$/m)
expect(stats).to.match(/^RepoSize:\s+\d+$/m)
expect(stats).to.match(/^StorageMax:\s+\d+$/m)
expect(stats).to.not.match(/^RepoPath:$/m)
expect(stats).to.not.match(/^Version:$/m)
})

it('get human readable repo stats', async () => {
Expand All @@ -52,11 +69,11 @@ describe('repo', () => {
})

const stats = await cli('repo stat --human', { ipfs })
expect(stats).to.match(/^NumObjects:\s\d+$/m)
expect(stats).to.match(/^NumObjects:\s+\d+$/m)
expect(stats).to.match(/^RepoSize:\s+[\d.]+\s[PTGMK]?B$/gm)
expect(stats).to.match(/^StorageMax:\s+[\d.]+\s[PTGMK]?B$/gm)
expect(stats).to.match(/^RepoPath:\s.+$/m)
expect(stats).to.match(/^Version:\s\d+$/m)
expect(stats).to.match(/^Version:\s+\d+$/m)
})

it('get repo with timeout', async () => {
Expand All @@ -72,11 +89,11 @@ describe('repo', () => {
})

const stats = await cli('repo stat --timeout=1s', { ipfs })
expect(stats).to.match(/^NumObjects:\s\d+$/m)
expect(stats).to.match(/^RepoSize:\s\d+$/m)
expect(stats).to.match(/^StorageMax:\s\d+$/m)
expect(stats).to.match(/^NumObjects:\s+\d+$/m)
expect(stats).to.match(/^RepoSize:\s+\d+$/m)
expect(stats).to.match(/^StorageMax:\s+\d+$/m)
expect(stats).to.match(/^RepoPath:\s.+$/m)
expect(stats).to.match(/^Version:\s\d+$/m)
expect(stats).to.match(/^Version:\s+\d+$/m)
})
})

Expand Down

0 comments on commit b4d3bf8

Please sign in to comment.