Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(install): automatically add alias on conflict command #11

Merged
merged 1 commit into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ At this point you would have removed `g` and `go` entirely.
- [x] Warn users they already have a golang installation when using `g-install`
- [ ] Use better naming for `g install <version>`, maybe `use` or `set`. See #8
- [ ] Use `install` only for install and remove the `--download` option
- [ ] Handle the case when `g` already exists, mainly `zsh` with `oh-my-zsh`
- [ ] Make it so `g-install` offers the user to setup an alternative alias for `g`
- [x] Handle the case when `g` already exists, mainly `zsh` with `oh-my-zsh`
- [x] Make it so `g-install` offers the user to setup an alternative alias for `g`
- [x] Make the `self-upgrade` command throw if `g` was not installed in the common way
- [ ] Add a `complete` command that generates completions for the supported shells
- [ ] And have `g-install` setup the shells to call this command for completions
Expand Down
134 changes: 134 additions & 0 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ SUPPORTED_SHELLS="ash dash bash csh tsch zsh fish"
INSTALLED_SHELLS=""
SELECTED_SHELLS=""

G_ALIAS="g"
G_ALIAS_CHANGED=false
G_ALIAS_USED_IN_SHELL=""

#
# POSIX utils.
#
Expand Down Expand Up @@ -262,6 +266,123 @@ install_g() {
chmod +x "$GOPATH/bin/g"
}

#
# Check whether 'g' alias is already used, setup alternatives alias if it is
#

configure_g_alias() {
if ! g_alias_defined "$G_ALIAS"; then
return 0
fi

G_ALIAS_CHANGED=true

if [ "$NON_INTERACTIVE" = true ]; then
G_ALIAS="ggovm"
return 0
fi

while g_alias_defined "$G_ALIAS"; do
if ! prompt_set_alias "$G_ALIAS" "$G_ALIAS_USED_IN_SHELL"; then
echo
echo
log_info "info" "skipping setup alias for g"
G_ALIAS_CHANGED=false
break
fi

G_ALIAS=$(set_alias_name)
done
}

#
# Iteratively check every selected shells for defined alias
#

g_alias_defined() {
alias_already_used=false

for shell in $SELECTED_SHELLS; do
if exists $shell "$1"; then
alias_already_used=true
G_ALIAS_USED_IN_SHELL="$shell"
fi
done

if [ "$alias_already_used" = true ]; then
return 0
fi

return 1
}

#
# Check whether command already exists
#

exists() {
case "$1" in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alvinmatias69 here you gotta add a default case. It will say "g alias does exist" in the shells not supported by the alias setup feature.

For non-supported shells, we can simply assume g doesn't exists, and skip the whole thing since we cannot do anything anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we skip the alias configuration for all shells or only those shells are not supported?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really following.

So the deal here is that, as it stands now, if you try to install g in a shell diff from the ones here (zsh, bash, fish), for example, in ash, you will be promoted to setup an alias in ash, even tho we don't support it (yet, hopefully). And you will also be stuck in a loop of prompts.

This because exit code 0 means "does exist" while exit code 1 means "does not exist".

Since there's no defuault case, when the shell is not supported the default exit code is 0.

So an *) return 1;; at the end would help fix these cases.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should invert the exit codes and add explicit returns inside if statements to make it clearer for future readers. 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or am I missing something? I'm reviewing from my phone so I don't have the big picture. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I get it. I initially thought that the alias won't be set for all shells if there's an unsupported shell. So, basically return 1 as default so the installer won't recognize it as already exists alias, right? 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That. I think we are on the same page now. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allrite, I'm already on it

zsh)
zsh -ci alias | grep "$2=" > /dev/null;
;;
bash)
bash -ci alias | grep "$2=" > /dev/null;
;;
fish)
fish -c 'alias' | grep " $2 " > /dev/null;
;;
*)
return 1;;
esac
}


#
# Prompt user whether they want to set a new alias
#

prompt_set_alias() {
stefanmaric marked this conversation as resolved.
Show resolved Hide resolved
echo
printf "Alias '$1' is already defined in '$2', set another alias? [y/N] "
yes_alias=
read_user_input yes_alias

case $yes_alias in
[Yy]* )
return 0;;
* )
return 1;;
esac
}


#
# Prompt user for new alias name
#

set_alias_name() {
input_device=
# Determine where to read user input from.
if [ -f "${0:-}" ]; then
# When running the script as a binary file, use stdin.
input_device="/dev/stdin"
else
# When running in a pipe, read from the tty.
input_device="/dev/tty"
fi

alias_name=
echo >&2
printf "Enter alias name! [ggovm] " >&2
read alias_name < $input_device
echo >&2
if [ -z "$alias_name" ]; then
echo "ggovm"
else
echo $alias_name
fi
}

#
# Set environment variables in all selected shells.
#
Expand All @@ -278,10 +399,22 @@ configure_selected_shells() {
else
log_info "info" "configuring $shell in $dotfile_path"
echo "$config_line" >> "$dotfile_path"
set_g_alias $dotfile_path
fi
done
}

#
# set alias in dotfile
#

set_g_alias() {
if [ "$G_ALIAS_CHANGED" = true ]; then
alias_line="alias $G_ALIAS=\"\$GOPATH/bin/g\"; # $COMMENT_MESSAGE"
echo "$alias_line" >> "$1"
fi
}

#
# Start the installation process.
#
Expand Down Expand Up @@ -359,6 +492,7 @@ initiate() {
echo

install_g
configure_g_alias
configure_selected_shells

echo
Expand Down