Skip to content

Commit

Permalink
Merge pull request #389 from upamune/add-go-version-file-input
Browse files Browse the repository at this point in the history
add `go_version_file` input
  • Loading branch information
shogo82148 committed Aug 14, 2022
2 parents 60bc741 + ac3e818 commit 99a63a1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -101,6 +101,11 @@ Optional. Additional reviewdog flags
Optional. Install a specific version of Go.
By default, the latest version of Go 1.x is installed.

### `go_version_file`

Optional. Install a specific version of Go from a file. It accepts a path to a `go.mod` file or a file containing only Go version.
If both the `go_version` and the `go_version_file` inputs are provided then the `go_version` input is used.

### `cache`

Optional. [`true`, `false`]
Expand Down
42 changes: 36 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/main.ts
Expand Up @@ -19,6 +19,7 @@ async function run() {
const reviewdogVersion = core.getInput("reviewdog_version") || "latest";
const golangciLintVersion = core.getInput("golangci_lint_version") || "latest";
const goVersion = core.getInput("go_version");
const goVersionFile = core.getInput("go_version_file");
const golangciLintFlags = core.getInput("golangci_lint_flags");
const toolName = core.getInput("tool_name") || "golangci";
const level = core.getInput("level") || "error";
Expand All @@ -30,9 +31,9 @@ async function run() {
const cwd = path.relative(process.env["GITHUB_WORKSPACE"] || process.cwd(), workdir);
const enableCache = core.getBooleanInput("cache");

if (goVersion !== "") {
if (goVersion !== "" || goVersionFile !== "") {
await core.group("Installing Go ...", async () => {
await setupGo.run(goVersion);
await setupGo.run(goVersion, goVersionFile);
});
}

Expand Down
14 changes: 13 additions & 1 deletion src/setup-go/installer.ts
@@ -1,4 +1,4 @@
// this file comes from https://github.com/actions/setup-go/blob/3b4dc6cbed1779f759b9c638cb83696acea809d1/src/installer.ts
// this file comes from https://github.com/actions/setup-go/blob/b22fbbc2921299758641fab08929b4ac52b32923/src/installer.ts
// see LICENSE for its license

import * as core from "@actions/core";
Expand All @@ -8,6 +8,7 @@ import * as semver from "semver";
import * as sys from "./system";
import * as tc from "@actions/tool-cache";
import os from "os";
import fs from "fs";

type InstallationType = "dist" | "manifest";

Expand Down Expand Up @@ -261,3 +262,14 @@ export function makeSemver(version: string): string {
}
return fullVersion;
}

export function parseGoVersionFile(versionFilePath: string): string {
const contents = fs.readFileSync(versionFilePath).toString();

if (path.basename(versionFilePath) === "go.mod") {
const match = contents.match(/^go (\d+(\.\d+)*)/m);
return match ? match[1] : "";
}

return contents.trim();
}
28 changes: 26 additions & 2 deletions src/setup-go/main.ts
@@ -1,4 +1,4 @@
// this file comes from https://github.com/actions/setup-go/blob/3b4dc6cbed1779f759b9c638cb83696acea809d1/src/main.ts
// this file comes from https://github.com/actions/setup-go/blob/b22fbbc2921299758641fab08929b4ac52b32923/src/main.ts
// see LICENSE for its license

import * as core from "@actions/core";
Expand All @@ -9,8 +9,13 @@ import cp from "child_process";
import fs from "fs";
import path from "path";

export async function run(versionSpec: string): Promise<void> {
export async function run(version: string, versionFilePath: string): Promise<void> {
try {
const versionSpec = resolveVersionInput(version, versionFilePath);
if (versionSpec === "") {
return;
}

core.info(`Setup go version spec ${versionSpec}`);

if (versionSpec) {
Expand Down Expand Up @@ -78,3 +83,22 @@ function isGhes(): boolean {
const ghUrl = new URL(process.env["GITHUB_SERVER_URL"] || "https://github.com");
return ghUrl.hostname.toUpperCase() !== "GITHUB.COM";
}

function resolveVersionInput(version: string, versionFilePath: string): string {
if (version && versionFilePath) {
core.warning("Both go_version and go_version_file inputs are specified, only go_version will be used");
}

if (version) {
return version;
}

if (versionFilePath) {
if (!fs.existsSync(versionFilePath)) {
throw new Error(`The specified go version file at: ${versionFilePath} does not exist`);
}
version = installer.parseGoVersionFile(versionFilePath);
}

return version;
}

0 comments on commit 99a63a1

Please sign in to comment.