From 5d96981a00924c78701ad26551546f94950ab3e5 Mon Sep 17 00:00:00 2001 From: Martin Mena Date: Fri, 22 Mar 2024 13:20:53 -0500 Subject: [PATCH] Fix prompt dialog and setup issues --- home/.chezmoi.toml.tmpl | 4 ++- .../run_before_00-intro.sh.tmpl | 10 +++---- ...n_once_after_00-passwordless-sudo.sh.tmpl} | 30 ++++++++----------- .../run_once_after_01-tmux-conf.sh.tmpl | 3 +- .../run_once_after_02-asdf-install.sh.tmpl | 6 ++-- .../run_once_after_03-docker-setup.sh.tmpl | 5 ++-- .../run_once_after_04-ssh-keys.sh.tmpl | 10 +++---- .../run_once_after_06_1password-setup.sh.tmpl | 3 +- .../run_once_after_07_vscode-setup.sh.tmpl | 3 +- ...once_after_09-font-fira-code-setup.sh.tmpl | 3 +- .../run_once_after_10-gh-cli-setup.sh.tmpl | 3 +- .../run_once_after_11_eza-setup.sh.tmpl | 3 +- ...un_once_before_01-install-homebrew.sh.tmpl | 3 +- home/.chezmoitemplates/common | 15 +++++----- .../functions/__git.default_branch.fish | 30 +++++++++++-------- 15 files changed, 60 insertions(+), 71 deletions(-) rename home/.chezmoiscripts/{run_once_after_00-passwordless_sudo.sh.tmpl => run_once_after_00-passwordless-sudo.sh.tmpl} (65%) diff --git a/home/.chezmoi.toml.tmpl b/home/.chezmoi.toml.tmpl index fda4e64..08df11a 100644 --- a/home/.chezmoi.toml.tmpl +++ b/home/.chezmoi.toml.tmpl @@ -1,5 +1,6 @@ +{{- $dev := or (env "dev") "true" -}} {{- $email := "example@email.com" -}} -{{- if eq (env "dev") "true" -}} +{{- if eq $dev "true" -}} {{- $email = promptStringOnce . "email" "Email address" -}} {{- end -}} {{- $osid := .chezmoi.os -}} @@ -11,3 +12,4 @@ [data] email = {{ $email | quote }} osid = {{ $osid | quote }} + dev = {{ $dev | quote }} diff --git a/home/.chezmoiscripts/run_before_00-intro.sh.tmpl b/home/.chezmoiscripts/run_before_00-intro.sh.tmpl index d27c792..0911752 100644 --- a/home/.chezmoiscripts/run_before_00-intro.sh.tmpl +++ b/home/.chezmoiscripts/run_before_00-intro.sh.tmpl @@ -1,20 +1,18 @@ -{{ if eq (env "dev") "true" -}} +{{ if eq .dev "true" -}} {{ template "common" . -}} {{ template "dotfile" }} bot "This script will guide you through the dofiles setup as well as installing required system packages." echo "It will not install anything without your direct agreement!" echo -answer=$(prompt "Do you want to proceed with the installation?") -echo -if [[ ! $answer =~ ^[Yy]$ ]];then +if ! prompt "Do you want to proceed with the installation?"; then + echo "Answer captured: $answer" exit 1 fi bot "Setting dotfiles up on the home folder." echo -answer=$(prompt "Proceed?") -if [[ ! $answer =~ ^[Yy]$ ]];then +if ! prompt "Proceed?"; then exit 1 fi diff --git a/home/.chezmoiscripts/run_once_after_00-passwordless_sudo.sh.tmpl b/home/.chezmoiscripts/run_once_after_00-passwordless-sudo.sh.tmpl similarity index 65% rename from home/.chezmoiscripts/run_once_after_00-passwordless_sudo.sh.tmpl rename to home/.chezmoiscripts/run_once_after_00-passwordless-sudo.sh.tmpl index 176c684..d251aae 100644 --- a/home/.chezmoiscripts/run_once_after_00-passwordless_sudo.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_00-passwordless-sudo.sh.tmpl @@ -1,5 +1,5 @@ -{{ if eq (env "dev") "true" }} -{{ template "common" . }} +{{ if eq .dev "true" -}} +{{ template "common" . -}} validate_logname() { if [[ ! "$LOGNAME" =~ ^[a-zA-Z0-9][a-zA-Z0-9_-]*$ ]]; then @@ -8,27 +8,22 @@ validate_logname() { fi } -prompt_for_sudo_if_needed() { - if ! sudo -nv > /dev/null 2>&1; then - echo "Please provide your sudo password:" - sudo -v - fi - # Keep-alive: update existing sudo time stamp until the script has finished - while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null & -} - setup_passwordless_sudo() { + bot "Verifying passwordless sudo config" # Determine sudoers.d directory based on OS - local sudoers_d_dir="{{ if eq .osid "darwin" }}/private/etc/sudoers.d{{ else }}/etc/sudoers.d{{ end }}" - local includedir_line="{{ if eq .osid "darwin" }}#includedir /private/etc/sudoers.d{{ else }}@includedir /etc/sudoers.d{{ end }}" + local sudoers_d_dir="/etc/sudoers.d" + local includedir_line="@includedir /etc/sudoers.d" + {{ if eq .osid "darwin" }} + sudoers_d_dir="/private/etc/sudoers.d" + includedir_line="#includedir /private/etc/sudoers.d" + {{ end }} # Check if NOPASSWD entry already exists for the user if ! sudo grep -q "NOPASSWD: ALL" "$sudoers_d_dir/$LOGNAME" > /dev/null 2>&1; then echo "No sudoer file found for passwordless operation." bot "Enabling passwordless sudo can reduce security. Are you sure you want to proceed?" - answer=$(prompt "Make sudo passwordless? [y/N]") - if [[ $answer =~ ^[Yy]$ ]]; then + if prompt "Make sudo passwordless? [y/N]"; then # Ensure sudoers.d is included and directory exists if ! sudo grep -q "$includedir_line" /etc/sudoers; then echo "$includedir_line" | sudo tee -a /etc/sudoers > /dev/null @@ -49,9 +44,10 @@ setup_passwordless_sudo() { else echo "Passwordless sudo already configured for $LOGNAME." fi -fi +} validate_logname -prompt_for_sudo_if_needed + setup_passwordless_sudo + {{ end }} diff --git a/home/.chezmoiscripts/run_once_after_01-tmux-conf.sh.tmpl b/home/.chezmoiscripts/run_once_after_01-tmux-conf.sh.tmpl index eb5d1a5..cc29edd 100644 --- a/home/.chezmoiscripts/run_once_after_01-tmux-conf.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_01-tmux-conf.sh.tmpl @@ -7,8 +7,7 @@ if [ ! -d "$HOME/.tmux" ]; then warn "git is not installed. Please install git first." exit 1 fi - answer=$(prompt "Would you like to install the awesome tmux configuration from https://github.com/gpakosz/.tmux.git?") - if [[ $answer =~ ^[Yy]$ ]]; then + if ! prompt "Would you like to install the awesome tmux configuration from https://github.com/gpakosz/.tmux.git?"; then action "git clone https://github.com/gpakosz/.tmux.git" git clone https://github.com/gpakosz/.tmux.git $HOME/.tmux ok diff --git a/home/.chezmoiscripts/run_once_after_02-asdf-install.sh.tmpl b/home/.chezmoiscripts/run_once_after_02-asdf-install.sh.tmpl index b698f1c..ee17a45 100644 --- a/home/.chezmoiscripts/run_once_after_02-asdf-install.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_02-asdf-install.sh.tmpl @@ -4,8 +4,7 @@ install_asdf() { bot "Checking asdf..." echo if ! _exists asdf ; then - answer=$(prompt "Would you like to install asdf?") - if [[ $answer =~ ^[Yy]$ ]]; then + if ! prompt "Would you like to install asdf?"; then if [[ ! -d ~/.asdf ]]; then action "Cloning git repo..." git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0 || { echo "Failed to clone asdf repository"; exit 1; } @@ -33,8 +32,7 @@ configure_asdf() { install_asdf_tools() { echo - answer=$(prompt "Would you like to install the languages defined in .tool-versions?") - if [[ $answer =~ ^[Yy]$ ]];then + if prompt "Would you like to install the languages defined in .tool-versions?";then if cat ~/.tool-versions > /dev/null 2>&1 ;then while read -r line; do # get the first word of the line diff --git a/home/.chezmoiscripts/run_once_after_03-docker-setup.sh.tmpl b/home/.chezmoiscripts/run_once_after_03-docker-setup.sh.tmpl index 48df18e..105b32d 100644 --- a/home/.chezmoiscripts/run_once_after_03-docker-setup.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_03-docker-setup.sh.tmpl @@ -1,11 +1,10 @@ -{{ if eq (env "dev") "true" -}} +{{ if eq .dev "true" -}} {{ template "common" . -}} bot "Docker" echo if _exists docker && ! systemctl is-enabled --quiet docker ;then - answer=$(prompt "Enable docker and add your user to the docker group?") - if [[ $answer =~ ^[Yy]$ ]];then + if prompt "Enable docker and add your user to the docker group?";then action "Enabling docker" sudo systemctl enable --now docker.service ok diff --git a/home/.chezmoiscripts/run_once_after_04-ssh-keys.sh.tmpl b/home/.chezmoiscripts/run_once_after_04-ssh-keys.sh.tmpl index 29ce0e6..96cd9cf 100644 --- a/home/.chezmoiscripts/run_once_after_04-ssh-keys.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_04-ssh-keys.sh.tmpl @@ -1,12 +1,11 @@ -{{ if eq (env "dev") "true" -}} +{{ if eq .dev "true" -}} {{ template "common" . -}} set_ssh_keys() { bot "Checking ssh keys..." echo if [ ! -f ~/.ssh/id_ed25519 ] ; then - answer=$(prompt "Would you like to set ssh keys now?") - if [[ $answer =~ ^[Yy]$ ]] ; then + if prompt "Would you like to set ssh keys now?" ; then echo "Setting ssh keys using the ed25519 algorithm." read -rp "Enter the passphrase: (empty for no passphrase)" passphrase action "ssh-keygen -t ed25519 -C {{- .email }} -q -N \"$passphrase\"" @@ -21,8 +20,7 @@ set_ssh_keys() { set_github_ssh_key() { if [ -f ~/.ssh/id_ed25519 ] && _exists gh ; then - answer=$(prompt "Would you like to set your public ssh key to your gh account?") - if [[ $answer =~ ^[Yy]$ ]] ; then + if prompt "Would you like to set your public ssh key to your gh account?" ; then action "Adding SSH key to GitHub account" gh ssh-key add ~/.ssh/id_ed25519.pub -t "$(hostname)" else @@ -33,7 +31,7 @@ set_github_ssh_key() { main() { set_ssh_keys "$*" - set_ghithub_ssh_key "$*" + set_github_ssh_key "$*" } main "$*" diff --git a/home/.chezmoiscripts/run_once_after_06_1password-setup.sh.tmpl b/home/.chezmoiscripts/run_once_after_06_1password-setup.sh.tmpl index eb5af20..8ca235b 100644 --- a/home/.chezmoiscripts/run_once_after_06_1password-setup.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_06_1password-setup.sh.tmpl @@ -5,8 +5,7 @@ bot "Checking 1password..." echo if ! _exists 1password ; then - answer=$(prompt "Would you like to install 1password?") - if [[ $answer =~ ^[Yy]$ ]] ;then + if prompt "Would you like to install 1password?" ;then {{ if eq .osid "linux-debian" }} action "Adding key for apt repo..." curl -sS https://downloads.1password.com/linux/keys/1password.asc | sudo gpg --dearmor --output /usr/share/keyrings/1password-archive-keyring.gpg diff --git a/home/.chezmoiscripts/run_once_after_07_vscode-setup.sh.tmpl b/home/.chezmoiscripts/run_once_after_07_vscode-setup.sh.tmpl index b029ed2..e387f6d 100644 --- a/home/.chezmoiscripts/run_once_after_07_vscode-setup.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_07_vscode-setup.sh.tmpl @@ -5,8 +5,7 @@ bot "Checking VSCode..." echo if ! _exists code ; then - answer=$(prompt "Would you like to install vscode?") - if [[ $answer =~ ^[Yy]$ ]] ;then + if prompt "Would you like to install vscode?" ;then action "checking if snapd is enabled" if systemctl is-active --quiet snapd ;then action "snap install code --classic" diff --git a/home/.chezmoiscripts/run_once_after_09-font-fira-code-setup.sh.tmpl b/home/.chezmoiscripts/run_once_after_09-font-fira-code-setup.sh.tmpl index fee3685..7ed7365 100644 --- a/home/.chezmoiscripts/run_once_after_09-font-fira-code-setup.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_09-font-fira-code-setup.sh.tmpl @@ -5,8 +5,7 @@ bot "Fira Code setup." if ! fc-list | grep -qi "Fira Code" > /dev/null 2>&1; then - answer=$(prompt "Would you like to install Fira Code NerdFont to get cool icons on the terminal?") - if [[ $answer =~ ^[Yy]$ ]]; then + if prompt "Would you like to install Fira Code NerdFont to get cool icons on the terminal?"; then action "Downloading from https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/FiraCode.zip" wget -q https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/FiraCode.zip && \ if [ -f "FiraCode.zip" ]; then diff --git a/home/.chezmoiscripts/run_once_after_10-gh-cli-setup.sh.tmpl b/home/.chezmoiscripts/run_once_after_10-gh-cli-setup.sh.tmpl index 3ba83d0..8624b15 100644 --- a/home/.chezmoiscripts/run_once_after_10-gh-cli-setup.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_10-gh-cli-setup.sh.tmpl @@ -5,8 +5,7 @@ bot "Checking GitHub CLI..." echo if ! _exists gh ; then - answer=$(prompt "Would you like to install GitHub cli?") - if [[ $answer =~ ^[Yy]$ ]] ;then + if prompt "Would you like to install GitHub cli?" ;then {{ if eq .osid "linux-debian" -}} type -p curl >/dev/null || (sudo apt -qq update && sudo apt -qq install curl -y) curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ diff --git a/home/.chezmoiscripts/run_once_after_11_eza-setup.sh.tmpl b/home/.chezmoiscripts/run_once_after_11_eza-setup.sh.tmpl index f55fd64..506380c 100644 --- a/home/.chezmoiscripts/run_once_after_11_eza-setup.sh.tmpl +++ b/home/.chezmoiscripts/run_once_after_11_eza-setup.sh.tmpl @@ -5,8 +5,7 @@ bot "Checking eza..." echo if ! _exists eza ; then - answer=$(prompt "Would you like to install eza?") - if [[ $answer =~ ^[Yy]$ ]] ;then + if prompt "Would you like to install eza?" ;then {{ if eq .osid "linux-debian" }} action "Adding key for apt repo..." wget -qO- https://raw.githubusercontent.com/eza-community/eza/main/deb.asc | sudo gpg --dearmor -o /etc/apt/keyrings/gierens.gpg diff --git a/home/.chezmoiscripts/run_once_before_01-install-homebrew.sh.tmpl b/home/.chezmoiscripts/run_once_before_01-install-homebrew.sh.tmpl index 4714a6d..36396d1 100644 --- a/home/.chezmoiscripts/run_once_before_01-install-homebrew.sh.tmpl +++ b/home/.chezmoiscripts/run_once_before_01-install-homebrew.sh.tmpl @@ -5,8 +5,7 @@ bot "Checking brew..." echo if ! _exists brew ; then - answer=$(prompt "Would you like to install Homebrew?") - if [[ $answer =~ ^[Yy]$ ]] ;then + if prompt "Would you like to install Homebrew?" ;then action "Installing homebrew..." export NONINTERACTIVE=1 bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" diff --git a/home/.chezmoitemplates/common b/home/.chezmoitemplates/common index ccb6011..4c65391 100644 --- a/home/.chezmoitemplates/common +++ b/home/.chezmoitemplates/common @@ -43,16 +43,15 @@ function error() { function prompt { local prompt_message=${1:-"Default prompt message"} - {{ if eq (env "dev") "true" -}} - read -rp "$prompt_message [y/N]" -n 1 -r -s answer + {{ if eq .dev "true" -}} + read -p "$prompt_message [y/N]" -n 1 -r -s answer echo # Ensure newline after input - {{ else -}} - answer="y" - {{ end -}} - if [[ $answer =~ ^[Yy]$ ]]; then - echo "y" + return 0 # Indicative of 'yes' else - echo "n" + return 1 # Indicative of 'no' or any input other than 'y' fi + {{ else -}} + return 0 + {{ end -}} } diff --git a/home/private_dot_config/private_fish/functions/__git.default_branch.fish b/home/private_dot_config/private_fish/functions/__git.default_branch.fish index 06a4427..4924847 100644 --- a/home/private_dot_config/private_fish/functions/__git.default_branch.fish +++ b/home/private_dot_config/private_fish/functions/__git.default_branch.fish @@ -1,14 +1,20 @@ -# Reference: -# https://github.com/jhillyerd/plugin-git/blob/master/functions/__git.default_branch.fish +function __git.default_branch + # Get the remote name, defaulting to 'origin' + set -l remote (git remote show | head -n 1) + if test -z "$remote" + echo "No remote found" + return 1 + end + + # Fetch remote information to ensure we have the latest default branch info + git remote update $remote --prune + + # Get the default branch from the remote + set -l default_branch (git remote show $remote | grep 'HEAD branch' | sed 's/.*: //') + if test -z "$default_branch" + echo "Could not determine the default branch" + return 1 + end -function __git.default_branch -d "Use init.defaultBranch if it's set and exists, otherwise use main if it exists. Falls back to master" - command git rev-parse --git-dir &>/dev/null; or return - if set -l default_branch (command git config --get init.defaultBranch) - and command git show-ref -q --verify refs/heads/{$default_branch} echo $default_branch - else if command git show-ref -q --verify refs/heads/main - echo main - else - echo master - end -end \ No newline at end of file +end