mirror of
https://github.com/actions/setup-go.git
synced 2025-05-22 02:22:24 +00:00
prefer globally installed bin
This commit is contained in:
@ -1,12 +1,9 @@
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import * as cm from '@actions/core';
|
||||
import * as path from 'path';
|
||||
import * as semver from 'semver';
|
||||
import * as httpm from '@actions/http-client';
|
||||
import * as sys from './system';
|
||||
import {debug} from '@actions/core';
|
||||
import * as cp from 'child_process';
|
||||
import * as fs from 'fs';
|
||||
|
||||
export async function downloadGo(
|
||||
versionSpec: string,
|
||||
@ -37,8 +34,6 @@ export async function downloadGo(
|
||||
// extracts with a root folder that matches the fileName downloaded
|
||||
const toolRoot = path.join(extPath, 'go');
|
||||
toolPath = await tc.cacheDir(toolRoot, 'go', makeSemver(match.version));
|
||||
|
||||
addBinToPath();
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to download version ${versionSpec}: ${error}`);
|
||||
@ -60,17 +55,6 @@ export interface IGoVersion {
|
||||
files: IGoVersionFile[];
|
||||
}
|
||||
|
||||
export async function addBinToPath() {
|
||||
let buf = cp.execSync('go env GOPATH');
|
||||
if (buf) {
|
||||
let d = buf.toString().trim();
|
||||
let bp = path.join(d, 'bin');
|
||||
if (fs.existsSync(bp)) {
|
||||
cm.addPath(bp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function findMatch(
|
||||
versionSpec: string,
|
||||
stable: boolean
|
||||
|
24
src/main.ts
24
src/main.ts
@ -2,6 +2,8 @@ import * as core from '@actions/core';
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import * as installer from './installer';
|
||||
import * as path from 'path';
|
||||
import * as cp from 'child_process';
|
||||
import * as fs from 'fs';
|
||||
|
||||
export async function run() {
|
||||
try {
|
||||
@ -19,6 +21,8 @@ export async function run() {
|
||||
`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`
|
||||
);
|
||||
|
||||
// if there's a globally install go and bin path, prefer that
|
||||
let addedBin = addBinToPath();
|
||||
if (versionSpec) {
|
||||
let installDir: string | undefined = tc.find('go', versionSpec);
|
||||
|
||||
@ -34,6 +38,12 @@ export async function run() {
|
||||
core.exportVariable('GOROOT', installDir);
|
||||
core.addPath(path.join(installDir, 'bin'));
|
||||
console.log('Added go to the path');
|
||||
|
||||
// if the global installed bin wasn't added,
|
||||
// we can add the bin just installed
|
||||
if (!addBinToPath) {
|
||||
addBinToPath();
|
||||
}
|
||||
} else {
|
||||
throw new Error(
|
||||
`Could not find a version that satisfied version spec: ${versionSpec}`
|
||||
@ -48,3 +58,17 @@ export async function run() {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
function addBinToPath(): boolean {
|
||||
let added = false;
|
||||
let buf = cp.execSync('go env GOPATH');
|
||||
if (buf) {
|
||||
let d = buf.toString().trim();
|
||||
let bp = path.join(d, 'bin');
|
||||
if (fs.existsSync(bp)) {
|
||||
core.addPath(bp);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
return added;
|
||||
}
|
||||
|
Reference in New Issue
Block a user