Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON output of go env and some env as strings #334

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/outputs.yml
@@ -0,0 +1,24 @@
name: Test outputs

on:
push:
branches:
- main
pull_request:

jobs:
setup-go-env:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- id: setup-go
uses: ./
- run: |
echo GOPATH=${{ steps.setup-go.outputs.go-path }}
echo GOMOD=${{ steps.setup-go.outputs.go-mod }}
echo GOMODCACHE=${{ steps.setup-go.outputs.go-mod-cache }}
echo GOVERSION=${{ steps.setup-go.outputs.go-version }}
echo GOCACHE=${{ steps.setup-go.outputs.go-cache }}

echo Go environment variables json:
jq . <<< '${{ steps.setup-go.outputs.go-env }}'
23 changes: 23 additions & 0 deletions __tests__/setup-go.test.ts
Expand Up @@ -138,6 +138,29 @@ describe('setup-go', () => {
expect(main.parseGoVersion(goVersionOutput)).toBe('1.16.6');
});

it('can read go env variables', async () => {
const goRoot = '/opt/hostedtoolcache/go/1.18.10/x64';
const goPath = '/home/runner/go';
const goModCache = '/home/runner/go/pkg/mod';
const goCache = '/home/runner/.cache/go-build';
const goVersion = 'go1.18.10';

const env = `
GOROOT="${goRoot}"
GOPATH="${goPath}"
GOMODCACHE="${goModCache}"
GOCACHE="${goCache}"
GOVERSION="${goVersion}"
`;
const json = JSON.parse(main.convertEnvStringToJson(env));
expect(json).toBeDefined();
expect(json['GOROOT']).toBe(goRoot);
expect(json['GOPATH']).toBe(goPath);
expect(json['GOMODCACHE']).toBe(goModCache);
expect(json['GOCACHE']).toBe(goCache);
expect(json['GOVERSION']).toBe(goVersion);
});

it('can find 1.9.7 from manifest on osx', async () => {
os.platform = 'darwin';
os.arch = 'x64';
Expand Down
12 changes: 12 additions & 0 deletions action.yml
Expand Up @@ -22,6 +22,18 @@ inputs:
outputs:
go-version:
description: 'The installed Go version. Useful when given a version range as input.'
go-cache:
description: 'The GOCACHE environment variable'
go-path:
description: 'The GOPATH environment variable'
go-root:
description: 'The GOROOT environment variable'
go-mod:
description: 'The GOMOD environment variable'
go-mod-cache:
description: 'The GOMODCACHE environment variable'
go-env:
description: 'The Go environment variables in JSON format'
cache-hit:
description: 'A boolean value to indicate if a cache was hit'
runs:
Expand Down
31 changes: 25 additions & 6 deletions dist/setup/index.js
Expand Up @@ -88408,7 +88408,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseGoVersion = exports.addBinToPath = exports.run = void 0;
exports.convertEnvStringToJson = exports.parseGoVersion = exports.addBinToPath = exports.run = void 0;
const core = __importStar(__nccwpck_require__(2186));
const io = __importStar(__nccwpck_require__(7436));
const installer = __importStar(__nccwpck_require__(2574));
Expand Down Expand Up @@ -88456,11 +88456,19 @@ function run() {
core.debug(`add bin ${added}`);
const goPath = yield io.which('go');
const goVersion = (child_process_1.default.execSync(`${goPath} version`) || '').toString();
const goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
const goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
const parsedGoVersion = parseGoVersion(goVersion);
// Go versions less that 1.16 do not have the GOVERSION environment variable
if (semver.lt(parsedGoVersion, '1.16.0')) {
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
}
core.info(goVersion);
if (cache && (0, cache_utils_1.isCacheFeatureAvailable)()) {
const packageManager = 'default';
const cacheDependencyPath = core.getInput('cache-dependency-path');
try {
yield (0, cache_restore_1.restoreCache)(parseGoVersion(goVersion), packageManager, cacheDependencyPath);
yield (0, cache_restore_1.restoreCache)(parsedGoVersion, packageManager, cacheDependencyPath);
}
catch (error) {
core.warning(`Restore cache failed: ${error.message}`);
Expand All @@ -88469,11 +88477,12 @@ function run() {
// add problem matchers
const matchersPath = path_1.default.join(__dirname, '../..', 'matchers.json');
core.info(`##[add-matcher]${matchersPath}`);
// output the version actually being used
core.info(goVersion);
core.setOutput('go-version', parseGoVersion(goVersion));
core.setOutput('go-version', parsedGoVersion);
core.setOutput('go-path', goEnvJson['GOPATH']);
core.setOutput('go-cache', goEnvJson['GOCACHE']);
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
core.setOutput('go-env', goEnvJson);
core.startGroup('go env');
const goEnv = (child_process_1.default.execSync(`${goPath} env`) || '').toString();
core.info(goEnv);
core.endGroup();
}
Expand Down Expand Up @@ -88521,6 +88530,16 @@ function parseGoVersion(versionString) {
return versionString.split(' ')[2].slice('go'.length);
}
exports.parseGoVersion = parseGoVersion;
function convertEnvStringToJson(envString) {
const envArray = envString.split('\n');
const envObject = {};
envArray.forEach(envVar => {
const [key, value] = envVar.split(/=(?=")/);
envObject[key] = value === null || value === void 0 ? void 0 : value.replace(/"/g, '');
});
return JSON.stringify(envObject);
}
exports.convertEnvStringToJson = convertEnvStringToJson;
function resolveVersionInput() {
let version = core.getInput('go-version');
const versionFilePath = core.getInput('go-version-file');
Expand Down
34 changes: 28 additions & 6 deletions src/main.ts
Expand Up @@ -63,13 +63,23 @@ export async function run() {

const goPath = await io.which('go');
const goVersion = (cp.execSync(`${goPath} version`) || '').toString();
const goEnv = (cp.execSync(`${goPath} env`) || '').toString();
const goEnvJson = JSON.parse(convertEnvStringToJson(goEnv));
const parsedGoVersion = parseGoVersion(goVersion);

// Go versions less that 1.16 do not have the GOVERSION environment variable
if (semver.lt(parsedGoVersion, '1.16.0')) {
goEnvJson['GOVERSION'] = 'go' + parsedGoVersion;
}

core.info(goVersion);

if (cache && isCacheFeatureAvailable()) {
const packageManager = 'default';
const cacheDependencyPath = core.getInput('cache-dependency-path');
try {
await restoreCache(
parseGoVersion(goVersion),
parsedGoVersion,
packageManager,
cacheDependencyPath
);
Expand All @@ -82,13 +92,13 @@ export async function run() {
const matchersPath = path.join(__dirname, '../..', 'matchers.json');
core.info(`##[add-matcher]${matchersPath}`);

// output the version actually being used
core.info(goVersion);

core.setOutput('go-version', parseGoVersion(goVersion));
core.setOutput('go-version', parsedGoVersion);
core.setOutput('go-path', goEnvJson['GOPATH']);
core.setOutput('go-cache', goEnvJson['GOCACHE']);
core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);
core.setOutput('go-env', goEnvJson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @lucacome

Can you please remove the most outputs except these 3 ones

    core.setOutput('go-version', parsedGoVersion);
    core.setOutput('go-cache', goEnvJson['GOCACHE']);
    core.setOutput('go-mod-cache', goEnvJson['GOMODCACHE']);

The others does not seem relate to any real-world use cases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dsame

I'm personally interested in go-path so I would like to keep that one as well 😅
Are you saying to also remove core.setOutput('go-env', goEnvJson); ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still of the opinion that the entire go env -json should be exposed in its entirety - I find most of the vars useful.


core.startGroup('go env');
const goEnv = (cp.execSync(`${goPath} env`) || '').toString();
core.info(goEnv);
core.endGroup();
} catch (error) {
Expand Down Expand Up @@ -135,6 +145,18 @@ export function parseGoVersion(versionString: string): string {
return versionString.split(' ')[2].slice('go'.length);
}

export function convertEnvStringToJson(envString: string): string {
const envArray = envString.split('\n');
const envObject: {[key: string]: string} = {};

envArray.forEach(envVar => {
const [key, value] = envVar.split(/=(?=")/);
envObject[key] = value?.replace(/"/g, '');
});

return JSON.stringify(envObject);
}

function resolveVersionInput(): string {
let version = core.getInput('go-version');
const versionFilePath = core.getInput('go-version-file');
Expand Down