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

feat(cli): Add ls command to list files and sizes in a snapshot #2689

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions packages/openneuro-cli/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getSnapshots } from './snapshots.js'
import { getDownload } from './download.js'
import { configuredClient } from './configuredClient.js'
import { validateApiKey } from './validateApiKey'
import { lsSnapshot } from './ls.js'

/**
* Login action to save an auth key locally
Expand Down Expand Up @@ -283,3 +284,26 @@ export const download = (datasetId, destination, cmd) => {
}
apmTransaction.end()
}

/**
* List files for a snapshot
*
* @param {string} datasetId
* @param {object} cmd
*/
export const ls = (datasetId, cmd) => {
const client = configuredClient()
if (!cmd.snapshot) {
return getSnapshots(client)(datasetId).then(({ data }) => {
if (data.dataset && data.dataset.snapshots) {
const tags = data.dataset.snapshots.map(snap => snap.tag)
tags.reverse()
return promptTags(tags).then(choices => {
lsSnapshot(client, datasetId, choices.tag)
})
}
})
} else {
return lsSnapshot(client, datasetId, cmd.snapshot)
}
}
8 changes: 7 additions & 1 deletion packages/openneuro-cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import commander from 'commander'
import { version } from './lerna.json'
import { login, upload, download } from './actions.js'
import { login, upload, download, ls } from './actions.js'
import { gitCredential } from './gitCredential.js'
import { gitAnnexRemote } from './gitAnnexRemote.js'
import { create } from './createDataset.js'
Expand Down Expand Up @@ -68,6 +68,12 @@ commander
create()
})

commander
.command('ls <datasetId>')
.description('Lists files from a snapshotted dataset version.')
.option('-s, --snapshot [snapshotVersion]')
.action(ls)

const processName = process.argv[1].endsWith('index.js')
? process.env.npm_lifecycle_event
: process.argv[1]
Expand Down
23 changes: 20 additions & 3 deletions packages/openneuro-cli/src/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ export const getDatasetFiles = async (
datasetId,
path = '',
tree = null,
) => {
const files = []
await _getDatasetFiles(client, datasetId, f => files.push(f), path, tree)
return files
}

/**
* Get an existing dataset's files
* @param {object} client GraphQL client
* @param {*} datasetId
*/
export const _getDatasetFiles = async (
client,
datasetId,
callback,
path = '',
tree = null,
) => {
const files = []
const { data } = await client.query({
Expand All @@ -56,15 +73,15 @@ export const getDatasetFiles = async (
})
for (const f of data.dataset.draft.files) {
if (f.directory) {
const nestedFiles = await getDatasetFiles(
await _getDatasetFiles(
client,
datasetId,
callback,
path ? `${path}/${f.filename}` : f.filename,
f.id,
)
files.push(...nestedFiles)
} else {
files.push({
callback({
...f,
filename: path ? `${path}/${f.filename}` : f.filename,
})
Expand Down
13 changes: 13 additions & 0 deletions packages/openneuro-cli/src/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { _getDatasetFiles } from './datasets'

export function lsSnapshot(client, datasetId, tree) {
return _getDatasetFiles(
client,
datasetId,
f => {
process.stdout.write(`${f.filename}\t${f.size}\n`)
},
'',
tree,
)
}