feat: add user-access package

This commit is contained in:
Amin Yahyaabadi 2022-08-07 21:03:16 -07:00
parent b4a83891fb
commit b38bf66f56
14 changed files with 151 additions and 23 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -69,6 +69,7 @@
"time-delta": "github:aminya/time-delta#69d91a41cef28e569be9a2991129f5f7d1f0d00e",
"ubuntu-version": "^2.0.0",
"untildify-user": "workspace:*",
"user-access": "workspace:*",
"which": "^2.0.2"
},
"devDependencies": {

View File

@ -48,7 +48,7 @@ Execute a command as root if sudo is available. Otherwise executes the command n
- program (`string`) - The program to spawn
- args (`string[]`) - The command arguments
- execOptions (`execa.SyncOptions`) - The options passed to `execa`. Defaults to `{ stdio: "inherit" }`
- execOptions (`execa.SyncOptions`) - The options passed to `execa`. Defaults to `{ stdio: "inherit", shell: true }`
**returns:** execa.ExecaSyncReturnValue<string>
@ -60,7 +60,7 @@ Asynchronously execute a command as root if sudo is available. Otherwise execute
- program (`string`) - The program to spawn
- args (`string[]`) - The command arguments
- execOptions (`execa.Options`) - The options passed to `execa`. Defaults to `{ stdio: "inherit" }`
- execOptions (`execa.Options`) - The options passed to `execa`. Defaults to `{ stdio: "inherit", shell: true }`
**returns:** execa.ExecaChildProcess<string>

View File

@ -0,0 +1,38 @@
<h1 align="center">untildify-user</h1>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000" />
<a href="#" target="_blank">
<img alt="License: Apache--2.0" src="https://img.shields.io/badge/License-Apache--2.0-yellow.svg" />
</a>
</p>
> Untildify a path for the current user even if it is root
## Install
```sh
npm install --save untildify-user
```
## Usage
<!-- INSERT GENERATED DOCS START -->
### `untildifyUser` (function)
**Parameters:**
- path (`string`)
**returns:** any
<!-- INSERT GENERATED DOCS END -->
## 🤝 Contributing
You can sponsor my work here:
https://github.com/sponsors/aminya
Pull requests, issues and feature requests are welcome.
See the [Contributing guide](https://github.com/aminya/setup-cpp/blob/master/CONTRIBUTING.md).

View File

@ -1,7 +1,7 @@
{
"name": "untildify-user",
"version": "1.0.0",
"description": "Untildify a path for the current user",
"description": "Untildify a path for the current user even if it is root",
"homepage": "https://github.com/aminya/setup-cpp",
"license": "Apache-2.0",
"author": "Amin Yahyaabadi",

View File

@ -0,0 +1,41 @@
<h1 align="center">user-access</h1>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000" />
<a href="#" target="_blank">
<img alt="License: Apache--2.0" src="https://img.shields.io/badge/License-Apache--2.0-yellow.svg" />
</a>
</p>
> Give user access to the given path
## Install
```sh
npm install --save user-access
```
## Usage
<!-- INSERT GENERATED DOCS START -->
### `giveUserAccess` (function)
Give the user access to the given path and its sub-directories. It changes the owner to the SUDO_USER. This allows
the user to use the folder without sudo
**Parameters:**
- path (`string`) - The path to give the user access to
**returns:** void
<!-- INSERT GENERATED DOCS END -->
## 🤝 Contributing
You can sponsor my work here:
https://github.com/sponsors/aminya
Pull requests, issues and feature requests are welcome.
See the [Contributing guide](https://github.com/aminya/setup-cpp/blob/master/CONTRIBUTING.md).

View File

@ -0,0 +1,29 @@
{
"name": "user-access",
"version": "1.0.0",
"description": "Give user access to the given path",
"homepage": "https://github.com/aminya/setup-cpp",
"license": "Apache-2.0",
"author": "Amin Yahyaabadi",
"main": "./dist/index.js",
"source": "./src/index.ts",
"scripts": {
"build": "tsc"
},
"dependencies": {
"root-tools": "workspace:*"
},
"keywords": [
"chown",
"recursive",
"user-access",
"user",
"access",
"path",
"fs",
"home",
"directory",
"shell",
"bash"
]
}

View File

@ -0,0 +1,17 @@
import { isSudo, execRootSync } from "root-tools"
/**
* Give the user access to the given path and its sub-directories. It changes the owner to the SUDO_USER. This allows
* the user to use the folder without sudo
*
* @param path The path to give the user access to
*/
export function giveUserAccess(path: string) {
if (
(process.platform === "linux" || process.platform === "darwin") &&
isSudo() &&
process.env.SUDO_USER !== undefined
) {
execRootSync("chown", ["-R", process.env.SUDO_USER, path], { cwd: path, stdio: "inherit", shell: true })
}
}

View File

@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}

View File

@ -56,6 +56,7 @@ importers:
typescript: ^4.7.4
ubuntu-version: ^2.0.0
untildify-user: workspace:*
user-access: workspace:*
which: ^2.0.2
dependencies:
'@actions/core': 1.9.0
@ -76,6 +77,7 @@ importers:
time-delta: github.com/aminya/time-delta/69d91a41cef28e569be9a2991129f5f7d1f0d00e
ubuntu-version: 2.0.0
untildify-user: link:packages/untildify-user
user-access: link:packages/user-access
which: 2.0.2
devDependencies:
'@types/cross-spawn': 6.0.2
@ -138,6 +140,12 @@ importers:
root-tools: link:../root-tools
untildify: 4.0.0
packages/user-access:
specifiers:
root-tools: workspace:*
dependencies:
root-tools: link:../root-tools
packages:
/@actions/cache/3.0.0:

View File

@ -1,13 +0,0 @@
import { isSudo } from "root-tools"
import { execRootSync } from "root-tools"
/// change the owner to the SUDO_USER. This is required so the user can use the folder without sudo
export function folderUserAccess(folder: string) {
if (
(process.platform === "linux" || process.platform === "darwin") &&
isSudo() &&
process.env.SUDO_USER !== undefined
) {
execRootSync("chown", ["-R", process.env.SUDO_USER, folder], { cwd: folder, stdio: "inherit", shell: true })
}
}

View File

@ -3,7 +3,7 @@ import { mkdirP } from "@actions/io"
import which from "which"
import { setupSevenZip } from "../../sevenzip/sevenzip"
import { warning } from "../io/io"
import { folderUserAccess } from "../fs/userAccess"
import { giveUserAccess } from "user-access"
export { extractTar, extractXar } from "@actions/tool-cache"
let sevenZip: string | undefined
@ -11,7 +11,7 @@ let sevenZip: string | undefined
/// Extract 7z using 7z
export async function extract7Zip(file: string, dest: string) {
await execa(await getSevenZip(), ["x", file, `-o${dest}`, "-y"], { stdio: "inherit" })
folderUserAccess(dest)
giveUserAccess(dest)
return dest
}
@ -55,6 +55,6 @@ export async function extractTarByExe(file: string, dest: string, flags = ["--st
}
}
folderUserAccess(dest)
giveUserAccess(dest)
return dest
}

View File

@ -12,7 +12,7 @@ import { isArch } from "../utils/env/isArch"
import { hasDnf } from "../utils/env/hasDnf"
import { setupDnfPack } from "../utils/setup/setupDnfPack"
import { isUbuntu } from "../utils/env/isUbuntu"
import { folderUserAccess } from "../utils/fs/userAccess"
import { giveUserAccess } from "user-access"
let hasVCPKG = false
@ -53,7 +53,7 @@ export async function setupVcpkg(_version: string, setupDir: string, _arch: stri
execa.sync(addShellExtension(addShellHere("bootstrap-vcpkg")), { cwd: setupDir, shell: true, stdio: "inherit" })
folderUserAccess(setupDir)
giveUserAccess(setupDir)
await addPath(setupDir)
// eslint-disable-next-line require-atomic-updates