@stoar/cli
Version:
CLI tool for STOAR - decentralized file storage on Arweave
92 lines • 3.76 kB
JavaScript
import { Command } from 'commander';
import { getWalletConfig } from '../utils/wallet.js';
import { output, createTable, createSpinner, formatBytes } from '../utils/output.js';
import { wrapCommand } from '../utils/error.js';
export const listCommand = new Command('list')
.description('List uploaded files')
.option('-l, --limit <number>', 'Number of results to show', '20')
.option('-t, --tags <tags>', 'Filter by tags (key=value,key2=value2)')
.option('-o, --owner <address>', 'Filter by owner address')
.option('--from <date>', 'Filter by date (YYYY-MM-DD)')
.option('--to <date>', 'Filter by date (YYYY-MM-DD)')
.option('--expand-bundles', 'Show individual files within bundles')
.action(wrapCommand(async (options, command) => {
const globalOptions = command.parent?.opts() || {};
const spinner = createSpinner('Querying files...');
spinner.start();
try {
const { client } = await getWalletConfig(globalOptions);
// Parse tags filter
const tags = {};
if (options.tags) {
const tagPairs = options.tags.split(',');
for (const pair of tagPairs) {
const [key, value] = pair.split('=');
if (key && value) {
tags[key] = value;
}
}
}
// Build query
const queryOptions = {
limit: parseInt(options.limit),
tags,
owner: options.owner
};
// Date filtering would need to be implemented in the query
if (options.from || options.to) {
console.warn('Date filtering is not yet implemented');
}
// Query files
const results = await client.query(queryOptions);
spinner.stop();
if (globalOptions.json) {
output(results, globalOptions);
return;
}
if (results.length === 0) {
console.log('No files found');
return;
}
// Create table for display
const table = createTable(['ID', 'File Name', 'Size', 'Date', 'Cost']);
for (const item of results) {
// Extract file info from tags
let fileName = 'Unknown';
let fileSize = '0';
if (item.tags && typeof item.tags === 'object') {
for (const [key, value] of Object.entries(item.tags)) {
if (key === 'File-Name' || key === 'Title') {
fileName = value;
}
else if (key === 'Content-Length' || key === 'File-Size') {
fileSize = value;
}
}
}
// Convert winston to AR (divide by 1e12)
const feeInAr = item.fee ? (parseFloat(item.fee) / 1e12).toFixed(6) + ' AR' : 'N/A';
// Convert timestamp from seconds to milliseconds
const dateStr = item.block?.timestamp
? new Date(item.block.timestamp * 1000).toLocaleDateString()
: 'N/A';
// Check if this is a bundle and user wants to expand
const isBundle = item.tags['Bundle-Format'] && item.tags['Bundle-Version'];
const bundleMarker = isBundle ? '📦 ' : '';
table.push([
bundleMarker + item.id.substring(0, 8) + '...',
fileName,
fileSize !== '0' ? formatBytes(parseInt(fileSize)) : 'N/A',
dateStr,
feeInAr
]);
}
console.log(table.toString());
console.log(`\nShowing ${results.length} results`);
}
catch (error) {
spinner.stop();
throw error;
}
}));
//# sourceMappingURL=list.js.map