Skip to content

Commit

Permalink
feat: detect if sudo should be used (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
iTrooz committed Oct 18, 2022
1 parent 918c69b commit ac30c9d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 0 additions & 1 deletion .github/workflows/manual-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ jobs:
- uses: ./
with:
limit-access-to-actor: ${{ matrix.limit-access-to-actor }}
sudo: false
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ inputs:
sudo:
description: 'If apt should be executed with sudo or without'
required: false
default: 'true'
default: 'auto'
install-dependencies:
description: 'Whether or not to install dependencies for tmate on linux (openssh-client, xz-utils)'
required: false
Expand Down
11 changes: 10 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12470,6 +12470,15 @@ var external_child_process_ = __nccwpck_require__(2081);




/**
* @returns {boolean}
*/
const useSudoPrefix = () => {
const input = core.getInput("sudo");
return input === "auto" ? external_os_default().userInfo().uid !== 0 : input === "true";
}

/**
* @param {string} cmd
* @returns {Promise<string>}
Expand Down Expand Up @@ -12570,7 +12579,7 @@ async function run() {
} else if (process.platform === "win32") {
await execShellCommand('pacman -Sy --noconfirm tmate');
} else {
const optionalSudoPrefix = core.getInput("sudo") === "true" ? "sudo " : "";
const optionalSudoPrefix = useSudoPrefix() ? "sudo " : "";
const distro = await getLinuxDistro();
core.debug("linux distro: [" + distro + "]");
if (distro === "alpine") {
Expand Down
9 changes: 9 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
import { spawn } from 'child_process'
import * as core from "@actions/core"
import fs from 'fs'
import os from 'os'

/**
* @returns {boolean}
*/
export const useSudoPrefix = () => {
const input = core.getInput("sudo");
return input === "auto" ? os.userInfo().uid !== 0 : input === "true";
}

/**
* @param {string} cmd
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as github from "@actions/github"
import * as tc from "@actions/tool-cache"
import { Octokit } from "@octokit/rest"

import { execShellCommand, getValidatedInput, getLinuxDistro } from "./helpers"
import { execShellCommand, getValidatedInput, getLinuxDistro, useSudoPrefix } from "./helpers"

const TMATE_LINUX_VERSION = "2.4.0"

Expand All @@ -34,7 +34,7 @@ export async function run() {
} else if (process.platform === "win32") {
await execShellCommand('pacman -Sy --noconfirm tmate');
} else {
const optionalSudoPrefix = core.getInput("sudo") === "true" ? "sudo " : "";
const optionalSudoPrefix = useSudoPrefix() ? "sudo " : "";
const distro = await getLinuxDistro();
core.debug("linux distro: [" + distro + "]");
if (distro === "alpine") {
Expand Down

0 comments on commit ac30c9d

Please sign in to comment.