Skip to content

Commit

Permalink
dorothy: support --user=<user> flag on install
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Feb 5, 2024
1 parent 293e52b commit b4e968b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
81 changes: 53 additions & 28 deletions commands/dorothy
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function dorothy_() (
dorothy install
[--clean]
[--skip-install]
[--user=<user>]
Installs Dorothy interactively, with configuration for dotfile modifications.
Expand Down Expand Up @@ -901,6 +902,8 @@ function dorothy_() (
}

function ensure_user_configured {
local user="${1-}"

# sanity check
if ! [[ $action =~ ^(install|update)$ ]]; then
return 29 # ESPIPE 29 Illegal seek
Expand All @@ -915,27 +918,46 @@ function dorothy_() (
fi

# prepare
local question='' user='' where='' cli='' repo_git='' repo_url='' repo_name repo_description local_ip
local question='' where='' cli='' repo_git='' repo_url='' repo_name repo_description local_ip
repo_name='dotfiles'
repo_description="My user configuration for the Dorothy dotfile ecosystem: $dorothy_homepage"

# check where they want to store the user config
if is-interactive; then
where="$(
choose-option --required --label \
--question="Where do you want to store your Dorothy user configuration?" \
-- \
github \
'Use a GitHub repository for your user configuration.' \
gitlab \
'Use a GitLab repository for your user configuration.' \
custom \
'Manually specify a repository URL for your user configration.' \
'local' \
"Don't use a repository for your user configuration, just store it locally for now."
)"
else
where='local'
# handle github and gitlab prefixes, local value, custom urls, and default github usernames
if test -n "$user"; then
if [[ $user =~ ^(gh|github): ]]; then
where='github'
user="${user#*:}"
elif [[ $user =~ ^(gitlab|gl): ]]; then
where='github'
user="${user#*:}"
elif [[ $user =~ ^(local|none)$ ]]; then
where='local'
user=''
elif [[ $user == *:* ]]; then
where='custom'
else
user='github'
where=''
fi
fi
if test -z "$where"; then
if is-interactive; then
where="$(
choose-option --required --label \
--question="Where do you want to store your Dorothy user configuration?" \
-- \
github \
'Use a GitHub repository for your user configuration.' \
gitlab \
'Use a GitLab repository for your user configuration.' \
custom \
'Manually specify a repository URL for your user configration.' \
'local' \
"Don't use a repository for your user configuration, just store it locally for now."
)"
else
where='local'
fi
fi
if test "$where" = 'github' && __command_exists gh; then
cli='gh'
Expand All @@ -944,15 +966,17 @@ function dorothy_() (
fi
if test "$where" = 'github' -o "$where" = 'gitlab'; then
# prepare username default, and clear generic usernames
user="$(get-profile username || :)"
if test -n "$user" && is-generic -- "$user"; then
user=''
if test -z "$user"; then
user="$(get-profile username || :)"
if test -n "$user" && is-generic -- "$user"; then
user=''
fi
user="$(
ask --required --confirm \
--question="Enter your $where username." \
--default="$user"
)"
fi
user="$(
ask --required --confirm \
--question="Enter your $where username." \
--default="$user"
)"

# check what they want to call the repository
repo_name="$(
Expand Down Expand Up @@ -1360,14 +1384,15 @@ function dorothy_() (

function act_install {
# process arguments
local item option_clean='no' option_skip_install='no'
local item option_clean='no' option_skip_install='no' option_user=''
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--clean' | '--clean=yes') option_clean='yes' ;;
'--no-clean' | '--clean=no') option_clean='no' ;;
'--skip-install') option_skip_install='yes' ;;
'--user='*) option_user="${item#*=}" ;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*) help "An unrecognised argument was provided: $item" ;;
esac
Expand All @@ -1384,7 +1409,7 @@ function dorothy_() (
if test "$option_skip_install" = 'no'; then
ensure_minimal_dependencies
ensure_machine_configured
ensure_user_configured
ensure_user_configured "$option_user"
fi
ensure_shell_configured --clean="$option_clean"

Expand Down
2 changes: 1 addition & 1 deletion commands/ssh-helper
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function ssh_helper() (
--find='AddKeysToAgent .+' --replace='AddKeysToAgent yes'

# trim the identity segment
sd -f mis '# <dorothy:SSH_IDENTITY_AGENT>.+?# </dorothy:SSH_IDENTITY_AGENT>[[:space:]]*' '' "$HOME/.ssh/config"
sd --flags mis '# <dorothy:SSH_IDENTITY_AGENT>.+?# </dorothy:SSH_IDENTITY_AGENT>[[:space:]]*' '' "$HOME/.ssh/config"

# and add it back in if desired
if test -n "$SSH_IDENTITY_AGENT"; then
Expand Down

0 comments on commit b4e968b

Please sign in to comment.