Skip to content

Commit 368d308

Browse files
committed
fix(install): install and authenticate Bitwarden CLI before chezmoi init
chezmoi templates call bitwarden for secrets (e.g. wakatime API key), but bw was only installed by the bootstrap chezmoiscript which runs after template evaluation. Install bitwarden-cli as a prerequisite and prompt for login if unauthenticated so chezmoi can resolve secrets on first apply. Made-with: Cursor
1 parent 34bd863 commit 368d308

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

install.ps1.tmpl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
{{- $repoURL := env "DOTFILES_REPO_URL" -}}
22
{{- $releaseTag := env "DOTFILES_RELEASE_TAG" -}}
33
# install.ps1 — Bootstrap chezmoi dotfiles on Windows.
4-
# Forks: update $repoUrl below to point at your fork.
5-
#
6-
# Render source: install.ps1.tmpl
74

85
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'Install script uses Write-Host for coloured terminal output')]
96
param()
@@ -72,13 +69,24 @@ if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
7269
}
7370
}
7471
75-
# Ensure mise is available before chezmoi init (git credential helper uses `mise x -- gh`)
76-
if (-not (Get-Command mise -ErrorAction SilentlyContinue)) {
77-
Write-Host "Installing mise..." -ForegroundColor Cyan
78-
choco install mise -y --no-progress
72+
# Install remaining prerequisites that chezmoi init needs
73+
$prereqs = @()
74+
if (-not (Get-Command mise -ErrorAction SilentlyContinue)) { $prereqs += "mise" }
75+
if (-not (Get-Command bw -ErrorAction SilentlyContinue)) { $prereqs += "bitwarden-cli" }
76+
if ($prereqs.Count -gt 0) {
77+
Write-Host "Installing $($prereqs -join ', ')..." -ForegroundColor Cyan
78+
choco install @prereqs -y --no-progress
7979
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User")
8080
}
8181
82+
# Log in to Bitwarden if unauthenticated (chezmoi manages the session)
83+
$bwStatus = (bw status 2>$null | ConvertFrom-Json).status
84+
if ($bwStatus -eq "unauthenticated") {
85+
Write-Host "Bitwarden vault is not logged in. Please log in:" -ForegroundColor Yellow
86+
bw login
87+
if ($LASTEXITCODE -ne 0) { throw "Bitwarden login failed." }
88+
}
89+
8290
# Initialise and apply chezmoi dotfiles.
8391
# If the script is running from a local clone, use that as the source.
8492
# Otherwise (e.g. irm | iex), let chezmoi clone from GitHub into the default location.

install.sh.tmpl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
{{- $attestationRepo := env "DOTFILES_ATTESTATION_REPO" -}}
44
{{- $releaseTag := env "DOTFILES_RELEASE_TAG" -}}
55
#!/bin/sh
6-
# Forks: update repo_url below to point at your fork.
7-
#
8-
# Render source: install.sh.tmpl
96

107
# -e: exit on error
118
# -u: exit on unset variables
@@ -136,6 +133,27 @@ if ! chezmoi="$(command -v chezmoi)"; then
136133
fi
137134
fi
138135

136+
# Ensure Bitwarden CLI is available before chezmoi init (templates call bitwarden for secrets)
137+
if [ -z "${CODESPACES:-}" ]; then
138+
if ! command -v bw >/dev/null 2>&1; then
139+
if command -v brew >/dev/null 2>&1; then
140+
echo "Installing Bitwarden CLI via Homebrew" >&2
141+
brew install bitwarden-cli
142+
else
143+
echo "Bitwarden CLI (bw) is required but not installed and Homebrew is not available." >&2
144+
echo "Install it manually: https://bitwarden.com/help/cli/" >&2
145+
exit 1
146+
fi
147+
fi
148+
149+
# Log in to Bitwarden if unauthenticated (chezmoi manages the session)
150+
bw_status="$(bw status 2>/dev/null | sed -n 's/.*"status":"\([^"]*\)".*/\1/p')"
151+
if [ "$bw_status" = "unauthenticated" ]; then
152+
echo "Bitwarden vault is not logged in. Please log in:" >&2
153+
bw login </dev/tty
154+
fi
155+
fi
156+
139157
# POSIX way to get script's dir: https://stackoverflow.com/a/29834779/12156188
140158
script_dir="$(cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P)"
141159

0 commit comments

Comments
 (0)