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

Commit

Permalink
feat: add human flag to repo stat cli command (#2630)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Santos authored and Alan Shaw committed Nov 27, 2019
1 parent 44579fd commit 39bc5b4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"ipfs-http-response": "~0.4.0",
"ipfs-mfs": "^0.13.0",
"ipfs-multipart": "^0.2.0",
"ipfs-repo": "^0.29.0",
"ipfs-repo": "github:ipfs/js-ipfs-repo#feat/remove-options-object-from-stat",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "^0.38.0",
"ipfs-unixfs-importer": "^0.40.0",
Expand Down
27 changes: 19 additions & 8 deletions src/cli/commands/repo/stat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const prettyBytes = require('pretty-bytes')

module.exports = {
command: 'stat',

Expand All @@ -15,14 +17,23 @@ module.exports = {

handler (argv) {
argv.resolve((async () => {
const ipfs = await argv.getIpfs()
const stats = await ipfs.repo.stat({ human: argv.human })
argv.print(`repo status
number of objects: ${stats.numObjects}
repo size: ${stats.repoSize}
repo path: ${stats.repoPath}
version: ${stats.version}
maximum storage: ${stats.storageMax}`)
const { getIpfs, human } = argv

const ipfs = await getIpfs()
const stats = await ipfs.repo.stat()

if (human) {
stats.numObjects = stats.numObjects.toNumber()
stats.repoSize = prettyBytes(stats.repoSize.toNumber()).toUpperCase()
stats.storageMax = prettyBytes(stats.storageMax.toNumber()).toUpperCase()
}

argv.print(
`NumObjects: ${stats.numObjects}
RepoSize: ${stats.repoSize}
StorageMax: ${stats.storageMax}
RepoPath: ${stats.repoPath}
Version: ${stats.version}`)
})())
}
}
6 changes: 2 additions & 4 deletions src/core/components/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ module.exports = function repo (self) {

gc: require('./pin/gc')(self),

stat: callbackify.variadic(async (options) => {
options = options || {}

const stats = await self._repo.stat(options)
stat: callbackify.variadic(async () => {
const stats = await self._repo.stat()

return {
numObjects: stats.numObjects,
Expand Down
3 changes: 1 addition & 2 deletions src/http/api/resources/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ exports.version = async (request, h) => {

exports.stat = async (request, h) => {
const { ipfs } = request.server.app
const human = request.query.human === 'true'
const stat = await ipfs.repo.stat({ human })
const stat = await ipfs.repo.stat()

return h.response({
NumObjects: stat.numObjects,
Expand Down
18 changes: 18 additions & 0 deletions test/cli/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ describe('repo', () => runOnAndOff((thing) => {
ipfs = thing.ipfs
})

it('get repo stats', async () => {
const stats = await ipfs('repo stat')
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)
})

it('get human readable repo stats', async () => {
const stats = await ipfs('repo stat --human')
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)
})

it('get the repo version', async () => {
const out = await ipfs('repo version')
expect(out).to.eql(`${repoVersion}\n`)
Expand Down

0 comments on commit 39bc5b4

Please sign in to comment.