|
| 1 | +import { getStorageConfig } from '@auth/provider.js'; |
| 2 | +import { head } from '@tigrisdata/storage'; |
| 3 | +import { failWithError } from '@utils/exit.js'; |
| 4 | +import { formatOutput, formatSize } from '@utils/format.js'; |
| 5 | +import { msg, printStart, printSuccess } from '@utils/messages.js'; |
| 6 | +import { getFormat, getOption } from '@utils/options.js'; |
| 7 | +import { resolveObjectArgs } from '@utils/path.js'; |
| 8 | + |
| 9 | +const context = msg('objects', 'info'); |
| 10 | + |
| 11 | +export default async function objectInfo(options: Record<string, unknown>) { |
| 12 | + printStart(context); |
| 13 | + |
| 14 | + const format = getFormat(options); |
| 15 | + const bucketArg = getOption<string>(options, ['bucket']); |
| 16 | + const keyArg = getOption<string>(options, ['key']); |
| 17 | + const snapshotVersion = getOption<string>(options, [ |
| 18 | + 'snapshot-version', |
| 19 | + 'snapshotVersion', |
| 20 | + 'snapshot', |
| 21 | + ]); |
| 22 | + |
| 23 | + if (!bucketArg) { |
| 24 | + failWithError(context, 'Bucket name or path is required'); |
| 25 | + } |
| 26 | + |
| 27 | + const { bucket, key } = resolveObjectArgs(bucketArg, keyArg); |
| 28 | + |
| 29 | + if (!key) { |
| 30 | + failWithError(context, 'Object key is required'); |
| 31 | + } |
| 32 | + |
| 33 | + const config = await getStorageConfig(); |
| 34 | + |
| 35 | + const { data, error } = await head(key, { |
| 36 | + ...(snapshotVersion ? { snapshotVersion } : {}), |
| 37 | + config: { |
| 38 | + ...config, |
| 39 | + bucket, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + if (error) { |
| 44 | + failWithError(context, error); |
| 45 | + } |
| 46 | + |
| 47 | + if (!data) { |
| 48 | + failWithError(context, 'Object not found'); |
| 49 | + } |
| 50 | + |
| 51 | + const info = [ |
| 52 | + { metric: 'Path', value: data.path }, |
| 53 | + { metric: 'Size', value: formatSize(data.size) }, |
| 54 | + { metric: 'Content-Type', value: data.contentType || 'N/A' }, |
| 55 | + { metric: 'Content-Disposition', value: data.contentDisposition || 'N/A' }, |
| 56 | + { metric: 'Modified', value: data.modified.toISOString() }, |
| 57 | + { metric: 'URL', value: data.url }, |
| 58 | + ]; |
| 59 | + |
| 60 | + const output = formatOutput(info, format!, 'object-info', 'info', [ |
| 61 | + { key: 'metric', header: 'Metric' }, |
| 62 | + { key: 'value', header: 'Value' }, |
| 63 | + ]); |
| 64 | + |
| 65 | + console.log(output); |
| 66 | + printSuccess(context, { bucket, key }); |
| 67 | +} |
0 commit comments