-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathinfo.ts
More file actions
171 lines (149 loc) · 6.14 KB
/
Copy pathinfo.ts
File metadata and controls
171 lines (149 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Argv } from 'yargs';
import { OCIManifest, OCIRef, fetchOCIManifestIfExists, getPublishedTags, getRef } from '../../spec-configuration/containerCollectionsOCI';
import { Log, LogLevel, mapLogLevel } from '../../spec-utils/log';
import { getPackageConfig } from '../../spec-utils/product';
import { createLog } from '../devContainers';
import { UnpackArgv } from '../devContainersSpecCLI';
import { buildDependencyGraph, generateMermaidDiagram } from '../../spec-configuration/containerFeaturesOrder';
import { DevContainerFeature } from '../../spec-configuration/configuration';
import { processFeatureIdentifier } from '../../spec-configuration/containerFeaturesConfiguration';
import { runAsyncHandler } from '../utils';
export function featuresInfoOptions(y: Argv) {
return y
.options({
'log-level': { choices: ['info' as 'info', 'debug' as 'debug', 'trace' as 'trace'], default: 'info' as 'info', description: 'Log level.' },
'output-format': { choices: ['text' as 'text', 'json' as 'json'], default: 'text', description: 'Output format.' },
})
.positional('mode', { choices: ['manifest' as 'manifest', 'tags' as 'tags', 'dependencies' as 'dependencies', 'verbose' as 'verbose'], description: 'Data to query. Select \'verbose\' to return everything.' })
.positional('feature', { type: 'string', demandOption: true, description: 'Feature Identifier' });
}
export type FeaturesInfoArgs = UnpackArgv<ReturnType<typeof featuresInfoOptions>>;
export function featuresInfoHandler(args: FeaturesInfoArgs) {
runAsyncHandler(featuresInfo.bind(null, args));
}
interface InfoJsonOutput {
manifest?: OCIManifest;
canonicalId?: string;
publishedTags?: string[];
}
async function featuresInfo({
'mode': mode,
'feature': featureId,
'log-level': inputLogLevel,
'output-format': outputFormat,
}: FeaturesInfoArgs) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
await Promise.all(disposables.map(d => d()));
};
const pkg = getPackageConfig();
const output = createLog({
logLevel: mapLogLevel(inputLogLevel),
logFormat: 'text',
log: (str) => process.stderr.write(str),
terminalDimensions: undefined,
}, pkg, new Date(), disposables, true);
const params = { output, env: process.env, outputFormat };
const jsonOutput: InfoJsonOutput = {};
// Parse the provided Feature Id
const featureRef = getRef(output, featureId);
if (!featureRef) {
if (outputFormat === 'json') {
console.log(JSON.stringify({}), LogLevel.Info);
} else {
console.log(`Failed to parse Feature identifier '${featureId}'\n`, LogLevel.Error);
}
process.exit(1);
}
const manifestContainer = await getManifest(params, featureRef);
if (!manifestContainer) {
process.exit(1);
}
// -- Display the manifest
if (mode === 'manifest' || mode === 'verbose') {
const { manifestObj, canonicalId } = manifestContainer;
if (outputFormat === 'text') {
console.log(encloseStringInBox('Manifest'));
console.log(`${JSON.stringify(manifestObj, undefined, 2)}\n`);
console.log(encloseStringInBox('Canonical Identifier'));
console.log(`${canonicalId}\n`);
} else {
jsonOutput.manifest = manifestObj;
jsonOutput.canonicalId = canonicalId;
}
}
// --- Get all published tags for resource
if (mode === 'tags' || mode === 'verbose') {
const publishedTags = await getTags(params, featureRef);
if (outputFormat === 'text') {
console.log(encloseStringInBox('Published Tags'));
console.log(`${publishedTags.join('\n ')}`);
} else {
jsonOutput.publishedTags = publishedTags;
}
}
if ((mode === 'dependencies' || mode === 'verbose') && outputFormat === 'text') {
output.write(`Building dependency graph for '${featureId}'...`, LogLevel.Info);
if (!featureRef) {
output.write(`Provide Feature reference '${featureId}' is invalid.`, LogLevel.Error);
process.exit(1);
}
const processFeature = async (_userFeature: DevContainerFeature) => {
return await processFeatureIdentifier(params, undefined, '', _userFeature);
};
const graph = await buildDependencyGraph(params, processFeature, [{ userFeatureId: featureId, options: {} }], { overrideFeatureInstallOrder: undefined }, undefined);
output.write(JSON.stringify(graph, undefined, 4), LogLevel.Trace);
if (!graph) {
output.write(`Could not build dependency graph.`, LogLevel.Error);
process.exit(1);
}
if (outputFormat === 'text') {
console.log(encloseStringInBox('Dependency Tree (Render with https://mermaid.live/)'));
const diagram = generateMermaidDiagram(params, graph.worklist);
console.log(diagram);
}
}
// -- Output and clean up
if (outputFormat === 'json') {
console.log(JSON.stringify(jsonOutput, undefined, 4));
}
await dispose();
process.exit();
}
async function getManifest(params: { output: Log; env: NodeJS.ProcessEnv; outputFormat: string }, featureRef: OCIRef) {
const { outputFormat } = params;
const manifestContainer = await fetchOCIManifestIfExists(params, featureRef, undefined);
if (!manifestContainer) {
if (outputFormat === 'json') {
console.log(JSON.stringify({}));
} else {
console.log('No manifest found! If this manifest requires authentication, please login.');
}
return process.exit(1);
}
return manifestContainer;
}
async function getTags(params: { output: Log; env: NodeJS.ProcessEnv; outputFormat: string }, featureRef: OCIRef) {
const { outputFormat } = params;
const publishedTags = await getPublishedTags(params, featureRef);
if (!publishedTags || publishedTags.length === 0) {
if (outputFormat === 'json') {
console.log(JSON.stringify({}));
} else {
console.log(`No published versions found for feature '${featureRef.resource}'\n`);
}
process.exit(1);
}
return publishedTags;
}
function encloseStringInBox(str: string, indent: number = 0) {
const lines = str.split('\n');
lines[0] = `\u001b[1m${lines[0]}\u001b[22m`; // Bold
const maxWidth = Math.max(...lines.map(l => l.length - (l.includes('\u001b[1m') ? 9 : 0)));
const box = [
'┌' + '─'.repeat(maxWidth) + '┐',
...lines.map(l => '│' + l.padEnd(maxWidth + (lines.length > 1 && l.includes('\u001b[1m') ? 9 : 0)) + '│'),
'└' + '─'.repeat(maxWidth) + '┘',
];
return box.map(t => `${' '.repeat(indent)}${t}`).join('\n');
}