Read go version from go.mod

This commit is contained in:
So Jomura 2022-04-29 11:11:36 +00:00 committed by GitHub
parent f5fe54e5a4
commit 55b9afc327
4 changed files with 76 additions and 22 deletions

View file

@ -266,3 +266,12 @@ export function makeSemver(version: string): string {
return `${verPart}${prereleasePart}`;
}
export function parseGoVersionFile(contents: string, isMod: boolean): string {
if (!isMod) {
return contents.trim();
}
const match = contents.match(/^go (\d+(\.\d+)*)/m);
return match ? match[1] : '';
}

View file

@ -12,14 +12,7 @@ export async function run() {
// versionSpec is optional. If supplied, install / use from the tool cache
// If not supplied then problem matchers will still be setup. Useful for self-hosted.
//
const versionFilePath = core.getInput('go-version-from-file');
const versionSpecFromFile =
versionFilePath &&
fs
.readFileSync(versionFilePath)
.toString()
.trim();
let versionSpec = core.getInput('go-version') || versionSpecFromFile;
const versionSpec = resolveVersionInput();
// stable will be true unless false is the exact input
// since getting unstable versions should be explicit
@ -97,3 +90,21 @@ function isGhes(): boolean {
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
function resolveVersionInput(): string {
let version = core.getInput('go-version');
const versionFilePath = core.getInput('go-version-file');
if (version) {
return version;
}
if (versionFilePath) {
version = installer.parseGoVersionFile(
fs.readFileSync(versionFilePath).toString(),
path.basename(versionFilePath) === 'go.mod'
);
}
return version;
}