Add utility function to run file command

This commit is contained in:
Michael B. Gale 2023-09-28 11:46:00 +01:00
parent c08086a26a
commit 3c15d2383b
No known key found for this signature in database
GPG key ID: FF5E2765BD00628F
3 changed files with 58 additions and 2 deletions

View file

@ -425,3 +425,33 @@ export function getWorkflowRunAttempt(): number {
}
return workflowRunAttempt;
}
/**
* Tries to obtain the output of the `file` command for the file at the specified path.
*/
export const getFileType = async (fp: string): Promise<string> => {
let stderr = "";
try {
let fileOut = "";
await new toolrunner.ToolRunner(
await safeWhich.safeWhich("file"),
["-L", fp],
{
silent: true,
listeners: {
stdout: (data) => {
fileOut += data.toString();
},
stderr: (data) => {
stderr += data.toString();
},
},
},
).exec();
return fileOut;
} catch (e) {
core.info(`Could not determine type of ${fp}. ${stderr}`);
throw e;
}
};