generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 41
/
utils.ts
88 lines (80 loc) · 2.38 KB
/
utils.ts
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
import * as actions_exec from "@actions/exec";
import * as core from "@actions/core";
import * as im from "@actions/exec/lib/interfaces";
/**
* Execute a command and wrap the output in a log group.
*
* @param commandLine command to execute (can include additional args). Must be correctly escaped.
* @param args optional arguments for tool. Escaping is handled by the lib.
* @param options optional exec options. See ExecOptions
* @param log_message log group title.
* @returns Promise<number> exit code
*/
export async function exec(
commandLine: string,
args?: string[],
options?: im.ExecOptions,
log_message?: string
): Promise<number> {
const argsAsString = (args || []).join(" ");
const message = log_message || `Invoking "${commandLine} ${argsAsString}"`;
return core.group(message, () => {
return actions_exec.exec(commandLine, args, options);
});
}
export function getRequiredRosDistributions(): string[] {
let requiredRosDistributionsList: string[] = [];
const requiredRosDistributions = core.getInput("required-ros-distributions");
if (requiredRosDistributions) {
requiredRosDistributionsList = requiredRosDistributions.split(
RegExp("\\s")
);
}
if (!validateDistro(requiredRosDistributionsList)) {
throw new Error("Input has invalid distribution names.");
}
return requiredRosDistributionsList;
}
//list of valid linux distributions
const validDistro: string[] = [
"melodic",
"noetic",
"foxy",
"humble",
"iron",
"rolling",
];
//Determine whether all inputs name supported ROS distributions.
export function validateDistro(
requiredRosDistributionsList: string[]
): boolean {
for (const rosDistro of requiredRosDistributionsList) {
if (validDistro.indexOf(rosDistro) <= -1) {
return false;
}
}
return true;
}
/**
* Determines the Ubuntu distribution codename.
*
* This function directly source /etc/lsb-release instead of invoking
* lsb-release as the package may not be installed.
*
* @returns Promise<string> Ubuntu distribution codename (e.g. "focal")
*/
export async function determineDistribCodename(): Promise<string> {
let distribCodename = "";
const options: im.ExecOptions = {};
options.listeners = {
stdout: (data: Buffer) => {
distribCodename += data.toString();
},
};
await exec(
"bash",
["-c", 'source /etc/lsb-release ; echo -n "$DISTRIB_CODENAME"'],
options
);
return distribCodename;
}