-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathfile-finder.ts
More file actions
142 lines (127 loc) · 5.86 KB
/
file-finder.ts
File metadata and controls
142 lines (127 loc) · 5.86 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
import * as ghCore from "@actions/core";
import * as semver from "semver";
import { Inputs } from "../generated/inputs-outputs";
import { findClientVersionFromGithub, getReleaseAssets } from "../github-client-finder/repository-finder";
import { findClientDir, getDirContents } from "../mirror-client-finder/directory-finder";
import { isOCV3, getOCV3File } from "../mirror-client-finder/oc-3-finder";
import {
ClientDetailOverrides, ClientDirectory, ClientFile, GITHUB, InstallableClient, MIRROR,
} from "../util/types";
import {
getArch, getGitHubReleaseAssetPath, getOS,
} from "../util/utils";
import {
filterByOS, filterByZipped, filterByExecutable, filterByVersioned, filterByArch, filterClients, filterByNotZipped,
} from "./filters";
type ClientFilterFunc = ((filename: string) => boolean);
/**
* Finds the latest version of client that satisifies the desiredVersionRange.
* This is done by:
* - If source is 'mirror', looking up the directory for the client on mirror.openshift.com
* - If source is 'github', looking up the GitHub releases for the client on the respective GitHub repositories
* - reading the available versions (directory names / releases)
* - finding the max version that satisfies
* - navigating into that versioned directory / release
* - finding the filename/asset for the client, operating system, and architecture for the current machine.
*
* @returns All the required information about the client file, once it's been located.
*/
export async function findMatchingClient(source: string, client: InstallableClient, desiredVersionRange: semver.Range):
Promise<ClientFile> {
let clientDir: ClientDirectory;
let clientFiles: string[];
let clientVersion: string;
let ClientDirectoryUrl = "";
if (source === MIRROR) {
clientDir = await findClientDir(client, desiredVersionRange);
ClientDirectoryUrl = clientDir.url;
clientFiles = await getDirContents(ClientDirectoryUrl);
clientVersion = clientDir.version;
ghCore.debug(`${client} ${clientVersion} files: ${clientFiles.join(", ")}`);
if (isOCV3(client, desiredVersionRange)) {
return getOCV3File(clientDir, desiredVersionRange);
}
}
else {
clientVersion = await findClientVersionFromGithub(client, desiredVersionRange);
clientFiles = await getReleaseAssets(client, clientVersion);
ghCore.debug(`${client} ${clientVersion} files: ${clientFiles.join(", ")}`);
}
// select a 'filter pipeline'
// in the case of 'mirror' as the source the ocp directory and camel-k
// have different naming / organization than the others
let filters: ClientFilterFunc[];
if (client === Inputs.KAMEL && source === MIRROR) {
// these filters are used for camel-k / kamel, which is amd64 only.
filters = [ filterByOS, filterByZipped ];
}
// Since directory name for opm is ocp (in case of mirror)
// but this filter pipeline is not valid for opm when source is github
else if (ClientDetailOverrides[client]?.mirror?.directoryName === "ocp" && source !== GITHUB) {
// we have to filter out the other client we're not interested in
// - ie remove 'oc' if we're installing 'openshift-install'.
filters = [
filterByOS,
filterByArch,
filterByExecutable.bind(client),
filterByVersioned.bind(clientVersion),
filterByZipped,
];
}
// in case of 'github' as the source operator-sdk only has execuatables in the release assets
else if (client === Inputs.OPERATOR_SDK && source === GITHUB) {
filters = [ filterByOS, filterByArch, filterByExecutable.bind(client) ];
}
// In case of client being 'yq', executable and zip files both are present
// this will give warning to the users as multiple files will be found after
// filteration. So removing all the zipped files.
else if (client === Inputs.YQ && source === GITHUB) {
filters = [ filterByOS, filterByArch, filterByNotZipped ];
}
// chart-verifier only publishes a linux binary, but publishes other release
// assets that are not in an archive format.
else if (client === Inputs.CHART_VERIFIER && source === GITHUB) {
filters = [
filterByZipped,
filterByExecutable.bind(client),
filterByVersioned.bind(clientVersion),
];
}
else {
// these filters are used for all the other clients.
filters = [ filterByOS, filterByArch, filterByZipped ];
}
const filteredClientFiles = filterClients(clientFiles, filters);
let archiveFilename = filteredClientFiles[0];
// Since tkn-pac binary is also present in the same directory
if (filteredClientFiles.length > 1 && client === "tkn" && filteredClientFiles[0].includes("pac")) {
archiveFilename = filteredClientFiles[1];
}
else if (filteredClientFiles.length > 1) {
ghCore.warning(`Multiple files were found for ${client} that matched the current OS and architecture: `
+ `${filteredClientFiles.join(", ")}. Selecting the first one.`);
}
else if (filteredClientFiles.length === 0) {
throw new Error(`No ${client} file was found for ${getArch()} ${getOS()}`
+ ` ${ClientDirectoryUrl ? `under ${ClientDirectoryUrl}` : ""}.`);
}
ghCore.info(`Selecting ${archiveFilename}`);
let archiveUrl;
if (source === MIRROR) {
archiveUrl = `${ClientDirectoryUrl}${archiveFilename}`;
}
else {
archiveUrl = getGitHubReleaseAssetPath(client, clientVersion, archiveFilename);
}
const clientFile: ClientFile = {
archiveFilename,
archiveFileUrl: archiveUrl,
clientName: client,
version: clientVersion,
versionRange: desiredVersionRange,
};
if (ClientDirectoryUrl !== "") {
clientFile.mirrorDirectoryUrl = ClientDirectoryUrl;
}
return clientFile;
}