Skip to content

Commit

Permalink
use native throw instead of Result
Browse files Browse the repository at this point in the history
  • Loading branch information
bollwyvl committed Nov 22, 2020
1 parent 6ca11f6 commit 0e849fe
Show file tree
Hide file tree
Showing 12 changed files with 525 additions and 812 deletions.
9 changes: 3 additions & 6 deletions dist/delete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1102,13 +1102,10 @@ function execute(command) {
},
},
};
try {
yield exec.exec(command[0], command.slice(1), options);
}
catch (err) {
return { ok: false, error: err };
const rc = yield exec.exec(command[0], command.slice(1), options);
if (rc !== 0) {
throw new Error(`${command[0]} return error code ${rc}`);
}
return { ok: true, data: "ok" };
});
}
exports.execute = execute;
Expand Down
580 changes: 218 additions & 362 deletions dist/setup/index.js

Large diffs are not rendered by default.

72 changes: 29 additions & 43 deletions src/conda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as core from "@actions/core";
import * as io from "@actions/io";

import { IS_LINUX, IS_MAC, IS_WINDOWS, MINICONDA_DIR_PATH } from "./constants";
import { IShells, Result, TCondaConfig } from "./types";
import { IShells, TCondaConfig } from "./types";
import { execute } from "./utils";

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function condaCommand(
cmd: string[],
useBundled: boolean,
useMamba: boolean = false
): Promise<Result> {
): Promise<void> {
const command = [condaExecutable(useBundled, useMamba), ...cmd];
return await execute(command);
}
Expand All @@ -62,49 +62,39 @@ export async function condaCommand(
export async function applyCondaConfiguration(
condaConfig: TCondaConfig,
useBundled: boolean
): Promise<Result> {
let result: Result;
try {
for (const key of Object.keys(condaConfig)) {
core.info(`"${key}": "${condaConfig[key]}"`);
if (condaConfig[key].length !== 0) {
if (key === "channels") {
// Split by comma and reverse order to preserve higher priority
// as listed in the option
let channels: Array<string> = condaConfig[key].split(",").reverse();
let channel: string;
for (channel of channels) {
result = await condaCommand(
["config", "--add", key, channel],
useBundled,
false
);
if (!result.ok) return result;
}
} else {
result = await condaCommand(
): Promise<void> {
for (const key of Object.keys(condaConfig)) {
core.info(`"${key}": "${condaConfig[key]}"`);
if (condaConfig[key].length !== 0) {
if (key === "channels") {
// Split by comma and reverse order to preserve higher priority
// as listed in the option
let channels: Array<string> = condaConfig[key].split(",").reverse();
let channel: string;
for (channel of channels) {
await condaCommand(
["config", "--add", key, channel],
useBundled,
false
);
}
} else {
try {
await condaCommand(
["config", "--set", key, condaConfig[key]],
useBundled,
false
);
if (!result.ok) return result;
} catch (err) {
core.warning(`Couldn't set conda configuration '${key}'`);
}
}
}
}

result = await condaCommand(
["config", "--show-sources"],
useBundled,
false
);
if (!result.ok) return result;
await condaCommand(["config", "--show-sources"], useBundled, false);

result = await condaCommand(["config", "--show"], useBundled, false);
if (!result.ok) return result;
} catch (err) {
return { ok: false, error: err };
}
return { ok: true, data: "ok" };
await condaCommand(["config", "--show"], useBundled, false);
}

/**
Expand All @@ -115,8 +105,7 @@ export async function condaInit(
useBundled: boolean,
condaConfig: TCondaConfig,
removeProfiles: string
): Promise<Result> {
let result: Result;
): Promise<void> {
let ownPath: string;
const isValidActivate: boolean =
activateEnvironment !== "base" &&
Expand All @@ -130,15 +119,14 @@ export async function condaInit(
if (IS_MAC) {
core.startGroup("Fixing conda folders ownership");
const userName: string = process.env.USER as string;
result = await execute([
await execute([
"sudo",
"chown",
"-R",
`${userName}:staff`,
minicondaPath(useBundled),
]);
core.endGroup();
if (!result.ok) return result;
} else if (IS_WINDOWS) {
for (let folder of [
"condabin/",
Expand All @@ -150,9 +138,8 @@ export async function condaInit(
ownPath = path.join(minicondaPath(useBundled), folder);
if (fs.existsSync(ownPath)) {
core.startGroup(`Fixing ${folder} ownership`);
result = await execute(["takeown", "/f", ownPath, "/r", "/d", "y"]);
await execute(["takeown", "/f", ownPath, "/r", "/d", "y"]);
core.endGroup();
if (!result.ok) return result;
}
}
}
Expand Down Expand Up @@ -279,5 +266,4 @@ export async function condaInit(
fs.appendFileSync(filePath, text);
}
});
return { ok: true, data: "ok" };
}
17 changes: 5 additions & 12 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as path from "path";
import * as core from "@actions/core";

import { minicondaPath, condaCommand } from "./conda";
import { Result } from "./types";

/**
* Check if a given conda environment exists
Expand All @@ -26,30 +25,24 @@ export async function createTestEnvironment(
activateEnvironment: string,
useBundled: boolean,
useMamba: boolean
): Promise<Result> {
let result: Result;
): Promise<void> {
if (
activateEnvironment !== "root" &&
activateEnvironment !== "base" &&
activateEnvironment !== ""
) {
if (!environmentExists(activateEnvironment, useBundled)) {
core.startGroup("Create test environment...");
result = await condaCommand(
await condaCommand(
["create", "--name", activateEnvironment],
useBundled,
useMamba
);
if (!result.ok) return result;
core.endGroup();
}
} else {
return {
ok: false,
error: new Error(
'To activate "base" environment use the "auto-activate-base" action input!'
),
};
throw new Error(
'To activate "base" environment use the "auto-activate-base" action input!'
);
}
return { ok: true, data: "ok" };
}
16 changes: 4 additions & 12 deletions src/installer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as core from "@actions/core";
import * as io from "@actions/io";
import * as tc from "@actions/tool-cache";

import { ILocalInstallerOpts, Result } from "../types";
import { ILocalInstallerOpts } from "../types";
import { minicondaPath } from "../conda";
import { execute } from "../utils";

Expand Down Expand Up @@ -89,7 +89,7 @@ export async function ensureLocalInstaller(
export async function runInstaller(
installerPath: string,
useBundled: boolean
): Promise<Result> {
): Promise<void> {
const outputPath: string = minicondaPath(useBundled);
const installerExtension = path.extname(installerPath);
let command: string[];
Expand All @@ -111,18 +111,10 @@ export async function runInstaller(
command = ["bash", installerPath, "-f", "-b", "-p", outputPath];
break;
default:
return {
ok: false,
error: Error(`Unknown installer extension: ${installerExtension}`),
};
throw new Error(`Unknown installer extension: ${installerExtension}`);
}

core.info(`Install Command:\n\t${command}`);

try {
return await execute(command);
} catch (err) {
core.error(err);
return { ok: false, error: err };
}
return await execute(command);
}
31 changes: 11 additions & 20 deletions src/installer/download-miniconda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
MINICONDA_BASE_URL,
OS_NAMES,
} from "../constants";
import { Result } from "../types";

/**
* List available Miniconda versions
Expand Down Expand Up @@ -48,11 +47,11 @@ export async function downloadMiniconda(
pythonMajorVersion: number,
minicondaVersion: string,
architecture: string
): Promise<Result> {
): Promise<string> {
// Check valid arch
const arch: string = ARCHITECTURES[architecture];
if (!arch) {
return { ok: false, error: new Error(`Invalid arch "${architecture}"!`) };
throw new Error(`Invalid arch "${architecture}"!`);
}

let extension: string = IS_UNIX ? "sh" : "exe";
Expand All @@ -64,24 +63,16 @@ export async function downloadMiniconda(
let versions: string[] = await minicondaVersions(arch);
if (versions) {
if (!versions.includes(minicondaInstallerName)) {
return {
ok: false,
error: new Error(
`Invalid miniconda version!\n\nMust be among ${versions.toString()}`
),
};
throw new Error(
`Invalid miniconda version!\n\nMust be among ${versions.toString()}`
);
}
}

try {
const downloadPath = await ensureLocalInstaller({
url: MINICONDA_BASE_URL + minicondaInstallerName,
tool: `Miniconda${pythonMajorVersion}`,
version: minicondaVersion,
arch: arch,
});
return { ok: true, data: downloadPath };
} catch (error) {
return { ok: false, error };
}
return await ensureLocalInstaller({
url: MINICONDA_BASE_URL + minicondaInstallerName,
tool: `Miniconda${pythonMajorVersion}`,
version: minicondaVersion,
arch: arch,
});
}
10 changes: 2 additions & 8 deletions src/installer/download-url.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { Result } from "../types";
import { ensureLocalInstaller } from "./base";

/**
* @param url A URL for a file with the CLI of a `constructor`-built artifact
*/
export async function downloadCustomInstaller(url: string): Promise<Result> {
try {
const downloadPath = await ensureLocalInstaller({ url });
return { ok: true, data: downloadPath };
} catch (error) {
return { ok: false, error };
}
export async function downloadCustomInstaller(url: string): Promise<string> {
return await ensureLocalInstaller({ url });
}
Loading

0 comments on commit 0e849fe

Please sign in to comment.