Overhaul the initialization workflow

+ Prompt for values with sensible defaults to reduce typing.
+ Detect machine metadata only once during initialization.
+ Only prompt when necessary.
This commit is contained in:
Lockszmith (VAST@MacBook) 2025-04-15 12:11:55 -04:00
parent de47f0c388
commit d7e5c7d58a
4 changed files with 140 additions and 76 deletions

View File

@ -3,28 +3,36 @@
# which of the _home.* dirs will be set as the .chezmoiroot
#
{{- $githubToken := or (env "CHEZMOI_GITHUB_ACCESS_TOKEN") (env "GITHUB_ACCESS_TOKEN") (env "GITHUB_TOKEN") }}
{{- if not ($githubToken) }}
{{- $githubToken = promptStringOnce . "githubToken" "Public GITHUB token (mostly for rate limits - !insecure!)" }}
{{- end }}
{{- $githubToken := promptStringOnce . "githubToken" "Public GITHUB token (mostly for rate limits - !insecure!)" (or (
env "CZ_GITHUB_ACCESS_TOKEN"
) ( env "CHEZMOI_GITHUB_ACCESS_TOKEN"
) ( env "GITHUB_ACCESS_TOKEN"
) ( env "GITHUB_TOKEN"
)) -}}
{{- $scriptTempDir := "~/.cache/chezmoi/tmp" }}
{{- $chassisType := "desktop" }}
{{- $sysType := "posix" }}
{{- $sysVendor := "unknown" }}
{{- if eq .chezmoi.os "darwin" }}
{{- $sysType = "macos" }}
{{- $sysVendor = "apple" }}
{{- if contains "BatteryData" (output "ioreg" "-c" "AppleSmartBattery") }}
{{- $chassisType = "laptop" }}
{{- else }}
{{- $chassisType = "desktop" }}
{{- end }}
{{- else if eq .chezmoi.os "linux" }}
# $chassisType = (output "hostnamectl" "--json=short" | mustFromJson).Chassis }}
{{- $chassisType = "server" }}
{{- $chassisType = or (and
(lookPath "hostnamectl") (output "hostnamectl" "--json=short" | mustFromJson).Chassis
)
"server"
}}
{{- else if eq .chezmoi.os "windows" }}
{{- $sysType = "windows" }}
{{- $sysVendor = "pc" }}
{{- $chassisType = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "if ((Get-CimInstance -Class Win32_Battery | Measure-Object).Count -gt 0) { Write-Output 'laptop' } else { Write-Output 'desktop' }") | trim }}
{{- end -}}
{{- end }}
scriptTempDir={{- $scriptTempDir | quote }}
@ -34,3 +42,5 @@ scriptTempDir={{- $scriptTempDir | quote }}
CZ_SYS={{- $sysType | quote }}
CZ_OS={{- .chezmoi.os | quote }}
CZ_GITHUB_ACCESS_TOKEN={{- $githubToken | quote }}
CZ_VENDOR={{- $sysVendor | quote }}

View File

@ -2,18 +2,19 @@
set -e
env | grep -E 'chezmoi|^CZ'
[ -z "$SET_X" ] || env | grep -E 'chezmoi|^CZ'
cd "$CHEZMOI_SOURCE_DIR"
#CZ_MODEL="$(ioreg -l | grep "product-name" | sed -Ee 's/^.*\<"(.*)\"\>.*$/\1/')"
set | grep '^CZ_'
${SET_X:-:} | grep '^CZ_' || true
printf 'System detected as %s/%s, setting up .chezmoiroot and initializing...\n' "$CZ_SYS" "$CZ_CHASSIS"
set -x
if [ -r "base.chezmoiroot.$CZ_SYS.$CZ_CHASSIS" ]; then
ln -s "base.chezmoiroot.$CZ_SYS.$CZ_CHASSIS" .chezmoiroot
elif [ -r "base.chezmoiroot.$CZ_SYS" ]; then
ln -s "base.chezmoiroot.$CZ_SYS" .chezmoiroot
elif [ -r "base.chezmoiroot.$CZ_CHASSIS" ]; then
ln -s "base.chezmoiroot.$CZ_CHASSIS" .chezmoiroot
${SET_X:-:} -x
CZROOTLINK=chezmoi.roots/__root_links/base.chezmoiroot
if [ -r "${CZROOTLINK}.$CZ_SYS.$CZ_CHASSIS" ]; then
ln -s "${CZROOTLINK}.$CZ_SYS.$CZ_CHASSIS" .chezmoiroot
elif [ -r "${CZROOTLINK}.$CZ_SYS" ]; then
ln -s "${CZROOTLINK}.$CZ_SYS" .chezmoiroot
elif [ -r "${CZROOTLINK}.$CZ_CHASSIS" ]; then
ln -s "${CZROOTLINK}.$CZ_CHASSIS" .chezmoiroot
else
printf 'Failed to find a matching .chezmoiroot. Aborting!\n'
false

View File

@ -1,6 +1,39 @@
#! /usr/bin/env bash
set -ex
CZ_DEBUG=
while true; do
case "$1" in
--init) break;;
--debug) [[ -z "$SET_X" ]] && export SET_X=set || CZ_DEBUG=--debug;;
*) printf '%s\n' \
'Re-initialize chezmoi based environment, reloading it from source' \
'' \
'Usage:' \
" ${0##*/} [--init]" \
'' \
'Arguments:' \
' without any (or unknown) arguments, displays this usage message.' \
'' \
' --init Perform initialization' \
'' \
'Description:' \
' Deletes ~/.config/chezmoi and the .chezmoiroot and performs' \
' chezmoi init --apply' \
' This, in effect, will trigger .chezmoiscripts/run_init.sh template' \
'' \
' the init script template determins the type of OS and hardware we are' \
' running on and assigns (creates a symlink) the proper root to' \
' .chezmoiroot at which point, it re-initializes the local chezmoi' \
''
exit 2
;;
esac
shift
done
set -e
${SET_X:-:} -x
rm -fR ~/.config/chezmoi ~/.local/share/chezmoi/.chezmoiroot
chezmoi init --apply
chezmoi init ${CZ_DEBUG} --apply
unset SET_X

View File

@ -1,97 +1,117 @@
{{- $githubToken := or (env "CZ_GITHUB_ACCESS_TOKEN") (env "CHEZMOI_GITHUB_ACCESS_TOKEN") (env "GITHUB_ACCESS_TOKEN") (env "GITHUB_TOKEN") }}
{{- if not ($githubToken) }}
{{- $githubToken = promptStringOnce . "githubToken" "Public GITHUB token (mostly for rate limits - !insecure!)" }}
{{- end }}
{{- $sysname := promptStringOnce . "sysname" "System name" }}
{{- $sysgroup := promptStringOnce . "sysgroup" "System group" }}
{{- $sysarea := promptStringOnce . "sysarea" "System area" }}
{{- $gitEmail := promptStringOnce . "gitEmail" "email address (for git commits)" }}
{{- $gitName := promptStringOnce . "gitName" "Full name (for git commits)" }}
{{- $chassisType := "desktop" }}
{{- $sysType := "posix" }}
{{- $arch_alt := "x86_64" }}
{{- $arch_alt_dash := "x86-64" }}
{{- $sysVendor := "unknown" }}
{{- $clib_flavor := "" }}
{{- if eq .chezmoi.os "darwin" }}
{{- $arch_alt = "aarch64" }}
{{- $arch_alt_dash = $arch_alt }}
{{- $sysType = "macos" }}
{{- $sysVendor = "apple" }}
{{- if contains "BatteryData" (output "ioreg" "-c" "AppleSmartBattery") }}
{{- $chassisType = "laptop" }}
{{- else }}
{{- $chassisType = "desktop" }}
{{- end }}
{{- else if eq .chezmoi.os "linux" }}
{{- $clib_flavor = "-musl" }}
# $chassisType = (output "hostnamectl" "--json=short" | mustFromJson).Chassis }}
{{- $chassisType = "server" }}
{{- else if eq .chezmoi.os "windows" }}
{{- $clib_flavor = "-msvc" }}
{{- $sysType = "windows" }}
{{- $sysVendor = "pc" }}
{{- $chassisType = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "if ((Get-CimInstance -Class Win32_Battery | Measure-Object).Count -gt 0) { Write-Output 'laptop' } else { Write-Output 'desktop' }") | trim }}
{{- end }}
# source by .chezmoi.toml.tmpl, it is recommended to edit with
# cz edit-config-template && cz init
{{- $githubToken := or (
env "CZ_GITHUB_ACCESS_TOKEN"
) ( env "CHEZMOI_GITHUB_ACCESS_TOKEN"
) ( env "GITHUB_ACCESS_TOKEN"
) ( env "GITHUB_TOKEN"
) ( promptStringOnce . "githubToken" "Public GITHUB token (mostly for rate limits - !insecure!)"
) -}}
{{- $sysname := promptStringOnce . "sysname" "System name" (or (
env "CZ_SYSNAME"
) ( .chezmoi.hostname
)) -}}
{{- $sysgroup := promptStringOnce . "sysgroup" "System group" (or (
env "CZ_SYSGROUP"
) ( .chezmoi.group
)) -}}
{{- $sysuser := promptStringOnce . "sysuser" "System user" (or (
env "CZ_SYSUSER"
) ( .chezmoi.username
)) -}}
{{- $gitEmail := promptStringOnce . "gitEmail" "email address (for git commits)" (or (
env "CZ_GIT_EMAIL"
) ( output "git" "config" "user.email"
) ( list .chezmoi.username "@" .chezmoi.fqdnHostname | join ""
)) -}}
{{- $gitName := promptStringOnce . "gitName" "Full name (for git commits)" (or (
env "CZ_GIT_NAME"
) ( output "git" "config" "user.name"
) ( list .chezmoi.username " (@" $sysname ")" | join ""
)) -}}
{{- $scriptTempDir := or ( get . "sz.os.scriptTempDir" ) ( env "CZ_SCRIPT_TEMPDIR" ) -}}
{{- $chassisType := or ( get . "sz.os.chassisType" ) ( env "CZ_CHASSIS" ) -}}
{{- $sysType := or ( get . "sz.os.sysType" ) ( env "CZ_SYS" ) -}}
{{- $sysVendor := or ( get . "sz.os.sysVendor" ) ( env "CZ_VENDOR" ) -}}
{{- $arch_alt := "x86_64" -}}
{{- $arch_alt_dash := "x86-64" -}}
{{- $clib_flavor := "" -}}
{{- if eq .chezmoi.os "darwin" -}}
{{- $arch_alt = "aarch64" -}}
{{- $arch_alt_dash = $arch_alt -}}
{{- else if eq .chezmoi.os "linux" -}}
{{- $clib_flavor = "-musl" -}}
{{- else if eq .chezmoi.os "windows" -}}
{{- $clib_flavor = "-msvc" -}}
{{- end -}}
{{- $scriptTempDir := "~/.cache/chezmoi/tmp" }}
scriptTempDir={{- $scriptTempDir | quote }}
scriptTempDir={{ $scriptTempDir | quote }}
[data.sz.os]
{{- if eq .chezmoi.os "linux" }}
short={{- .chezmoi.os | quote }}
short={{- .chezmoi.os | quote }}
{{- else if eq .chezmoi.os "darwin" }}
short="mac"
short="mac"
{{- else }}
short={{- substr 0 3 (.chezmoi.os) | quote }}
short={{- substr 0 3 (.chezmoi.os) | quote }}
{{- end }}
rust_rel_target={{- printf "%s-%s%s" $sysVendor .chezmoi.os $clib_flavor | quote }}
rust_rel_target={{- printf "%s-%s%s" $sysVendor .chezmoi.os $clib_flavor | quote }}
{{- if ($arch_alt) }}
arch_alt={{- $arch_alt | quote }}
arch_alt={{- $arch_alt | quote }}
{{- end }}
{{- if ($arch_alt_dash) }}
arch_alt_dash={{- $arch_alt_dash | quote }}
arch_alt_dash={{- $arch_alt_dash | quote }}
{{- end }}
{{- if ($chassisType) }}
chassisType={{- $chassisType | quote }}
chassisType={{- $chassisType | quote }}
{{- end }}
{{- if ($sysType) }}
sysType={{- $sysType | quote }}
sysType={{- $sysType | quote }}
{{- end }}
[data]
{{- if ($githubToken) }}
githubToken={{- $githubToken | quote }}
githubToken={{- $githubToken | quote }}
{{- end }}
{{- if ($sysname) }}
sysname = {{ $sysname | quote }}
sysname = {{ $sysname | quote }}
{{- end }}
{{- if ($sysgroup) }}
sysgroup = {{ $sysgroup | quote }}
sysgroup = {{ $sysgroup | quote }}
{{- end }}
{{- if ($sysuser) }}
sysuser = {{ $sysuser | quote }}
{{- end }}
{{- if ($sysarea) }}
sysarea = {{ $sysarea | quote }}
{{ end }}
{{- if ($gitEmail) }}
gitEmail = {{ $gitEmail | quote }}
gitEmail = {{ $gitEmail | quote }}
{{- end }}
{{- if ($gitName) }}
gitName = {{ $gitName | quote }}
gitName = {{ $gitName | quote }}
{{- end }}
{{- if not ($githubToken) }}
githubToken={{- $githubToken | quote }}
githubToken={{- $githubToken | quote }}
{{- end }}
[scriptEnv]
GITHUB_ACCESS_TOKEN={{- $githubToken | quote }}
GITHUB_TOKEN={{- $githubToken | quote }}
CZ_SCRIPT_TEMPDIR={{- $scriptTempDir | quote }}
CZ_CHASSIS={{- $chassisType | quote }}
CZ_SYS={{- $sysType | quote }}
CZ_OS={{- .chezmoi.os | quote }}
CZ_GITHUB_ACCESS_TOKEN={{- $githubToken | quote }}
CZ_VENDOR={{- $sysVendor | quote }}
GITHUB_ACCESS_TOKEN={{- $githubToken | quote }}
GITHUB_TOKEN={{- $githubToken | quote }}
[diff]
# command = "nvim"
# args = ["-d", "{{ "{{ .Destination }}" }}", "{{ "{{ .Target }}" }}"]
command = "delta"
args = ["--paging=never", "{{ "{{ .Destination }}" }}", "{{ "{{ .Target }}" }}"]
pager = "moar"
command = "delta"
args = ["--paging=never", "{{ "{{ .Destination }}" }}", "{{ "{{ .Target }}" }}"]
pager = "moar"