Skip to content

Commit

Permalink
Fix prompt dialog and setup issues
Browse files Browse the repository at this point in the history
  • Loading branch information
userz authored and mmena1 committed Mar 22, 2024
1 parent 05cd2d2 commit 5d96981
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 71 deletions.
4 changes: 3 additions & 1 deletion home/.chezmoi.toml.tmpl
Original file line number Diff line number Diff line change
@@ -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 -}}
Expand All @@ -11,3 +12,4 @@
[data]
email = {{ $email | quote }}
osid = {{ $osid | quote }}
dev = {{ $dev | quote }}
10 changes: 4 additions & 6 deletions home/.chezmoiscripts/run_before_00-intro.sh.tmpl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 }}
3 changes: 1 addition & 2 deletions home/.chezmoiscripts/run_once_after_01-tmux-conf.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions home/.chezmoiscripts/run_once_after_02-asdf-install.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions home/.chezmoiscripts/run_once_after_03-docker-setup.sh.tmpl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 4 additions & 6 deletions home/.chezmoiscripts/run_once_after_04-ssh-keys.sh.tmpl
Original file line number Diff line number Diff line change
@@ -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\""
Expand All @@ -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
Expand All @@ -33,7 +31,7 @@ set_github_ssh_key() {

main() {
set_ssh_keys "$*"
set_ghithub_ssh_key "$*"
set_github_ssh_key "$*"
}

main "$*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions home/.chezmoiscripts/run_once_after_07_vscode-setup.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions home/.chezmoiscripts/run_once_after_10-gh-cli-setup.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
3 changes: 1 addition & 2 deletions home/.chezmoiscripts/run_once_after_11_eza-setup.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
15 changes: 7 additions & 8 deletions home/.chezmoitemplates/common
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
}
Original file line number Diff line number Diff line change
@@ -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
end

0 comments on commit 5d96981

Please sign in to comment.