Skip to content

Commit 3fab3c7

Browse files
author
Andrew Jacombs
authored
feat: add --from-file option to cogify create command (#2851)
Adds a `--from-file` option to the `cogify create` command. This has been added to simplify passing multiple arguments to `cogify create` inside an Argo workflow.
1 parent b6afd57 commit 3fab3c7

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

packages/cogify/src/cogify/cli/cli.cog.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { LogType, fsa } from '@basemaps/shared';
33
import { CliId, CliInfo } from '@basemaps/shared/build/cli/info.js';
44
import { CogTiff, TiffTag } from '@cogeotiff/core';
55
import { Metrics } from '@linzjs/metrics';
6-
import { command, flag, restPositionals } from 'cmd-ts';
6+
import { command, flag, option, optional, restPositionals } from 'cmd-ts';
77
import { mkdir, rm } from 'fs/promises';
88
import { tmpdir } from 'os';
99
import { StacAsset, StacCollection } from 'stac-ts';
@@ -13,7 +13,7 @@ import { HashTransform } from '../../hash.stream.js';
1313
import { getLogger, logArguments } from '../../log.js';
1414
import { gdalBuildCog, gdalBuildVrt, gdalBuildVrtWarp } from '../gdal.command.js';
1515
import { GdalRunner } from '../gdal.runner.js';
16-
import { Url } from '../parsers.js';
16+
import { Url, UrlArrayJsonFile } from '../parsers.js';
1717
import { CogifyCreationOptions, CogifyStacItem, getCutline, getSources } from '../stac.js';
1818

1919
// FIXME: HACK @cogeotiff/core to include the Lerc tiff tag
@@ -61,15 +61,24 @@ export const BasemapsCogifyCreateCommand = command({
6161
description: 'Create a COG from a covering configuration',
6262
args: {
6363
...logArguments,
64-
path: restPositionals({ type: Url, displayName: 'path', description: 'Path to item json' }),
64+
path: restPositionals({ type: Url, displayName: 'path', description: 'Path to covering configuration' }),
6565
force: flag({ long: 'force', description: 'Overwrite existing tiff files' }),
66+
fromFile: option({
67+
type: optional(UrlArrayJsonFile),
68+
long: 'from-file',
69+
description:
70+
'Path to JSON file containing array of paths to covering configurations. ' +
71+
'File must be an array of objects with key "path" and value of a path to a covering configuration.',
72+
}),
6673
},
6774

6875
async handler(args) {
6976
const metrics = new Metrics();
7077
const logger = getLogger(this, args);
7178

72-
const toCreate = await Promise.all(args.path.map(async (p) => loadItem(p, logger)));
79+
const paths = args.fromFile != null ? args.path.concat(args.fromFile) : args.path;
80+
81+
const toCreate = await Promise.all(paths.map(async (p) => loadItem(p, logger)));
7382
// // Filter out any missing items, also excluding items which already have COGs created
7483
const filtered = toCreate.filter((f) => {
7584
if (f == null) return false;

packages/cogify/src/cogify/parsers.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fsa } from '@basemaps/shared';
12
import { Type } from 'cmd-ts';
23
import { pathToFileURL } from 'node:url';
34

@@ -14,3 +15,26 @@ export const Url: Type<string, URL> = {
1415
}
1516
},
1617
};
18+
19+
/**
20+
* Parse a JSON file containing an array of URLs.
21+
*
22+
* JSON file must contain an outer array, inside of which is a series of objects
23+
* with key "path", the value of which will be parsed as a URL. If the looks
24+
* like a file path, it will instead be converted using `pathToFileURL`.
25+
**/
26+
export const UrlArrayJsonFile: Type<string, URL[]> = {
27+
async from(str) {
28+
const raw: { path: string }[] = await fsa.readJson(str);
29+
if (!Array.isArray(raw)) throw new Error('JSON does not contain an outer array');
30+
const urls = raw.map((f) => {
31+
if (!('path' in f)) throw new Error('Missing key "path"');
32+
try {
33+
return new URL(f.path);
34+
} catch (e) {
35+
return pathToFileURL(f.path);
36+
}
37+
});
38+
return urls;
39+
},
40+
};

0 commit comments

Comments
 (0)