2022-02-21 16:21:14 +03:00
|
|
|
import * as exec from '@actions/exec';
|
|
|
|
|
|
|
|
export interface PackageManagerInfo {
|
|
|
|
goSumFilePattern: string;
|
|
|
|
getCacheFolderCommand: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultPackageManager: PackageManagerInfo = {
|
2022-02-21 16:29:32 +03:00
|
|
|
goSumFilePattern: 'go.sum',
|
|
|
|
getCacheFolderCommand: 'go env GOMODCACHE'
|
2022-02-21 16:21:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getCommandOutput = async (toolCommand: string) => {
|
|
|
|
let {stdout, stderr, exitCode} = await exec.getExecOutput(
|
|
|
|
toolCommand,
|
|
|
|
undefined,
|
|
|
|
{ignoreReturnCode: true}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (exitCode) {
|
|
|
|
stderr = !stderr.trim()
|
|
|
|
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
|
|
|
|
: stderr;
|
|
|
|
throw new Error(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout.trim();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getPackageManagerInfo = async () => {
|
|
|
|
return defaultPackageManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getCacheDirectoryPath = async (
|
2022-02-21 16:29:32 +03:00
|
|
|
packageManagerInfo: PackageManagerInfo
|
2022-02-21 16:21:14 +03:00
|
|
|
) => {
|
|
|
|
const stdout = await getCommandOutput(
|
|
|
|
packageManagerInfo.getCacheFolderCommand
|
2022-02-21 16:29:32 +03:00
|
|
|
);
|
2022-02-21 16:21:14 +03:00
|
|
|
|
|
|
|
if (!stdout) {
|
|
|
|
throw new Error(`Could not get cache folder path.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout;
|
|
|
|
};
|