Skip to content

Commit e583d28

Browse files
committed
Refactor, make sudo optional
1 parent 2a99b55 commit e583d28

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

index.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Core = require("@actions/core");
22
const ToolCache = require("@actions/tool-cache");
3+
const IO = require("@actions/io");
34
const Octokit = require("@octokit/request");
45
const fetch = require("node-fetch");
56
const Path = require("path");
@@ -31,8 +32,7 @@ async function run() {
3132
}
3233
await maybeInstallShards(params, func(params));
3334

34-
Core.info("[command]crystal --version");
35-
const {stdout} = await execFile("crystal", ["--version"]);
35+
const {stdout} = await subprocess(["crystal", "--version"]);
3636
Core.info(stdout);
3737
} catch (error) {
3838
Core.setFailed(error);
@@ -76,6 +76,12 @@ function checkVersion(version, allowed) {
7676
throw `Version "${version}" is invalid`;
7777
}
7878

79+
async function subprocess(command, options) {
80+
Core.info("[command]" + command.join(" "));
81+
const [file, ...args] = command;
82+
return execFile(file, args, options);
83+
}
84+
7985
async function installCrystalForLinux({
8086
crystal,
8187
shards,
@@ -86,7 +92,7 @@ async function installCrystalForLinux({
8692
const suffixes = {"x86_64": "linux-x86_64", "x86": "linux-i686"};
8793
checkArch(arch, Object.keys(suffixes));
8894

89-
const p = installAptPackages(
95+
const depsTask = installAptPackages(
9096
"libevent-dev libgmp-dev libpcre3-dev libssl-dev libxml2-dev libyaml-dev".split(" "),
9197
);
9298
const path = await installBinaryRelease({crystal, shards, suffix: suffixes[arch], destination});
@@ -98,7 +104,7 @@ async function installCrystalForLinux({
98104
await FS.unlink(Path.join(path, "bin", "shards"));
99105
} catch (e) {}
100106
}
101-
await p;
107+
await depsTask;
102108
}
103109

104110
async function installCrystalForMac({
@@ -131,11 +137,13 @@ async function installCrystalForMac({
131137

132138
async function installAptPackages(packages) {
133139
Core.info("Installing package dependencies");
134-
const args = [
135-
"-n", "apt-get", "install", "-qy", "--no-install-recommends", "--no-upgrade", "--",
140+
const command = [
141+
"apt-get", "install", "-qy", "--no-install-recommends", "--no-upgrade", "--",
136142
].concat(packages);
137-
Core.info("[command]sudo " + args.join(" "));
138-
const {stdout} = await execFile("sudo", args);
143+
if (await IO.which("sudo")) {
144+
command.unshift("sudo", "-n");
145+
}
146+
const {stdout} = await subprocess(command);
139147
Core.startGroup("Finished installing package dependencies");
140148
Core.info(stdout);
141149
Core.endGroup();
@@ -169,8 +177,7 @@ async function maybeInstallShards({shards, destination}, crystalPromise) {
169177
await crystalPromise;
170178
}
171179
if (shards !== None) {
172-
Core.info("[command]shards --version");
173-
const {stdout} = await execFile("shards", ["--version"]);
180+
const {stdout} = await subprocess(["shards", "--version"]);
174181
Core.info(stdout);
175182
}
176183
}
@@ -186,8 +193,7 @@ async function installShards({shards, destination}, crystalPromise) {
186193
await crystalPromise;
187194

188195
Core.info("Building Shards");
189-
Core.info("[command]make");
190-
const {stdout} = await execFile("make", {cwd: path});
196+
const {stdout} = await subprocess(["make"], {cwd: path});
191197
Core.startGroup("Finished building Shards");
192198
Core.info(stdout);
193199
Core.endGroup();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"homepage": "https://github.com/oprypin/install-crystal",
2626
"dependencies": {
2727
"@actions/core": "^1.2.6",
28+
"@actions/io": "^1.0.2",
2829
"@actions/tool-cache": "^1.6.1",
2930
"@octokit/request": "^5.4.12",
3031
"node-fetch": "^2.6.1"

0 commit comments

Comments
 (0)