Skip to content

Commit

Permalink
prefer globally installed bin
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan MacFarlane committed Mar 26, 2020
1 parent 9c31f59 commit 3d0e382
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
39 changes: 22 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,8 @@ const core = __importStar(__webpack_require__(470));
const tc = __importStar(__webpack_require__(533));
const installer = __importStar(__webpack_require__(749));
const path = __importStar(__webpack_require__(622));
const cp = __importStar(__webpack_require__(129));
const fs = __importStar(__webpack_require__(747));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
Expand All @@ -1291,6 +1293,8 @@ function run() {
// since getting unstable versions should be explicit
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
console.log(`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 = tc.find('go', versionSpec);
if (!installDir) {
Expand All @@ -1302,6 +1306,11 @@ 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}`);
Expand All @@ -1317,6 +1326,19 @@ function run() {
});
}
exports.run = run;
function addBinToPath() {
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;
}


/***/ }),
Expand Down Expand Up @@ -4576,14 +4598,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const tc = __importStar(__webpack_require__(533));
const cm = __importStar(__webpack_require__(470));
const path = __importStar(__webpack_require__(622));
const semver = __importStar(__webpack_require__(280));
const httpm = __importStar(__webpack_require__(539));
const sys = __importStar(__webpack_require__(737));
const core_1 = __webpack_require__(470);
const cp = __importStar(__webpack_require__(129));
const fs = __importStar(__webpack_require__(747));
function downloadGo(versionSpec, stable) {
return __awaiter(this, void 0, void 0, function* () {
let toolPath;
Expand All @@ -4605,7 +4624,6 @@ function downloadGo(versionSpec, stable) {
// extracts with a root folder that matches the fileName downloaded
const toolRoot = path.join(extPath, 'go');
toolPath = yield tc.cacheDir(toolRoot, 'go', makeSemver(match.version));
addBinToPath();
}
}
catch (error) {
Expand All @@ -4615,19 +4633,6 @@ function downloadGo(versionSpec, stable) {
});
}
exports.downloadGo = downloadGo;
function addBinToPath() {
return __awaiter(this, void 0, void 0, function* () {
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);
}
}
});
}
exports.addBinToPath = addBinToPath;
function findMatch(versionSpec, stable) {
return __awaiter(this, void 0, void 0, function* () {
let archFilter = sys.getArch();
Expand Down
16 changes: 0 additions & 16 deletions src/installer.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}`);
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -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}`
Expand All @@ -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;
}

0 comments on commit 3d0e382

Please sign in to comment.