Skip to content

Commit 265edc1

Browse files
authored
Add go-version-file option (#62)
1 parent 193b404 commit 265edc1

File tree

8 files changed

+179
-3
lines changed

8 files changed

+179
-3
lines changed

.github/workflows/versions.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ jobs:
5050
- name: Verify Go
5151
run: go version
5252

53+
go-version-file:
54+
runs-on: ${{ matrix.os }}
55+
strategy:
56+
fail-fast: false
57+
matrix:
58+
os: [ubuntu-latest, windows-latest, macos-latest]
59+
steps:
60+
- uses: actions/checkout@v3
61+
- name: Setup Go and check latest
62+
uses: ./
63+
with:
64+
go-version-file: __tests__/data/go.mod
65+
- name: verify go
66+
run: __tests__/verify-go.sh 1.14
67+
shell: bash
68+
5369
setup-versions-from-manifest:
5470
name: Setup ${{ matrix.go }} ${{ matrix.os }}
5571
runs-on: ${{ matrix.os }}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ steps:
9494
check-latest: true
9595
- run: go run hello.go
9696
```
97+
## Getting go version from the go.mod file
98+
99+
The `go-version-file` input accepts a path to a `go.mod` file containing the version of Go to be used by a project. As the `go.mod` file contains only major and minor (e.g. 1.18) tags, the action will search for the latest available patch version sequentially in the runner's directory with the cached tools, in the [version-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go servers.
100+
101+
If both the `go-version` and the `go-version-file` inputs are provided then the `go-version` input is used.
102+
> The action will search for the `go.mod` file relative to the repository root
103+
104+
```yaml
105+
steps:
106+
- uses: actions/checkout@v3
107+
- uses: actions/setup-go@v3
108+
with:
109+
go-version-file: 'path/to/go.mod'
110+
- run: go version
111+
```
97112

98113
## Matrix testing
99114

__tests__/data/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module example.com/mymodule
2+
3+
go 1.14
4+
5+
require (
6+
example.com/othermodule v1.2.3
7+
example.com/thismodule v1.2.3
8+
example.com/thatmodule v1.2.3
9+
)
10+
11+
replace example.com/thatmodule => ../thatmodule
12+
exclude example.com/thismodule v1.3.0

__tests__/setup-go.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('setup-go', () => {
3333
let dbgSpy: jest.SpyInstance;
3434
let whichSpy: jest.SpyInstance;
3535
let existsSpy: jest.SpyInstance;
36+
let readFileSpy: jest.SpyInstance;
3637
let mkdirpSpy: jest.SpyInstance;
3738
let execSpy: jest.SpyInstance;
3839
let getManifestSpy: jest.SpyInstance;
@@ -71,6 +72,7 @@ describe('setup-go', () => {
7172
// io
7273
whichSpy = jest.spyOn(io, 'which');
7374
existsSpy = jest.spyOn(fs, 'existsSync');
75+
readFileSpy = jest.spyOn(fs, 'readFileSync');
7476
mkdirpSpy = jest.spyOn(io, 'mkdirP');
7577

7678
// gets
@@ -774,4 +776,68 @@ describe('setup-go', () => {
774776
expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`);
775777
});
776778
});
779+
780+
describe('go-version-file', () => {
781+
const goModContents = `module example.com/mymodule
782+
783+
go 1.14
784+
785+
require (
786+
example.com/othermodule v1.2.3
787+
example.com/thismodule v1.2.3
788+
example.com/thatmodule v1.2.3
789+
)
790+
791+
replace example.com/thatmodule => ../thatmodule
792+
exclude example.com/thismodule v1.3.0
793+
`;
794+
795+
it('reads version from go.mod', async () => {
796+
inputs['go-version-file'] = 'go.mod';
797+
existsSpy.mockImplementation(path => true);
798+
readFileSpy.mockImplementation(() => Buffer.from(goModContents));
799+
800+
await main.run();
801+
802+
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.14');
803+
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.14...');
804+
expect(logSpy).toHaveBeenCalledWith('matching 1.14...');
805+
});
806+
807+
it('reads version from .go-version', async () => {
808+
inputs['go-version-file'] = '.go-version';
809+
existsSpy.mockImplementation(path => true);
810+
readFileSpy.mockImplementation(() => Buffer.from(`1.13.0${osm.EOL}`));
811+
812+
await main.run();
813+
814+
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.0');
815+
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.0...');
816+
expect(logSpy).toHaveBeenCalledWith('matching 1.13.0...');
817+
});
818+
819+
it('is overwritten by go-version', async () => {
820+
inputs['go-version'] = '1.13.1';
821+
inputs['go-version-file'] = 'go.mod';
822+
existsSpy.mockImplementation(path => true);
823+
readFileSpy.mockImplementation(() => Buffer.from(goModContents));
824+
825+
await main.run();
826+
827+
expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.13.1');
828+
expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.13.1...');
829+
expect(logSpy).toHaveBeenCalledWith('matching 1.13.1...');
830+
});
831+
832+
it('reports a read failure', async () => {
833+
inputs['go-version-file'] = 'go.mod';
834+
existsSpy.mockImplementation(path => false);
835+
836+
await main.run();
837+
838+
expect(cnSpy).toHaveBeenCalledWith(
839+
`::error::The specified go version file at: go.mod does not exist${osm.EOL}`
840+
);
841+
});
842+
});
777843
});

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ author: 'GitHub'
44
inputs:
55
go-version:
66
description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges.'
7+
go-version-file:
8+
description: 'Path to the go.mod file.'
79
check-latest:
810
description: 'Set this option to true if you want the action to always check for the latest available version that satisfies the version spec'
911
default: false

dist/index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,7 +2074,7 @@ function run() {
20742074
// versionSpec is optional. If supplied, install / use from the tool cache
20752075
// If not supplied then problem matchers will still be setup. Useful for self-hosted.
20762076
//
2077-
let versionSpec = core.getInput('go-version');
2077+
const versionSpec = resolveVersionInput();
20782078
core.info(`Setup go version spec ${versionSpec}`);
20792079
if (versionSpec) {
20802080
let token = core.getInput('token');
@@ -2154,6 +2154,23 @@ function parseGoVersion(versionString) {
21542154
return versionString.split(' ')[2].slice('go'.length);
21552155
}
21562156
exports.parseGoVersion = parseGoVersion;
2157+
function resolveVersionInput() {
2158+
let version = core.getInput('go-version');
2159+
const versionFilePath = core.getInput('go-version-file');
2160+
if (version && versionFilePath) {
2161+
core.warning('Both go-version and go-version-file inputs are specified, only go-version will be used');
2162+
}
2163+
if (version) {
2164+
return version;
2165+
}
2166+
if (versionFilePath) {
2167+
if (!fs_1.default.existsSync(versionFilePath)) {
2168+
throw new Error(`The specified go version file at: ${versionFilePath} does not exist`);
2169+
}
2170+
version = installer.parseGoVersionFile(versionFilePath);
2171+
}
2172+
return version;
2173+
}
21572174
//# sourceMappingURL=main.js.map
21582175

21592176
/***/ }),
@@ -5894,13 +5911,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
58945911
return (mod && mod.__esModule) ? mod : { "default": mod };
58955912
};
58965913
Object.defineProperty(exports, "__esModule", { value: true });
5897-
exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.extractGoArchive = exports.getGo = void 0;
5914+
exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.extractGoArchive = exports.getGo = void 0;
58985915
const tc = __importStar(__webpack_require__(533));
58995916
const core = __importStar(__webpack_require__(470));
59005917
const path = __importStar(__webpack_require__(622));
59015918
const semver = __importStar(__webpack_require__(280));
59025919
const httpm = __importStar(__webpack_require__(539));
59035920
const sys = __importStar(__webpack_require__(737));
5921+
const fs_1 = __importDefault(__webpack_require__(747));
59045922
const os_1 = __importDefault(__webpack_require__(87));
59055923
function getGo(versionSpec, checkLatest, auth) {
59065924
return __awaiter(this, void 0, void 0, function* () {
@@ -6119,6 +6137,15 @@ function makeSemver(version) {
61196137
return fullVersion;
61206138
}
61216139
exports.makeSemver = makeSemver;
6140+
function parseGoVersionFile(versionFilePath) {
6141+
const contents = fs_1.default.readFileSync(versionFilePath).toString();
6142+
if (path.basename(versionFilePath) === 'go.mod') {
6143+
const match = contents.match(/^go (\d+(\.\d+)*)/m);
6144+
return match ? match[1] : '';
6145+
}
6146+
return contents.trim();
6147+
}
6148+
exports.parseGoVersionFile = parseGoVersionFile;
61226149
//# sourceMappingURL=installer.js.map
61236150

61246151
/***/ }),

src/installer.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from 'path';
44
import * as semver from 'semver';
55
import * as httpm from '@actions/http-client';
66
import * as sys from './system';
7+
import fs from 'fs';
78
import os from 'os';
89

910
type InstallationType = 'dist' | 'manifest';
@@ -298,3 +299,14 @@ export function makeSemver(version: string): string {
298299
}
299300
return fullVersion;
300301
}
302+
303+
export function parseGoVersionFile(versionFilePath: string): string {
304+
const contents = fs.readFileSync(versionFilePath).toString();
305+
306+
if (path.basename(versionFilePath) === 'go.mod') {
307+
const match = contents.match(/^go (\d+(\.\d+)*)/m);
308+
return match ? match[1] : '';
309+
}
310+
311+
return contents.trim();
312+
}

src/main.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function run() {
1313
// versionSpec is optional. If supplied, install / use from the tool cache
1414
// If not supplied then problem matchers will still be setup. Useful for self-hosted.
1515
//
16-
let versionSpec = core.getInput('go-version');
16+
const versionSpec = resolveVersionInput();
1717

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

@@ -104,3 +104,29 @@ export function parseGoVersion(versionString: string): string {
104104
// expecting go<version> for runtime.Version()
105105
return versionString.split(' ')[2].slice('go'.length);
106106
}
107+
108+
function resolveVersionInput(): string {
109+
let version = core.getInput('go-version');
110+
const versionFilePath = core.getInput('go-version-file');
111+
112+
if (version && versionFilePath) {
113+
core.warning(
114+
'Both go-version and go-version-file inputs are specified, only go-version will be used'
115+
);
116+
}
117+
118+
if (version) {
119+
return version;
120+
}
121+
122+
if (versionFilePath) {
123+
if (!fs.existsSync(versionFilePath)) {
124+
throw new Error(
125+
`The specified go version file at: ${versionFilePath} does not exist`
126+
);
127+
}
128+
version = installer.parseGoVersionFile(versionFilePath);
129+
}
130+
131+
return version;
132+
}

0 commit comments

Comments
 (0)