huyab-install-tools 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,79 @@
1
+ Metadata-Version: 2.4
2
+ Name: huyab-install-tools
3
+ Version: 1.0.0
4
+ Summary: Install the generic dev CLI tools (bws, gh, mise, cf, wrangler, biome, cloudflared, browse, gitleaks, jq, rg, fd) with one command. Changes to git, ssh or shell config are opt-in.
5
+ Project-URL: Source, https://github.com/nguyenhuy158/huyabhome/tree/main/python
6
+ Classifier: Environment :: Console
7
+ Classifier: Operating System :: POSIX
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Topic :: System :: Installation/Setup
10
+ Requires-Python: >=3.9
11
+ Description-Content-Type: text/markdown
12
+
13
+ # huyab-install-tools
14
+
15
+ Install the dev tools of a machine with one command. It is the `scripts/install-tools.sh` of [huyabhome](https://github.com/nguyenhuy158/huyabhome) as a pip package. It needs `bash` (Linux or macOS) and no Python dependencies.
16
+
17
+ ```bash
18
+ pip install huyab-install-tools
19
+ install-tools
20
+ ```
21
+
22
+ ## Safe by default
23
+
24
+ A plain `install-tools` installs only the generic CLI tools: bws, gh, mise, cf, wrangler, biome, cloudflared, browse, gitleaks, jq, rg, fd, shellcheck, shfmt, sqlite3.
25
+
26
+ It does **not** change your git config, your ssh config or your shell rc files, does not log in to any service, and does not write a README. Those are separate steps that run only when you ask:
27
+
28
+ | Step | What it does when you turn it on |
29
+ |---|---|
30
+ | `git-identity` | Sets the global git `user.name` and `user.email`. Needs `--git-name` and `--git-email`. |
31
+ | `git-hook` | Sets a global pre-commit hook that scans staged changes with gitleaks (changes `core.hooksPath`). |
32
+ | `ssh` | Adds tailnet hosts to `~/.ssh/config` (userspace Tailscale only). |
33
+ | `shell-env` | Makes new shells export `CLOUDFLARE_API_TOKEN` from bws (edits `~/.bashrc` / `~/.zshrc`). |
34
+ | `tailscale` | Installs Tailscale and logs in with `TS_AUTH_KEY`. Uses `sudo`. |
35
+ | `pipiops` | Clones the author's private `pipiops` repo with your logged-in `gh`. Useless without access to it. |
36
+ | `verify` | Checks the Cloudflare and Browserbase keys stored in your bws. |
37
+ | `readme` | Writes `~/README.md` for the machine. |
38
+
39
+ The tools themselves are installed with `sudo` when the install directory (`/usr/local/bin`) is not writable. Set `INSTALL_DIR` to install somewhere else.
40
+
41
+ ## Command
42
+
43
+ | Option | Does |
44
+ |---|---|
45
+ | (none) | Install the generic tools. |
46
+ | `--with STEP` | Also run a step from the table (repeatable). |
47
+ | `--git-name NAME --git-email EMAIL` | Turn on `git-identity` with your own name and email. |
48
+ | `--skip-auth` | Set `SKIP_AUTH=1`: install only, no login, no key checks. |
49
+ | `--list-steps` | List the steps. |
50
+ | `--path` / `--print` | Print the bundled script path / the script itself. |
51
+ | `--version` | Print the package version. |
52
+
53
+ Environment variables pass through (`BWS_ACCESS_TOKEN`, `TS_AUTH_KEY`, `INSTALL_DIR`, ...).
54
+
55
+ ## Library
56
+
57
+ ```python
58
+ import install_tools
59
+
60
+ install_tools.script_path() # pathlib.Path of the bundled script
61
+ install_tools.run() # generic tools only, returns the exit code
62
+ install_tools.run(enable=["git-hook"], env={"SKIP_AUTH": "1"})
63
+ install_tools.STEPS # the step names
64
+ ```
65
+
66
+ `run()` raises `ValueError` for an unknown step, and for `git-identity` without `GIT_USER_NAME` and `GIT_USER_EMAIL`.
67
+
68
+ The bundled script is the same file as `scripts/install-tools.sh` in the repo, where `curl ... | bash` runs every step. The pip command sets `INSTALL_SKIP` for you; to run the script yourself, use the same variable, for example `INSTALL_SKIP="git-identity ssh" bash script.sh`.
69
+
70
+ ## Release (maintainers)
71
+
72
+ The script is copied here by `pnpm build:install-tools`. Do not edit it by hand.
73
+
74
+ ```bash
75
+ python -m build python/
76
+ twine check python/dist/*
77
+ ```
78
+
79
+ Upload with an API token that is read from a secret store into the environment of the `twine` process only (`TWINE_USERNAME=__token__`, `TWINE_PASSWORD`). Never put the token on a command line or in a file. After the first upload, replace the account-wide token with one limited to this project, or switch to PyPI Trusted Publishing.
@@ -0,0 +1,67 @@
1
+ # huyab-install-tools
2
+
3
+ Install the dev tools of a machine with one command. It is the `scripts/install-tools.sh` of [huyabhome](https://github.com/nguyenhuy158/huyabhome) as a pip package. It needs `bash` (Linux or macOS) and no Python dependencies.
4
+
5
+ ```bash
6
+ pip install huyab-install-tools
7
+ install-tools
8
+ ```
9
+
10
+ ## Safe by default
11
+
12
+ A plain `install-tools` installs only the generic CLI tools: bws, gh, mise, cf, wrangler, biome, cloudflared, browse, gitleaks, jq, rg, fd, shellcheck, shfmt, sqlite3.
13
+
14
+ It does **not** change your git config, your ssh config or your shell rc files, does not log in to any service, and does not write a README. Those are separate steps that run only when you ask:
15
+
16
+ | Step | What it does when you turn it on |
17
+ |---|---|
18
+ | `git-identity` | Sets the global git `user.name` and `user.email`. Needs `--git-name` and `--git-email`. |
19
+ | `git-hook` | Sets a global pre-commit hook that scans staged changes with gitleaks (changes `core.hooksPath`). |
20
+ | `ssh` | Adds tailnet hosts to `~/.ssh/config` (userspace Tailscale only). |
21
+ | `shell-env` | Makes new shells export `CLOUDFLARE_API_TOKEN` from bws (edits `~/.bashrc` / `~/.zshrc`). |
22
+ | `tailscale` | Installs Tailscale and logs in with `TS_AUTH_KEY`. Uses `sudo`. |
23
+ | `pipiops` | Clones the author's private `pipiops` repo with your logged-in `gh`. Useless without access to it. |
24
+ | `verify` | Checks the Cloudflare and Browserbase keys stored in your bws. |
25
+ | `readme` | Writes `~/README.md` for the machine. |
26
+
27
+ The tools themselves are installed with `sudo` when the install directory (`/usr/local/bin`) is not writable. Set `INSTALL_DIR` to install somewhere else.
28
+
29
+ ## Command
30
+
31
+ | Option | Does |
32
+ |---|---|
33
+ | (none) | Install the generic tools. |
34
+ | `--with STEP` | Also run a step from the table (repeatable). |
35
+ | `--git-name NAME --git-email EMAIL` | Turn on `git-identity` with your own name and email. |
36
+ | `--skip-auth` | Set `SKIP_AUTH=1`: install only, no login, no key checks. |
37
+ | `--list-steps` | List the steps. |
38
+ | `--path` / `--print` | Print the bundled script path / the script itself. |
39
+ | `--version` | Print the package version. |
40
+
41
+ Environment variables pass through (`BWS_ACCESS_TOKEN`, `TS_AUTH_KEY`, `INSTALL_DIR`, ...).
42
+
43
+ ## Library
44
+
45
+ ```python
46
+ import install_tools
47
+
48
+ install_tools.script_path() # pathlib.Path of the bundled script
49
+ install_tools.run() # generic tools only, returns the exit code
50
+ install_tools.run(enable=["git-hook"], env={"SKIP_AUTH": "1"})
51
+ install_tools.STEPS # the step names
52
+ ```
53
+
54
+ `run()` raises `ValueError` for an unknown step, and for `git-identity` without `GIT_USER_NAME` and `GIT_USER_EMAIL`.
55
+
56
+ The bundled script is the same file as `scripts/install-tools.sh` in the repo, where `curl ... | bash` runs every step. The pip command sets `INSTALL_SKIP` for you; to run the script yourself, use the same variable, for example `INSTALL_SKIP="git-identity ssh" bash script.sh`.
57
+
58
+ ## Release (maintainers)
59
+
60
+ The script is copied here by `pnpm build:install-tools`. Do not edit it by hand.
61
+
62
+ ```bash
63
+ python -m build python/
64
+ twine check python/dist/*
65
+ ```
66
+
67
+ Upload with an API token that is read from a secret store into the environment of the `twine` process only (`TWINE_USERNAME=__token__`, `TWINE_PASSWORD`). Never put the token on a command line or in a file. After the first upload, replace the account-wide token with one limited to this project, or switch to PyPI Trusted Publishing.
@@ -0,0 +1,29 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "huyab-install-tools"
7
+ version = "1.0.0"
8
+ description = "Install the generic dev CLI tools (bws, gh, mise, cf, wrangler, biome, cloudflared, browse, gitleaks, jq, rg, fd) with one command. Changes to git, ssh or shell config are opt-in."
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ dependencies = []
12
+ classifiers = [
13
+ "Environment :: Console",
14
+ "Operating System :: POSIX",
15
+ "Programming Language :: Python :: 3",
16
+ "Topic :: System :: Installation/Setup",
17
+ ]
18
+
19
+ [project.urls]
20
+ Source = "https://github.com/nguyenhuy158/huyabhome/tree/main/python"
21
+
22
+ [project.scripts]
23
+ install-tools = "install_tools.cli:main"
24
+
25
+ [tool.setuptools.packages.find]
26
+ where = ["src"]
27
+
28
+ [tool.setuptools.package-data]
29
+ install_tools = ["install-tools.sh"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,79 @@
1
+ Metadata-Version: 2.4
2
+ Name: huyab-install-tools
3
+ Version: 1.0.0
4
+ Summary: Install the generic dev CLI tools (bws, gh, mise, cf, wrangler, biome, cloudflared, browse, gitleaks, jq, rg, fd) with one command. Changes to git, ssh or shell config are opt-in.
5
+ Project-URL: Source, https://github.com/nguyenhuy158/huyabhome/tree/main/python
6
+ Classifier: Environment :: Console
7
+ Classifier: Operating System :: POSIX
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Topic :: System :: Installation/Setup
10
+ Requires-Python: >=3.9
11
+ Description-Content-Type: text/markdown
12
+
13
+ # huyab-install-tools
14
+
15
+ Install the dev tools of a machine with one command. It is the `scripts/install-tools.sh` of [huyabhome](https://github.com/nguyenhuy158/huyabhome) as a pip package. It needs `bash` (Linux or macOS) and no Python dependencies.
16
+
17
+ ```bash
18
+ pip install huyab-install-tools
19
+ install-tools
20
+ ```
21
+
22
+ ## Safe by default
23
+
24
+ A plain `install-tools` installs only the generic CLI tools: bws, gh, mise, cf, wrangler, biome, cloudflared, browse, gitleaks, jq, rg, fd, shellcheck, shfmt, sqlite3.
25
+
26
+ It does **not** change your git config, your ssh config or your shell rc files, does not log in to any service, and does not write a README. Those are separate steps that run only when you ask:
27
+
28
+ | Step | What it does when you turn it on |
29
+ |---|---|
30
+ | `git-identity` | Sets the global git `user.name` and `user.email`. Needs `--git-name` and `--git-email`. |
31
+ | `git-hook` | Sets a global pre-commit hook that scans staged changes with gitleaks (changes `core.hooksPath`). |
32
+ | `ssh` | Adds tailnet hosts to `~/.ssh/config` (userspace Tailscale only). |
33
+ | `shell-env` | Makes new shells export `CLOUDFLARE_API_TOKEN` from bws (edits `~/.bashrc` / `~/.zshrc`). |
34
+ | `tailscale` | Installs Tailscale and logs in with `TS_AUTH_KEY`. Uses `sudo`. |
35
+ | `pipiops` | Clones the author's private `pipiops` repo with your logged-in `gh`. Useless without access to it. |
36
+ | `verify` | Checks the Cloudflare and Browserbase keys stored in your bws. |
37
+ | `readme` | Writes `~/README.md` for the machine. |
38
+
39
+ The tools themselves are installed with `sudo` when the install directory (`/usr/local/bin`) is not writable. Set `INSTALL_DIR` to install somewhere else.
40
+
41
+ ## Command
42
+
43
+ | Option | Does |
44
+ |---|---|
45
+ | (none) | Install the generic tools. |
46
+ | `--with STEP` | Also run a step from the table (repeatable). |
47
+ | `--git-name NAME --git-email EMAIL` | Turn on `git-identity` with your own name and email. |
48
+ | `--skip-auth` | Set `SKIP_AUTH=1`: install only, no login, no key checks. |
49
+ | `--list-steps` | List the steps. |
50
+ | `--path` / `--print` | Print the bundled script path / the script itself. |
51
+ | `--version` | Print the package version. |
52
+
53
+ Environment variables pass through (`BWS_ACCESS_TOKEN`, `TS_AUTH_KEY`, `INSTALL_DIR`, ...).
54
+
55
+ ## Library
56
+
57
+ ```python
58
+ import install_tools
59
+
60
+ install_tools.script_path() # pathlib.Path of the bundled script
61
+ install_tools.run() # generic tools only, returns the exit code
62
+ install_tools.run(enable=["git-hook"], env={"SKIP_AUTH": "1"})
63
+ install_tools.STEPS # the step names
64
+ ```
65
+
66
+ `run()` raises `ValueError` for an unknown step, and for `git-identity` without `GIT_USER_NAME` and `GIT_USER_EMAIL`.
67
+
68
+ The bundled script is the same file as `scripts/install-tools.sh` in the repo, where `curl ... | bash` runs every step. The pip command sets `INSTALL_SKIP` for you; to run the script yourself, use the same variable, for example `INSTALL_SKIP="git-identity ssh" bash script.sh`.
69
+
70
+ ## Release (maintainers)
71
+
72
+ The script is copied here by `pnpm build:install-tools`. Do not edit it by hand.
73
+
74
+ ```bash
75
+ python -m build python/
76
+ twine check python/dist/*
77
+ ```
78
+
79
+ Upload with an API token that is read from a secret store into the environment of the `twine` process only (`TWINE_USERNAME=__token__`, `TWINE_PASSWORD`). Never put the token on a command line or in a file. After the first upload, replace the account-wide token with one limited to this project, or switch to PyPI Trusted Publishing.
@@ -0,0 +1,12 @@
1
+ README.md
2
+ pyproject.toml
3
+ src/huyab_install_tools.egg-info/PKG-INFO
4
+ src/huyab_install_tools.egg-info/SOURCES.txt
5
+ src/huyab_install_tools.egg-info/dependency_links.txt
6
+ src/huyab_install_tools.egg-info/entry_points.txt
7
+ src/huyab_install_tools.egg-info/top_level.txt
8
+ src/install_tools/__init__.py
9
+ src/install_tools/__main__.py
10
+ src/install_tools/cli.py
11
+ src/install_tools/install-tools.sh
12
+ tests/test_install_tools.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ install-tools = install_tools.cli:main
@@ -0,0 +1,57 @@
1
+ import os
2
+ import subprocess
3
+ import sys
4
+ from importlib.metadata import PackageNotFoundError, version
5
+ from pathlib import Path
6
+
7
+ SCRIPT_NAME = "install-tools.sh"
8
+ BASH_NOT_FOUND = 127
9
+ STEPS = (
10
+ "verify",
11
+ "git-identity",
12
+ "git-hook",
13
+ "ssh",
14
+ "shell-env",
15
+ "pipiops",
16
+ "tailscale",
17
+ "readme",
18
+ )
19
+ IDENTITY_STEP = "git-identity"
20
+ IDENTITY_VARIABLES = ("GIT_USER_NAME", "GIT_USER_EMAIL")
21
+
22
+ try:
23
+ __version__ = version("huyab-install-tools")
24
+ except PackageNotFoundError:
25
+ __version__ = "0+unknown"
26
+
27
+
28
+ def script_path() -> Path:
29
+ return Path(__file__).with_name(SCRIPT_NAME)
30
+
31
+
32
+ def skipped_steps(enable=()) -> str:
33
+ unknown = sorted(set(enable) - set(STEPS))
34
+ if unknown:
35
+ raise ValueError(f"unknown step: {', '.join(unknown)}")
36
+ return " ".join(step for step in STEPS if step not in enable)
37
+
38
+
39
+ def build_env(env=None, enable=()) -> dict:
40
+ explicit = env or {}
41
+ merged = {**os.environ, "INSTALL_SKIP": skipped_steps(enable), **explicit}
42
+ if IDENTITY_STEP in enable and not all(merged.get(v) for v in IDENTITY_VARIABLES):
43
+ raise ValueError(
44
+ f"step {IDENTITY_STEP} needs {' and '.join(IDENTITY_VARIABLES)}"
45
+ )
46
+ return merged
47
+
48
+
49
+ def run(args=(), env=None, enable=()) -> int:
50
+ merged = build_env(env, enable)
51
+ try:
52
+ return subprocess.run(
53
+ ["bash", str(script_path()), *args], env=merged, check=False
54
+ ).returncode
55
+ except FileNotFoundError:
56
+ print("install-tools: bash not found", file=sys.stderr)
57
+ return BASH_NOT_FOUND
@@ -0,0 +1,3 @@
1
+ from .cli import main
2
+
3
+ raise SystemExit(main())
@@ -0,0 +1,69 @@
1
+ import argparse
2
+ import sys
3
+
4
+ from . import STEPS, __version__, build_env, run, script_path
5
+
6
+
7
+ def build_parser() -> argparse.ArgumentParser:
8
+ parser = argparse.ArgumentParser(
9
+ prog="install-tools",
10
+ description=(
11
+ "Install the generic dev tools (bws, gh, mise, cf, wrangler, biome, "
12
+ "cloudflared, browse, gitleaks, jq, rg, fd, shellcheck, shfmt, sqlite3). "
13
+ "Steps that change your git, ssh or shell config, or log in to a "
14
+ "service, run only when you ask with --with."
15
+ ),
16
+ )
17
+ parser.add_argument(
18
+ "--with",
19
+ dest="steps",
20
+ action="append",
21
+ default=[],
22
+ choices=STEPS,
23
+ metavar="STEP",
24
+ help="also run this step (repeatable): " + ", ".join(STEPS),
25
+ )
26
+ parser.add_argument("--git-name", help="git user.name, for --with git-identity")
27
+ parser.add_argument("--git-email", help="git user.email, for --with git-identity")
28
+ parser.add_argument(
29
+ "--skip-auth", action="store_true", help="install only, no login (SKIP_AUTH=1)"
30
+ )
31
+ parser.add_argument("--list-steps", action="store_true", help="list the steps")
32
+ parser.add_argument("--path", action="store_true", help="print the script path")
33
+ parser.add_argument("--print", action="store_true", help="print the script")
34
+ parser.add_argument("--version", action="version", version=__version__)
35
+ return parser
36
+
37
+
38
+ def env_from(args) -> dict:
39
+ env = {}
40
+ if args.skip_auth:
41
+ env["SKIP_AUTH"] = "1"
42
+ if args.git_name:
43
+ env["GIT_USER_NAME"] = args.git_name
44
+ if args.git_email:
45
+ env["GIT_USER_EMAIL"] = args.git_email
46
+ return env
47
+
48
+
49
+ def main(argv=None) -> int:
50
+ parser = build_parser()
51
+ args = parser.parse_args(argv)
52
+ if args.path:
53
+ print(script_path())
54
+ return 0
55
+ if args.print:
56
+ sys.stdout.write(script_path().read_text())
57
+ return 0
58
+ if args.list_steps:
59
+ print("\n".join(STEPS))
60
+ return 0
61
+ steps = list(args.steps)
62
+ if args.git_name or args.git_email:
63
+ steps.append("git-identity")
64
+ env = env_from(args)
65
+ try:
66
+ build_env(env, steps)
67
+ except ValueError as error:
68
+ parser.error(str(error))
69
+ return run(env=env, enable=steps)
@@ -0,0 +1,764 @@
1
+ #!/usr/bin/env bash
2
+ # GENERATED by scripts/build-install-tools.mjs from scripts/install-tools/*.sh. Edit those files, then run: pnpm build:install-tools
3
+ # Installs bws (Bitwarden Secrets Manager CLI), gh (GitHub CLI, latest release),
4
+ # mise (latest release), cf (Cloudflare CLI, beta), wrangler, biome (latest, npm), cloudflared,
5
+ # browse (Browserbase CLI), gitleaks, dev utils (jq, rg, fd, shellcheck, shfmt, sqlite3), Tailscale
6
+ # and pipiops (private repo: the pipimcp MCP server and the pipicli command, cloned with the logged-in gh).
7
+ # Also sets the global git identity, pull.rebase and a global pre-commit hook that scans
8
+ # staged changes with gitleaks.
9
+ # Linux x86_64 / arm64 and macOS. Idempotent: skips tools already installed.
10
+ # gh, mise and biome are the exception: they are upgraded when a newer release exists.
11
+ #
12
+ # Auth model:
13
+ # bws reads its token from the BWS_ACCESS_TOKEN env var.
14
+ # cf uses CLOUDFLARE_API_TOKEN, read from the bws secret of that name.
15
+ # New shells export it too: ~/.config/install-tools/env.sh (no secret stored) is
16
+ # sourced from ~/.bashrc / ~/.zshrc and reads bws when BWS_ACCESS_TOKEN is set.
17
+ # browse uses BB_API_KEY, read from the bws secret of that name.
18
+ # tailscale logs in with the TS_AUTH_KEY env var, else the TS_AUTH_KEY bws secret.
19
+ # pipimcp/pipicli use PIPIMCP_PR_WORKSPACE_TOKEN: env var, else ~/.config/pipimcp/pr-workspace.token,
20
+ # else the bws secret of that name. This script only reports which source is found.
21
+ # Without BWS_ACCESS_TOKEN (or TS_AUTH_KEY for tailscale) the tools are installed but not authenticated.
22
+ # pipiops is skipped, with a message, when gh is not logged in (gh auth login) or Node is too old.
23
+ # In containers without TUN/systemd tailscaled runs in userspace mode and ~/.ssh/config
24
+ # routes tailnet hosts through `tailscale nc`.
25
+ # Set SKIP_AUTH=1 to install only.
26
+ # Set INSTALL_SKIP to a space-separated list of steps to skip (default: none):
27
+ # verify git-identity git-hook ssh shell-env pipiops tailscale readme
28
+ # The pip package huyab-install-tools skips all of them unless asked.
29
+ # Finishes by writing a README.md for this machine (README_PATH, default ~/README.md).
30
+ set -euo pipefail
31
+
32
+ INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
33
+ NODE_MIN_MAJOR=22
34
+ NODE_MIN_MINOR=18
35
+ GIT_USER_NAME="${GIT_USER_NAME:-huy}"
36
+ GIT_USER_EMAIL="${GIT_USER_EMAIL:-huy.ntq02@gmail.com}"
37
+ GIT_HOOKS_DIR="${GIT_HOOKS_DIR:-$HOME/.config/git/hooks}"
38
+ DEV_UTILS="${DEV_UTILS:-jq:jq rg:ripgrep fd:fd-find shellcheck:shellcheck shfmt:shfmt sqlite3:sqlite3}"
39
+
40
+ detect_platform() {
41
+ case "$(uname -s)-$(uname -m)" in
42
+ Linux-x86_64) BWS_TARGET="x86_64-unknown-linux-gnu"; CLOUDFLARED_ASSET="cloudflared-linux-amd64"; GH_ARCH="amd64"; MISE_ARCH="x64"; GITLEAKS_ARCH="x64" ;;
43
+ Linux-aarch64) BWS_TARGET="aarch64-unknown-linux-gnu"; CLOUDFLARED_ASSET="cloudflared-linux-arm64"; GH_ARCH="arm64"; MISE_ARCH="arm64"; GITLEAKS_ARCH="arm64" ;;
44
+ Darwin-arm64) BWS_TARGET="aarch64-apple-darwin" ;;
45
+ Darwin-x86_64) BWS_TARGET="x86_64-apple-darwin" ;;
46
+ *) echo "Unsupported platform: $(uname -s)-$(uname -m)" >&2; exit 1 ;;
47
+ esac
48
+ }
49
+
50
+ detect_platform
51
+
52
+ SUDO=""
53
+ [ -w "$INSTALL_DIR" ] || SUDO="sudo"
54
+
55
+ CURL=(curl -fsSL --proto '=https' --tlsv1.2 --retry 3)
56
+
57
+ TMP_PATHS=()
58
+
59
+ cleanup() {
60
+ [ "${#TMP_PATHS[@]}" -eq 0 ] || rm -rf "${TMP_PATHS[@]}"
61
+ }
62
+ trap cleanup EXIT
63
+
64
+ require_brew() {
65
+ command -v brew >/dev/null 2>&1 || { echo "Homebrew not found; install it first (https://brew.sh)" >&2; exit 1; }
66
+ }
67
+
68
+ step_skipped() {
69
+ case " ${INSTALL_SKIP:-} " in
70
+ *" $1 "*) return 0 ;;
71
+ esac
72
+ return 1
73
+ }
74
+
75
+ run_step() {
76
+ if step_skipped "$1"; then
77
+ echo "$1: skipped (INSTALL_SKIP)"
78
+ return 0
79
+ fi
80
+ "$2"
81
+ }
82
+
83
+ brew_upgrade_or_install() {
84
+ require_brew
85
+ if command -v "$1" >/dev/null 2>&1; then brew upgrade "$1" || true; else brew install "$1"; fi
86
+ }
87
+
88
+ bws_secret() {
89
+ bws secret list | python3 -I -c '
90
+ import json, sys
91
+ secrets = {item["key"]: item["value"] for item in json.load(sys.stdin)}
92
+ value = secrets.get(sys.argv[1])
93
+ if not value:
94
+ sys.exit("bws secret " + sys.argv[1] + " not found")
95
+ sys.stdout.write(value)
96
+ ' "$1"
97
+ }
98
+
99
+ can_authenticate() {
100
+ [ "${SKIP_AUTH:-0}" != "1" ] && [ -n "${BWS_ACCESS_TOKEN:-}" ] && command -v bws >/dev/null 2>&1
101
+ }
102
+
103
+ sha256_of() {
104
+ if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi
105
+ }
106
+
107
+ RELEASE_TAG_CANDIDATES=3
108
+
109
+ latest_tag_with_asset() {
110
+ local repo="$1" template="$2" installed="${3:-}" tag asset
111
+ for tag in $(git ls-remote --tags --refs "https://github.com/$repo" 'v*' \
112
+ | sed 's#.*refs/tags/##' | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -rV | head -n "$RELEASE_TAG_CANDIDATES"); do
113
+ if [ -n "$installed" ] && [ "${tag#v}" = "$installed" ]; then
114
+ echo "$tag"
115
+ return
116
+ fi
117
+ asset="${template//@TAG@/$tag}"
118
+ asset="${asset//@VERSION@/${tag#v}}"
119
+ if "${CURL[@]}" -I -o /dev/null "https://github.com/$repo/releases/download/$tag/$asset" 2>/dev/null; then
120
+ echo "$tag"
121
+ return
122
+ fi
123
+ done
124
+ }
125
+
126
+ fetch_release_asset() {
127
+ local repo="$1" tag="$2" asset="$3" sums="$4" sums_entry="$5" workdir="$6" label="$7"
128
+ "${CURL[@]}" -o "$workdir/$asset" "https://github.com/$repo/releases/download/$tag/$asset"
129
+ "${CURL[@]}" -o "$workdir/$sums" "https://github.com/$repo/releases/download/$tag/$sums"
130
+ [ "$(sha256_of "$workdir/$asset")" = "$(awk -v f="$sums_entry" '$2 == f {print $1}' "$workdir/$sums")" ] \
131
+ || { echo "$label: checksum mismatch for $asset" >&2; exit 1; }
132
+ }
133
+
134
+ install_bws() {
135
+ if command -v bws >/dev/null 2>&1; then
136
+ echo "bws already installed: $(bws --version)"
137
+ return
138
+ fi
139
+ local version workdir
140
+ version="$("${CURL[@]}" -H 'User-Agent: install-tools' https://crates.io/api/v1/crates/bws \
141
+ | python3 -c 'import sys,json;print(json.load(sys.stdin)["crate"]["max_stable_version"])')"
142
+ workdir="$(mktemp -d)"
143
+ TMP_PATHS+=("$workdir")
144
+ "${CURL[@]}" -o "$workdir/bws.zip" \
145
+ "https://github.com/bitwarden/sdk-sm/releases/download/bws-v${version}/bws-${BWS_TARGET}-${version}.zip"
146
+ python3 -I -c 'import sys,zipfile;zipfile.ZipFile(sys.argv[1]).extractall(sys.argv[2])' \
147
+ "$workdir/bws.zip" "$workdir"
148
+ $SUDO install -m 755 "$workdir/bws" "$INSTALL_DIR/bws"
149
+ echo "bws installed: $(bws --version)"
150
+ }
151
+
152
+ gh_installed_version() {
153
+ command -v gh >/dev/null 2>&1 && gh --version | sed -n '1s/^gh version \([0-9.]*\).*/\1/p' || true
154
+ }
155
+
156
+ install_gh_macos() {
157
+ brew_upgrade_or_install gh
158
+ echo "gh installed: $(gh --version | head -1)"
159
+ }
160
+
161
+ install_gh() {
162
+ [ "$(uname -s)" != "Darwin" ] || { install_gh_macos; return; }
163
+ local installed tag version asset workdir
164
+ installed="$(gh_installed_version)"
165
+ tag="$(latest_tag_with_asset cli/cli "gh_@VERSION@_linux_${GH_ARCH}.tar.gz" "$installed")"
166
+ [ -n "$tag" ] || { echo "gh: could not find the latest release tag" >&2; exit 1; }
167
+ version="${tag#v}"
168
+ if [ "$installed" = "$version" ]; then
169
+ echo "gh already at latest: $version"
170
+ return
171
+ fi
172
+ asset="gh_${version}_linux_${GH_ARCH}.tar.gz"
173
+ workdir="$(mktemp -d)"
174
+ TMP_PATHS+=("$workdir")
175
+ fetch_release_asset cli/cli "$tag" "$asset" "gh_${version}_checksums.txt" "$asset" "$workdir" gh
176
+ tar -xzf "$workdir/$asset" -C "$workdir"
177
+ $SUDO install -m 755 "$workdir/gh_${version}_linux_${GH_ARCH}/bin/gh" "$INSTALL_DIR/gh"
178
+ echo "gh installed: $(gh --version | head -1) (was: ${installed:-none})"
179
+ }
180
+
181
+ install_mise_macos() {
182
+ brew_upgrade_or_install mise
183
+ echo "mise installed: $(mise --version | head -1)"
184
+ }
185
+
186
+ install_mise() {
187
+ [ "$(uname -s)" != "Darwin" ] || { install_mise_macos; return; }
188
+ local installed tag version asset workdir
189
+ installed="$(command -v mise >/dev/null 2>&1 && mise --version | awk '{print $1}' || true)"
190
+ tag="$(latest_tag_with_asset jdx/mise "mise-@TAG@-linux-${MISE_ARCH}" "$installed")"
191
+ [ -n "$tag" ] || { echo "mise: could not find the latest release tag" >&2; exit 1; }
192
+ version="${tag#v}"
193
+ if [ "$installed" = "$version" ]; then
194
+ echo "mise already at latest: $version"
195
+ return
196
+ fi
197
+ asset="mise-${tag}-linux-${MISE_ARCH}"
198
+ workdir="$(mktemp -d)"
199
+ TMP_PATHS+=("$workdir")
200
+ fetch_release_asset jdx/mise "$tag" "$asset" SHASUMS256.txt "./$asset" "$workdir" mise
201
+ $SUDO install -m 755 "$workdir/$asset" "$INSTALL_DIR/mise"
202
+ echo "mise installed: $(mise --version | head -1) (was: ${installed:-none})"
203
+ }
204
+
205
+ install_gitleaks_macos() {
206
+ require_brew
207
+ brew install gitleaks
208
+ echo "gitleaks installed: $(gitleaks version | head -1)"
209
+ }
210
+
211
+ install_gitleaks() {
212
+ if command -v gitleaks >/dev/null 2>&1; then
213
+ echo "gitleaks already installed: $(gitleaks version | head -1)"
214
+ return
215
+ fi
216
+ [ "$(uname -s)" != "Darwin" ] || { install_gitleaks_macos; return; }
217
+ local tag version workdir asset
218
+ tag="$(latest_tag_with_asset gitleaks/gitleaks "gitleaks_@VERSION@_linux_${GITLEAKS_ARCH}.tar.gz")"
219
+ [ -n "$tag" ] || { echo "gitleaks: could not find the latest release tag" >&2; exit 1; }
220
+ version="${tag#v}"
221
+ asset="gitleaks_${version}_linux_${GITLEAKS_ARCH}.tar.gz"
222
+ workdir="$(mktemp -d)"
223
+ TMP_PATHS+=("$workdir")
224
+ fetch_release_asset gitleaks/gitleaks "$tag" "$asset" "gitleaks_${version}_checksums.txt" "$asset" "$workdir" gitleaks
225
+ tar -xzf "$workdir/$asset" -C "$workdir" gitleaks
226
+ $SUDO install -m 755 "$workdir/gitleaks" "$INSTALL_DIR/gitleaks"
227
+ echo "gitleaks installed: $(gitleaks version | head -1)"
228
+ }
229
+
230
+ install_dev_utils() {
231
+ local entry missing=()
232
+ for entry in $DEV_UTILS; do
233
+ command -v "${entry%%:*}" >/dev/null 2>&1 || missing+=("${entry##*:}")
234
+ done
235
+ if [ "${#missing[@]}" -gt 0 ]; then
236
+ case "$(uname -s)" in
237
+ Darwin)
238
+ require_brew
239
+ brew install "${missing[@]/fd-find/fd}"
240
+ ;;
241
+ Linux)
242
+ command -v apt-get >/dev/null 2>&1 || { echo "dev utils: apt-get not found; install ${missing[*]} yourself" >&2; exit 1; }
243
+ local apt_sudo=""
244
+ [ "$(id -u)" -eq 0 ] || apt_sudo="sudo"
245
+ $apt_sudo apt-get update -qq
246
+ $apt_sudo apt-get install -y "${missing[@]}"
247
+ ;;
248
+ esac
249
+ fi
250
+ if ! command -v fd >/dev/null 2>&1 && command -v fdfind >/dev/null 2>&1; then
251
+ $SUDO ln -s "$(command -v fdfind)" "$INSTALL_DIR/fd"
252
+ fi
253
+ echo "dev utils ready"
254
+ }
255
+
256
+ setup_git_identity() {
257
+ command -v git >/dev/null 2>&1 || { echo "git not found; skipped git identity" >&2; return 1; }
258
+ git config --global user.name "$GIT_USER_NAME"
259
+ git config --global user.email "$GIT_USER_EMAIL"
260
+ git config --global pull.rebase true
261
+ git config --global init.defaultBranch main
262
+ echo "git: global identity set to $GIT_USER_NAME <$GIT_USER_EMAIL>"
263
+ }
264
+
265
+ setup_gitleaks_hook() {
266
+ local current
267
+ current="$(git config --global --get core.hooksPath || true)"
268
+ if [ -n "$current" ] && [ "$current" != "$GIT_HOOKS_DIR" ]; then
269
+ echo "gitleaks hook: skipped, global core.hooksPath already points to $current" >&2
270
+ return 0
271
+ fi
272
+ mkdir -p "$GIT_HOOKS_DIR"
273
+ cat > "$GIT_HOOKS_DIR/pre-commit" <<'HOOK'
274
+ #!/usr/bin/env bash
275
+ command -v gitleaks >/dev/null 2>&1 || { echo "pre-commit: gitleaks not found, scan skipped" >&2; exit 0; }
276
+ exec gitleaks git --pre-commit --staged --redact --verbose
277
+ HOOK
278
+ chmod 755 "$GIT_HOOKS_DIR/pre-commit"
279
+ git config --global core.hooksPath "$GIT_HOOKS_DIR"
280
+ echo "gitleaks hook: pre-commit scan enabled for every repo ($GIT_HOOKS_DIR)"
281
+ }
282
+
283
+ install_cf() {
284
+ if command -v cf >/dev/null 2>&1 && cf --version 2>/dev/null | grep -qi cloudflare; then
285
+ echo "cf already installed: $(cf --version)"
286
+ return
287
+ fi
288
+ command -v npm >/dev/null 2>&1 || { echo "npm not found; install Node >= ${NODE_MIN_MAJOR}.${NODE_MIN_MINOR}" >&2; exit 1; }
289
+ local node_major node_minor
290
+ node_major="$(node -p 'process.versions.node.split(".")[0]')"
291
+ node_minor="$(node -p 'process.versions.node.split(".")[1]')"
292
+ if [ "$node_major" -lt "$NODE_MIN_MAJOR" ] || { [ "$node_major" -eq "$NODE_MIN_MAJOR" ] && [ "$node_minor" -lt "$NODE_MIN_MINOR" ]; }; then
293
+ echo "cf needs Node >= ${NODE_MIN_MAJOR}.${NODE_MIN_MINOR}, found $(node --version)" >&2
294
+ exit 1
295
+ fi
296
+ npm install --global cf
297
+ echo "cf installed: $(cf --version)"
298
+ echo "Tip: if another 'cf' (Cloud Foundry) is on PATH, use 'cloudflare' instead."
299
+ }
300
+
301
+ install_wrangler() {
302
+ if command -v wrangler >/dev/null 2>&1; then
303
+ echo "wrangler already installed: $(wrangler --version | head -1)"
304
+ return
305
+ fi
306
+ command -v npm >/dev/null 2>&1 || { echo "npm not found; install Node >= ${NODE_MIN_MAJOR}.${NODE_MIN_MINOR}" >&2; exit 1; }
307
+ npm install --global wrangler
308
+ echo "wrangler installed: $(wrangler --version | head -1)"
309
+ }
310
+
311
+ install_biome() {
312
+ command -v npm >/dev/null 2>&1 || { echo "npm not found; install Node >= ${NODE_MIN_MAJOR}.${NODE_MIN_MINOR}" >&2; exit 1; }
313
+ local installed latest
314
+ installed="$(command -v biome >/dev/null 2>&1 && biome --version | sed -n '1s/^Version: *//p' || true)"
315
+ latest="$(npm view @biomejs/biome version)"
316
+ [ -n "$latest" ] || { echo "biome: could not find the latest version" >&2; exit 1; }
317
+ if [ "$installed" = "$latest" ]; then
318
+ echo "biome already at latest: $latest"
319
+ return
320
+ fi
321
+ npm install --global "@biomejs/biome@${latest}"
322
+ echo "biome installed: $(biome --version | head -1) (was: ${installed:-none})"
323
+ }
324
+
325
+ install_browserbase() {
326
+ if npm ls --global --depth=0 browse >/dev/null 2>&1; then
327
+ echo "browse already installed: $(browse --version | head -1)"
328
+ return
329
+ fi
330
+ command -v npm >/dev/null 2>&1 || { echo "npm not found; install Node >= ${NODE_MIN_MAJOR}.${NODE_MIN_MINOR}" >&2; exit 1; }
331
+ npm install --global browse
332
+ echo "browse installed: $(browse --version | head -1)"
333
+ }
334
+
335
+ install_cloudflared() {
336
+ if command -v cloudflared >/dev/null 2>&1; then
337
+ echo "cloudflared already installed: $(cloudflared --version | head -1)"
338
+ return
339
+ fi
340
+ case "$(uname -s)" in
341
+ Linux)
342
+ local workdir
343
+ workdir="$(mktemp -d)"
344
+ TMP_PATHS+=("$workdir")
345
+ "${CURL[@]}" -o "$workdir/cloudflared" \
346
+ "https://github.com/cloudflare/cloudflared/releases/latest/download/${CLOUDFLARED_ASSET}"
347
+ $SUDO install -m 755 "$workdir/cloudflared" "$INSTALL_DIR/cloudflared"
348
+ ;;
349
+ Darwin)
350
+ require_brew
351
+ brew install cloudflared
352
+ ;;
353
+ esac
354
+ echo "cloudflared installed: $(cloudflared --version | head -1)"
355
+ }
356
+
357
+ TS_STATE_DIR="${TS_STATE_DIR:-$HOME/.local/share/tailscale}"
358
+ TS_SOCKET="$TS_STATE_DIR/tailscaled.sock"
359
+
360
+ install_tailscale() {
361
+ if command -v tailscale >/dev/null 2>&1; then
362
+ echo "tailscale already installed: $(tailscale version | head -1)"
363
+ return
364
+ fi
365
+ case "$(uname -s)" in
366
+ Linux)
367
+ "${CURL[@]}" https://tailscale.com/install.sh | sh
368
+ ;;
369
+ Darwin)
370
+ require_brew
371
+ brew install tailscale
372
+ ;;
373
+ esac
374
+ echo "tailscale installed: $(tailscale version | head -1)"
375
+ }
376
+
377
+ use_userspace_tailscale() {
378
+ [ "$(uname -s)" = "Linux" ] || return 1
379
+ [ -e /dev/net/tun ] && [ -d /run/systemd/system ] && return 1
380
+ return 0
381
+ }
382
+
383
+ start_userspace_tailscaled() {
384
+ mkdir -p "$TS_STATE_DIR"
385
+ if [ -S "$TS_SOCKET" ] && tailscale --socket="$TS_SOCKET" version >/dev/null 2>&1; then
386
+ return
387
+ fi
388
+ nohup tailscaled --tun=userspace-networking \
389
+ --state="$TS_STATE_DIR/tailscaled.state" --socket="$TS_SOCKET" \
390
+ >"$TS_STATE_DIR/tailscaled.log" 2>&1 &
391
+ local waited=0
392
+ until [ -S "$TS_SOCKET" ] || [ "$waited" -ge 10 ]; do
393
+ sleep 1
394
+ waited=$((waited + 1))
395
+ done
396
+ [ -S "$TS_SOCKET" ] || { echo "tailscaled did not start; see $TS_STATE_DIR/tailscaled.log" >&2; return 1; }
397
+ }
398
+
399
+ tailscale_running() {
400
+ "$@" status --json 2>/dev/null | python3 -I -c '
401
+ import json, sys
402
+ sys.exit(0 if json.load(sys.stdin).get("BackendState") == "Running" else 1)
403
+ ' 2>/dev/null
404
+ }
405
+
406
+ tailscale_auth_available() {
407
+ [ -n "${TS_AUTH_KEY:-}" ] || can_authenticate
408
+ }
409
+
410
+ write_tailscale_keyfile() {
411
+ if [ -n "${TS_AUTH_KEY:-}" ]; then
412
+ printf '%s' "$TS_AUTH_KEY" > "$1"
413
+ else
414
+ bws_secret TS_AUTH_KEY > "$1"
415
+ fi
416
+ }
417
+
418
+ login_tailscale() {
419
+ [ "${SKIP_AUTH:-0}" != "1" ] || { echo "tailscale: skipped login (SKIP_AUTH=1)"; return; }
420
+ tailscale_auth_available || { echo "tailscale: skipped login (no TS_AUTH_KEY or BWS_ACCESS_TOKEN)"; return; }
421
+ local ts_cmd=(tailscale) up_cmd=(sudo tailscale) keyfile
422
+ if use_userspace_tailscale; then
423
+ start_userspace_tailscaled
424
+ ts_cmd=(tailscale --socket="$TS_SOCKET")
425
+ up_cmd=("${ts_cmd[@]}")
426
+ echo "tailscale: userspace mode (no TUN or systemd); socket $TS_SOCKET"
427
+ fi
428
+ if tailscale_running "${ts_cmd[@]}"; then
429
+ echo "tailscale already connected"
430
+ return
431
+ fi
432
+ [ "$(uname -s)" != "Darwin" ] || sudo brew services start tailscale
433
+ keyfile="$(mktemp)"
434
+ chmod 600 "$keyfile"
435
+ TMP_PATHS+=("$keyfile")
436
+ write_tailscale_keyfile "$keyfile"
437
+ "${up_cmd[@]}" up --auth-key="file:$keyfile"
438
+ echo "tailscale connected"
439
+ }
440
+
441
+ tailnet_peer_hosts() {
442
+ tailscale --socket="$TS_SOCKET" status --json | python3 -I -c '
443
+ import json, re, sys
444
+ status = json.load(sys.stdin)
445
+ names = []
446
+ for peer in status.get("Peer", {}).values():
447
+ for name in (peer["HostName"], peer["DNSName"].rstrip(".")):
448
+ if re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9.-]*", name) and name != "localhost":
449
+ names.append(name)
450
+ print(" ".join(sorted(set(names))), "100.*")
451
+ '
452
+ }
453
+
454
+ write_ssh_tailnet_block() {
455
+ local config="$1" hosts="$2" marker_begin="# BEGIN install-tools tailnet" marker_end="# END install-tools tailnet"
456
+ python3 -I - "$config" "$marker_begin" "$marker_end" <<PY
457
+ import sys
458
+ config, begin, end = sys.argv[1:4]
459
+ text = open(config).read()
460
+ if begin in text:
461
+ head, rest = text.split(begin, 1)
462
+ text = head + rest.split(end, 1)[1].lstrip("\n")
463
+ block = """$marker_begin
464
+ Host $hosts
465
+ ProxyCommand tailscale --socket=$TS_SOCKET nc %h %p
466
+ $marker_end
467
+ """
468
+ open(config, "w").write(text.rstrip("\n") + ("\n\n" if text.strip() else "") + block)
469
+ PY
470
+ }
471
+
472
+ setup_tailnet_ssh() {
473
+ use_userspace_tailscale || return 0
474
+ [ -S "$TS_SOCKET" ] || return 0
475
+ local config="$HOME/.ssh/config"
476
+ mkdir -p "$HOME/.ssh"
477
+ chmod 700 "$HOME/.ssh"
478
+ touch "$config"
479
+ chmod 600 "$config"
480
+ write_ssh_tailnet_block "$config" "$(tailnet_peer_hosts)"
481
+ echo "ssh: tailnet hosts routed through tailscale nc ($config)"
482
+ }
483
+
484
+ PIPIOPS_REPO="${PIPIOPS_REPO:-nguyenhuy158/pipiops}"
485
+ PIPIOPS_DIR="${PIPIOPS_DIR:-$HOME/.local/share/pipiops}"
486
+ PIPIOPS_TOKEN_NAME="PIPIMCP_PR_WORKSPACE_TOKEN"
487
+ PIPIOPS_TOKEN_FILE="$HOME/.config/pipimcp/pr-workspace.token"
488
+ PIPIOPS_NODE_MIN_MAJOR=22
489
+
490
+ pipiops_git() {
491
+ git -c credential.helper= -c 'credential.helper=!gh auth git-credential' "$@"
492
+ }
493
+
494
+ pipiops_token_source() {
495
+ if [ -n "${PIPIMCP_PR_WORKSPACE_TOKEN:-}" ]; then
496
+ echo "env"
497
+ elif [ -s "$PIPIOPS_TOKEN_FILE" ]; then
498
+ echo "file"
499
+ elif can_authenticate && bws_secret "$PIPIOPS_TOKEN_NAME" >/dev/null 2>&1; then
500
+ echo "bws"
501
+ else
502
+ echo "none"
503
+ fi
504
+ }
505
+
506
+ pipiops_version() {
507
+ [ -d "$PIPIOPS_DIR/.git" ] || { echo "not installed"; return; }
508
+ git -C "$PIPIOPS_DIR" rev-parse --short HEAD 2>/dev/null || echo "unknown"
509
+ }
510
+
511
+ pipiops_ready() {
512
+ gh auth status >/dev/null 2>&1 \
513
+ || { echo "pipiops: skipped (gh is not logged in; run 'gh auth login' and run this script again)"; return 1; }
514
+ command -v npm >/dev/null 2>&1 && command -v node >/dev/null 2>&1 \
515
+ || { echo "pipiops: skipped (node and npm not found)"; return 1; }
516
+ local node_major
517
+ node_major="$(node -p 'process.versions.node.split(".")[0]')"
518
+ [ "$node_major" -ge "$PIPIOPS_NODE_MIN_MAJOR" ] 2>/dev/null \
519
+ || { echo "pipiops: skipped (needs Node >= $PIPIOPS_NODE_MIN_MAJOR, found $(node --version))"; return 1; }
520
+ }
521
+
522
+ pipiops_fetch_source() {
523
+ if [ -d "$PIPIOPS_DIR/.git" ]; then
524
+ pipiops_git -C "$PIPIOPS_DIR" pull --ff-only -q \
525
+ || echo "pipiops: could not update $PIPIOPS_DIR, keeping the current copy" >&2
526
+ return 0
527
+ fi
528
+ mkdir -p "$(dirname "$PIPIOPS_DIR")"
529
+ pipiops_git clone --depth 1 -q "https://github.com/$PIPIOPS_REPO" "$PIPIOPS_DIR" \
530
+ || { echo "pipiops: clone of $PIPIOPS_REPO failed" >&2; return 1; }
531
+ }
532
+
533
+ pipiops_install_deps() {
534
+ (cd "$PIPIOPS_DIR" && npm ci --omit=dev --no-audit --no-fund --silent) \
535
+ || { echo "pipiops: npm ci failed in $PIPIOPS_DIR" >&2; return 1; }
536
+ }
537
+
538
+ pipiops_link_commands() {
539
+ local entry
540
+ for entry in pipimcp:server pipicli:cli; do
541
+ $SUDO ln -sf "$PIPIOPS_DIR/src/${entry##*:}.mjs" "$INSTALL_DIR/${entry%%:*}" \
542
+ || { echo "pipiops: could not link ${entry%%:*} into $INSTALL_DIR" >&2; return 1; }
543
+ done
544
+ }
545
+
546
+ pipiops_report_token() {
547
+ local token_source
548
+ token_source="$(pipiops_token_source)"
549
+ if [ "$token_source" = "none" ]; then
550
+ echo "pipiops: no PR workspace token found; set $PIPIOPS_TOKEN_NAME, or add it as a bws secret"
551
+ else
552
+ echo "pipiops: PR workspace token found ($token_source)"
553
+ fi
554
+ }
555
+
556
+ install_pipiops() {
557
+ pipiops_ready || return 0
558
+ pipiops_fetch_source || return 1
559
+ pipiops_install_deps || return 1
560
+ pipiops_link_commands || return 1
561
+ echo "pipiops installed: pipimcp and pipicli ($PIPIOPS_DIR)"
562
+ pipiops_report_token
563
+ }
564
+
565
+ verify_cf_token() {
566
+ can_authenticate || { echo "cf: skipped token check (no BWS_ACCESS_TOKEN or SKIP_AUTH=1)"; return; }
567
+ local token
568
+ token="$(bws_secret CLOUDFLARE_API_TOKEN)"
569
+ if CLOUDFLARE_API_TOKEN="$token" cf zones list >/dev/null 2>&1; then
570
+ echo "cf: CLOUDFLARE_API_TOKEN from bws works"
571
+ echo "Use it in your shell with:"
572
+ echo " export CLOUDFLARE_API_TOKEN=\"\$(bws secret list | python3 -I -c 'import json,sys;print({i[\"key\"]:i[\"value\"] for i in json.load(sys.stdin)}[\"CLOUDFLARE_API_TOKEN\"])')\""
573
+ else
574
+ echo "cf: CLOUDFLARE_API_TOKEN from bws was rejected" >&2
575
+ return 1
576
+ fi
577
+ }
578
+
579
+ verify_tokens() {
580
+ local failed=0
581
+ verify_cf_token || failed=1
582
+ verify_browserbase_key || failed=1
583
+ return "$failed"
584
+ }
585
+
586
+ verify_browserbase_key() {
587
+ can_authenticate || { echo "browse: skipped key check (no BWS_ACCESS_TOKEN or SKIP_AUTH=1)"; return; }
588
+ local key
589
+ key="$(bws_secret BB_API_KEY)"
590
+ if "${CURL[@]}" -o /dev/null -H "X-BB-API-Key: $key" https://api.browserbase.com/v1/projects; then
591
+ echo "browse: BB_API_KEY from bws works"
592
+ echo "Use it in your shell with:"
593
+ echo " export BB_API_KEY=\"\$(bws secret list | python3 -I -c 'import json,sys;print({i[\"key\"]:i[\"value\"] for i in json.load(sys.stdin)}[\"BB_API_KEY\"])')\""
594
+ else
595
+ echo "browse: BB_API_KEY from bws was rejected" >&2
596
+ return 1
597
+ fi
598
+ }
599
+
600
+ setup_shell_env() {
601
+ local env_dir="${ENV_DIR:-$HOME/.config/install-tools}"
602
+ local env_file="$env_dir/env.sh"
603
+ mkdir -p "$env_dir"
604
+ cat > "$env_file" <<'ENVSH'
605
+ if [ -n "${BWS_ACCESS_TOKEN:-}" ] && [ -z "${CLOUDFLARE_API_TOKEN:-}" ] && command -v bws >/dev/null 2>&1; then
606
+ _cf_token="$(bws secret list 2>/dev/null | python3 -I -c '
607
+ import json, sys
608
+ secrets = {item["key"]: item["value"] for item in json.load(sys.stdin)}
609
+ sys.stdout.write(secrets.get("CLOUDFLARE_API_TOKEN", ""))
610
+ ' 2>/dev/null)"
611
+ [ -z "$_cf_token" ] || export CLOUDFLARE_API_TOKEN="$_cf_token"
612
+ unset _cf_token
613
+ fi
614
+ ENVSH
615
+ chmod 600 "$env_file"
616
+ local default_rc=".bashrc" rc source_line="[ -f \"$env_file\" ] && . \"$env_file\""
617
+ [ "$(uname -s)" != "Darwin" ] || default_rc=".zshrc"
618
+ [ -e "$HOME/$default_rc" ] || touch "$HOME/$default_rc"
619
+ for rc in .bashrc .zshrc; do
620
+ [ -e "$HOME/$rc" ] || continue
621
+ grep -qF "$env_file" "$HOME/$rc" || printf '\n# install-tools: CLOUDFLARE_API_TOKEN from bws\n%s\n' "$source_line" >> "$HOME/$rc"
622
+ done
623
+ echo "shell: new shells export CLOUDFLARE_API_TOKEN from bws via $env_file"
624
+ }
625
+
626
+ tool_version() {
627
+ local name="$1"
628
+ command -v "$name" >/dev/null 2>&1 || { echo "not installed"; return; }
629
+ case "$name" in
630
+ tailscale) tailscale version 2>/dev/null | head -1 ;;
631
+ gitleaks) gitleaks version 2>/dev/null | head -1 ;;
632
+ *) "$name" --version 2>/dev/null | head -1 ;;
633
+ esac
634
+ }
635
+
636
+ tailscale_state() {
637
+ command -v tailscale >/dev/null 2>&1 || { echo "not installed"; return; }
638
+ if tailscale_running tailscale || tailscale_running tailscale --socket="$TS_SOCKET"; then echo "connected"; else echo "not connected"; fi
639
+ }
640
+
641
+ README_MARKER="<!-- generated by install-tools.sh -->"
642
+
643
+ readme_bws_auth() {
644
+ if [ -n "${BWS_ACCESS_TOKEN:-}" ]; then echo "yes (BWS_ACCESS_TOKEN is set)"; else echo "no (BWS_ACCESS_TOKEN not set)"; fi
645
+ }
646
+
647
+ readme_path() {
648
+ local target="${README_PATH:-$HOME/README.md}"
649
+ if [ -e "$target" ] && ! grep -qF "$README_MARKER" "$target"; then
650
+ target="${target%.md}.install-tools.md"
651
+ fi
652
+ printf '%s' "$target"
653
+ }
654
+
655
+ readme_intro() {
656
+ cat <<README
657
+ $README_MARKER
658
+ # This machine
659
+
660
+ Set up by \`install-tools.sh\` on $(date -u +"%Y-%m-%d %H:%M UTC") (host: $(hostname), $(uname -s) $(uname -m)).
661
+
662
+ README
663
+ }
664
+
665
+ readme_tools_table() {
666
+ cat <<README
667
+ ## Installed tools
668
+
669
+ | Tool | Version | State |
670
+ |---|---|---|
671
+ | bws (Bitwarden Secrets Manager) | $(tool_version bws) | token in env: $(readme_bws_auth) |
672
+ | gh (GitHub CLI) | $(tool_version gh) | latest release; \`gh auth login\` or \`GH_TOKEN\` needed |
673
+ | cf (Cloudflare CLI, beta) | $(tool_version cf) | uses \`CLOUDFLARE_API_TOKEN\` from bws |
674
+ | wrangler | $(tool_version wrangler) | uses \`CLOUDFLARE_API_TOKEN\` from bws |
675
+ | biome | $(tool_version biome) | latest release; upgraded on every run |
676
+ | mise | $(tool_version mise) | latest release, checksum verified; upgraded on every run |
677
+ | cloudflared | $(tool_version cloudflared) | tunnels; login or token needed to run one |
678
+ | browse (Browserbase CLI) | $(tool_version browse) | uses \`BB_API_KEY\` from bws |
679
+ | gitleaks | $(tool_version gitleaks) | secret scanner; pre-commit hook in \`$GIT_HOOKS_DIR\` |
680
+ | jq, rg, fd, shellcheck, shfmt | $(tool_version jq) / $(tool_version rg) | dev utils; installed only when missing |
681
+ | tailscale | $(tool_version tailscale) | $(tailscale_state) |
682
+ | pipimcp, pipicli (pipiops) | $(pipiops_version) | PR workspace token source: $(pipiops_token_source) |
683
+
684
+ README
685
+ }
686
+
687
+ readme_tokens_table() {
688
+ cat <<README
689
+ ## Where the tokens come from
690
+
691
+ | Tool | Token | Source |
692
+ |---|---|---|
693
+ | bws | \`BWS_ACCESS_TOKEN\` | environment variable (set it yourself) |
694
+ | cf, wrangler | \`CLOUDFLARE_API_TOKEN\` | bws secret with the same name; new shells export it via \`~/.config/install-tools/env.sh\` |
695
+ | browse | \`BB_API_KEY\` | bws secret with the same name |
696
+ | tailscale | \`TS_AUTH_KEY\` | environment variable, else bws secret; used by \`tailscale up\` |
697
+ | pipimcp, pipicli | \`PIPIMCP_PR_WORKSPACE_TOKEN\` | environment variable, else \`~/.config/pipimcp/pr-workspace.token\`, else bws secret |
698
+
699
+ No secret is stored in this file or in the script.
700
+
701
+ README
702
+ }
703
+
704
+ readme_commands() {
705
+ cat <<README
706
+ ## Common commands
707
+
708
+ \`\`\`bash
709
+ # bws: list secret names only (no values)
710
+ bws secret list | python3 -I -c 'import json,sys;[print(i["key"]) for i in json.load(sys.stdin)]'
711
+
712
+ # cf: use the Cloudflare token from bws for this shell
713
+ export CLOUDFLARE_API_TOKEN="\$(bws secret list | python3 -I -c 'import json,sys;print({i["key"]:i["value"] for i in json.load(sys.stdin)}["CLOUDFLARE_API_TOKEN"])')"
714
+ cf zones list
715
+
716
+ # tailscale
717
+ tailscale status
718
+ sudo tailscale up --auth-key=file:/path/to/keyfile
719
+ \`\`\`
720
+
721
+ ## Run it again
722
+
723
+ \`\`\`bash
724
+ curl -fsSL https://raw.githubusercontent.com/nguyenhuy158/huyabhome/main/scripts/install-tools.sh | bash
725
+ \`\`\`
726
+
727
+ Set \`SKIP_AUTH=1\` to install only, without logging in.
728
+ README
729
+ }
730
+
731
+ write_readme() {
732
+ local target
733
+ target="$(readme_path)"
734
+ [ "$target" = "${README_PATH:-$HOME/README.md}" ] \
735
+ || echo "readme: existing README.md is not ours; writing $target instead"
736
+ { readme_intro; readme_tools_table; readme_tokens_table; readme_commands; } > "$target"
737
+ echo "readme: wrote $target"
738
+ }
739
+
740
+ main() {
741
+ local failed=0
742
+ install_bws
743
+ install_gh
744
+ install_mise
745
+ install_cf
746
+ install_wrangler
747
+ install_biome
748
+ install_cloudflared
749
+ install_browserbase
750
+ install_gitleaks
751
+ install_dev_utils
752
+ run_step tailscale install_tailscale
753
+ run_step pipiops install_pipiops || failed=1
754
+ run_step git-identity setup_git_identity || failed=1
755
+ run_step git-hook setup_gitleaks_hook || failed=1
756
+ run_step verify verify_tokens || failed=1
757
+ run_step tailscale login_tailscale
758
+ run_step ssh setup_tailnet_ssh
759
+ run_step shell-env setup_shell_env
760
+ run_step readme write_readme
761
+ return "$failed"
762
+ }
763
+
764
+ main "$@"
@@ -0,0 +1,163 @@
1
+ import io
2
+ import os
3
+ import subprocess
4
+ import sys
5
+ import unittest
6
+ from contextlib import redirect_stderr, redirect_stdout
7
+ from pathlib import Path
8
+ from unittest import mock
9
+
10
+ sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
11
+
12
+ import install_tools # noqa: E402
13
+ from install_tools import cli # noqa: E402
14
+
15
+ ALL_STEPS = " ".join(install_tools.STEPS)
16
+ CLEAN_ENV = {"PATH": os.environ.get("PATH", "")}
17
+
18
+
19
+ def completed(code=0):
20
+ return subprocess.CompletedProcess(args=[], returncode=code)
21
+
22
+
23
+ class BundledScript(unittest.TestCase):
24
+ def test_is_the_built_bash_script(self):
25
+ lines = install_tools.script_path().read_text().splitlines()
26
+ self.assertEqual(lines[0], "#!/usr/bin/env bash")
27
+ self.assertIn("GENERATED", lines[1])
28
+ self.assertEqual(lines[-1], 'main "$@"')
29
+
30
+ def test_has_valid_bash_syntax(self):
31
+ result = subprocess.run(
32
+ ["bash", "-n", str(install_tools.script_path())],
33
+ capture_output=True,
34
+ text=True,
35
+ )
36
+ self.assertEqual(result.returncode, 0, result.stderr)
37
+
38
+ def test_every_step_name_is_known_to_the_script(self):
39
+ script = install_tools.script_path().read_text()
40
+ for step in install_tools.STEPS:
41
+ self.assertIn(f"run_step {step} ", script)
42
+
43
+
44
+ class SafeByDefault(unittest.TestCase):
45
+ def test_skips_every_step_unless_enabled(self):
46
+ self.assertEqual(install_tools.skipped_steps(), ALL_STEPS)
47
+
48
+ def test_enabling_a_step_removes_only_that_step(self):
49
+ skipped = install_tools.skipped_steps(["pipiops", "readme"]).split()
50
+ self.assertNotIn("pipiops", skipped)
51
+ self.assertNotIn("readme", skipped)
52
+ self.assertIn("tailscale", skipped)
53
+ self.assertEqual(len(skipped), len(install_tools.STEPS) - 2)
54
+
55
+ def test_rejects_an_unknown_step(self):
56
+ with self.assertRaises(ValueError):
57
+ install_tools.skipped_steps(["nope"])
58
+
59
+ def test_git_identity_needs_a_name_and_an_email(self):
60
+ with mock.patch.dict(os.environ, CLEAN_ENV, clear=True):
61
+ with self.assertRaises(ValueError):
62
+ install_tools.build_env({}, ["git-identity"])
63
+ with self.assertRaises(ValueError):
64
+ install_tools.build_env({"GIT_USER_NAME": "a"}, ["git-identity"])
65
+ env = install_tools.build_env(
66
+ {"GIT_USER_NAME": "a", "GIT_USER_EMAIL": "a@example.com"},
67
+ ["git-identity"],
68
+ )
69
+ self.assertNotIn("git-identity", env["INSTALL_SKIP"].split())
70
+
71
+ def test_the_environment_cannot_unskip_steps_by_accident(self):
72
+ with mock.patch.dict(os.environ, {"INSTALL_SKIP": ""}):
73
+ self.assertEqual(install_tools.build_env()["INSTALL_SKIP"], ALL_STEPS)
74
+
75
+ def test_an_explicit_install_skip_wins(self):
76
+ env = install_tools.build_env({"INSTALL_SKIP": "readme"})
77
+ self.assertEqual(env["INSTALL_SKIP"], "readme")
78
+
79
+
80
+ class Run(unittest.TestCase):
81
+ def test_runs_the_script_with_bash_and_the_safe_skip_list(self):
82
+ with mock.patch("subprocess.run", return_value=completed(3)) as run:
83
+ code = install_tools.run(["x"], env={"SKIP_AUTH": "1"})
84
+ self.assertEqual(code, 3)
85
+ self.assertEqual(
86
+ run.call_args.args[0], ["bash", str(install_tools.script_path()), "x"]
87
+ )
88
+ env = run.call_args.kwargs["env"]
89
+ self.assertEqual(env["SKIP_AUTH"], "1")
90
+ self.assertEqual(env["INSTALL_SKIP"], ALL_STEPS)
91
+ self.assertIn("PATH", env)
92
+
93
+ def test_reports_a_missing_bash(self):
94
+ with mock.patch("subprocess.run", side_effect=FileNotFoundError):
95
+ with redirect_stderr(io.StringIO()):
96
+ self.assertEqual(install_tools.run(), install_tools.BASH_NOT_FOUND)
97
+
98
+
99
+ class Cli(unittest.TestCase):
100
+ def output_of(self, argv):
101
+ out = io.StringIO()
102
+ with redirect_stdout(out):
103
+ code = cli.main(argv)
104
+ return code, out.getvalue()
105
+
106
+ def test_path_prints_the_script_path(self):
107
+ code, out = self.output_of(["--path"])
108
+ self.assertEqual(code, 0)
109
+ self.assertEqual(out.strip(), str(install_tools.script_path()))
110
+
111
+ def test_print_prints_the_script(self):
112
+ code, out = self.output_of(["--print"])
113
+ self.assertEqual(code, 0)
114
+ self.assertEqual(out, install_tools.script_path().read_text())
115
+
116
+ def test_list_steps_prints_every_step(self):
117
+ code, out = self.output_of(["--list-steps"])
118
+ self.assertEqual(code, 0)
119
+ self.assertEqual(out.split(), list(install_tools.STEPS))
120
+
121
+ def test_no_option_runs_with_every_personal_step_skipped(self):
122
+ with mock.patch.object(cli, "run", return_value=5) as run:
123
+ self.assertEqual(cli.main([]), 5)
124
+ run.assert_called_once_with(env={}, enable=[])
125
+
126
+ def test_with_enables_a_step_and_skip_auth_sets_the_flag(self):
127
+ with mock.patch.object(cli, "run", return_value=0) as run:
128
+ cli.main(["--with", "pipiops", "--with", "readme", "--skip-auth"])
129
+ run.assert_called_once_with(
130
+ env={"SKIP_AUTH": "1"}, enable=["pipiops", "readme"]
131
+ )
132
+
133
+ def test_git_name_and_email_turn_on_the_git_identity_step(self):
134
+ with mock.patch.object(cli, "run", return_value=0) as run:
135
+ cli.main(["--git-name", "Ann", "--git-email", "ann@example.com"])
136
+ run.assert_called_once_with(
137
+ env={"GIT_USER_NAME": "Ann", "GIT_USER_EMAIL": "ann@example.com"},
138
+ enable=["git-identity"],
139
+ )
140
+
141
+ def test_a_git_name_without_an_email_is_an_error_and_runs_nothing(self):
142
+ with mock.patch.dict(os.environ, CLEAN_ENV, clear=True):
143
+ with mock.patch.object(cli, "run") as run:
144
+ with self.assertRaises(SystemExit) as stop, redirect_stderr(
145
+ io.StringIO()
146
+ ):
147
+ cli.main(["--git-name", "Ann"])
148
+ self.assertEqual(stop.exception.code, 2)
149
+ run.assert_not_called()
150
+
151
+ def test_an_unknown_step_is_an_error(self):
152
+ with self.assertRaises(SystemExit) as stop, redirect_stderr(io.StringIO()):
153
+ cli.main(["--with", "nope"])
154
+ self.assertEqual(stop.exception.code, 2)
155
+
156
+ def test_version_comes_from_the_package(self):
157
+ with self.assertRaises(SystemExit) as stop, redirect_stdout(io.StringIO()):
158
+ cli.main(["--version"])
159
+ self.assertEqual(stop.exception.code, 0)
160
+
161
+
162
+ if __name__ == "__main__":
163
+ unittest.main()