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

add use command to switch version #12

Merged
merged 1 commit into from
Dec 16, 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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ curl -sSL https://git.io/g-install | sh -s
g install latest Install or activate the latest go release
g install -a 386 latest Force 32 bit architecture
g install -o darwin latest Override operating system
g download <version> Download go <version>
g set <version> Switch to go <version>
g run <version> Run a given version of go
g which <version> Output bin path for <version>
g remove <version ...> Remove the given version(s)
Expand All @@ -154,7 +156,6 @@ curl -sSL https://git.io/g-install | sh -s
-h, --help Display help information and exit
-v, --version Output current version of g and exit
-q, --quiet Disable curl output (if available)
-d, --download Download only
-c, --no-color Force disabled color output
-y, --non-interactive Prevent prompts
-o, --os Override operating system
Expand Down Expand Up @@ -225,8 +226,8 @@ At this point you would have removed `g` and `go` entirely.
- [x] Make `g` and `g-install` POSIX compliant
- [x] Add support for more shells
- [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
- [x] Use better naming for `g install <version>`, maybe `use` or `set`. See #8
- [x] Use `install` only for install and remove the `--download` option
- [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
Expand Down
93 changes: 51 additions & 42 deletions bin/g
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ set -o nounset
# Options.
#

ACTIVATE=true
QUIET=false
NO_COLOR=false
NON_INTERACTIVE=false
Expand Down Expand Up @@ -80,6 +79,8 @@ display_help() {
g install latest Install or activate the latest go release
g install -a 386 latest Force 32 bit architecture
g install -o darwin latest Override operating system
g download <version> Download go <version>
g set <version> Switch to go <version>
g run <version> Run a given version of go
g which <version> Output bin path for <version>
g remove <version ...> Remove the given version(s)
Expand All @@ -94,7 +95,6 @@ display_help() {
-h, --help Display help information and exit
-v, --version Output current version of g and exit
-q, --quiet Disable curl output (if available)
-d, --download Download only
-c, --no-color Force disabled color output
-y, --non-interactive Prevent prompts
-o, --os Override operating system
Expand Down Expand Up @@ -303,7 +303,7 @@ is_url_ok() {
# Download file with cUrl or fallback to wget.
#

download() {
download_file() {
if command -v curl > /dev/null; then
params="--location"

Expand Down Expand Up @@ -344,7 +344,7 @@ get_all_remote_versions() {
pattern='go[[:digit:]]+(\.[[:digit:]]+)+\b'
fi

download 2> /dev/null "https://go.googlesource.com/go/+refs" \
download_file 2> /dev/null "https://go.googlesource.com/go/+refs" \
| grep -E -o '\"/go/\+/refs/tags/go.+?\"' \
| grep -E -o "$pattern" \
| tr -d 'go' \
Expand All @@ -361,6 +361,18 @@ get_latest_version() {
get_all_remote_versions | tail -n1
}

#
# Resolve Version argument
#

resolve_version_argument() {
if [ "$1" = "latest" ]; then
get_latest_version
else
echo "$1"
fi
}

#
# Display installed versions in descending order.
#
Expand Down Expand Up @@ -542,7 +554,7 @@ open_interactive_ui() {
*)
# If the user hit the ENTER key, the variable includes a new line.
if [ "$(echo "$key" | wc -l)" -gt 1 ]; then
activate "$selected"
set_version "$selected"
leave_fullscreen
go version
exit
Expand All @@ -556,11 +568,15 @@ open_interactive_ui() {
# Activate <version>
#

activate() {
version=$1
set_version() {
version=$(resolve_version_argument "$1")
active=$(get_current_version)
Copy link
Owner

Choose a reason for hiding this comment

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

@feualpha only another check of latest here missing. Otherwise it looks good to merge. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yes, the consequence of exposing it

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 i make it something like this? since it's already duplicated more then 3 times

resolve_version_arg(){
    $version_arg=$1

    if [ "$version_arg" = "latest" ]; then
      echo $(get_latest_version)
    else
      echo $version_arg
    fi
}

version_args=$1
version=$(resolve_version_arg "$version_arg")

Copy link
Owner

Choose a reason for hiding this comment

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

@feualpha ok, let's do it. I usually avoid nesting calls too much since error handling in shell scripting is horrible, but since we are doing command substitution there already, it won't make it worse.

Let's compact it a bit:

resolve_version_argument() {
    if [ "$1" = "latest" ]; then
      get_latest_version
    else
      echo "$1"
    fi
}

And inside the <x>_version functions, a single line:

version=$(resolve_version_argument "$1")

Copy link
Owner

Choose a reason for hiding this comment

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

And @feualpha will need to you to rebase this branch on top of next since I just merged #10 which has been in the works for a while now.

dir="$GO_VERSIONS_DIR/$version"

if [ ! -d $dir ]; then
error_and_abort "Version $version is not available. Use 'g install $version' to install it."
fi

if [ "$version" != "$active" ]; then
if [ ! -e "$dir/g.lock" ]; then
for file in "$dir/"*; do
Expand All @@ -579,24 +595,31 @@ activate() {
# Install <version>.
#

install() {
version=$1

if [ "$version" = "latest" ]; then
version=$(get_latest_version)
fi

install_version() {
version=$(resolve_version_argument "$1")
dir="$GO_VERSIONS_DIR/$version"
Copy link
Owner

Choose a reason for hiding this comment

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

Before setting this, we have to resolve the latest version in case such keyword was used. Same for the set version.

Since we would be giving false positive since $GO_VERSIONS_DIR/latest is not an actual thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i see. i miss this one


if [ -d "$dir" ]; then
if [ "$ACTIVATE" = true ]; then
activate "$version"
fi
set_version "$version"
exit
fi

download_version $version
set_version $version
Copy link
Owner

Choose a reason for hiding this comment

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

@feualpha let's add a log_info here with installed and $(go version); this is mainly to confirm to the user the intended version was installed, in case something is off and the selected version is not actually activated.

Copy link
Owner

Choose a reason for hiding this comment

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

I mean this bit:


  if [ "$version" = "latest" ]; then
    version=$(get_latest_version)
  fi

It is being repeated quite a bit, but it is OK.


log_info "installed" "$(go version)"
}

#
# Download <version>.
#

download_version() {
version=$(resolve_version_argument "$1")
dir="$GO_VERSIONS_DIR/$version"

echo
log_info "installing" "$version"
log_info "selected" "$version"

url=$(get_tarball_url "$version")

Expand All @@ -616,17 +639,13 @@ install() {

log_info "downloading" "$url"

download "$url" | tar -zx --strip-components=1
download_file "$url" | tar -zx --strip-components=1

[ $QUIET = false ] && erase_line

rm -f "$dir/g.lock"

if [ "$ACTIVATE" = true ]; then
activate "$version"
log_info "installed" "$(go version)"
fi
echo
log_info "downloaded" "$version"
}

#
Expand Down Expand Up @@ -672,12 +691,7 @@ display_bin_path_for_version() {
error_and_abort "version required"
fi

version=$1

if [ "$version" = "latest" ]; then
version=$(get_latest_version)
fi

version=$(resolve_version_argument "$1")
bin="$GO_VERSIONS_DIR/$version/bin/go"

if [ -f "$bin" ]; then
Expand All @@ -696,13 +710,9 @@ run_with_version() {
error_and_abort "version required"
fi

version=$1
version=$(resolve_version_argument "$1")
response=

if [ "$version" = "latest" ]; then
version=$(get_latest_version)
fi

root="$GO_VERSIONS_DIR/$version"
bin="$root/bin/go"

Expand All @@ -712,16 +722,14 @@ run_with_version() {
echo

if [ "$NON_INTERACTIVE" = true ]; then
ACTIVATE=false
install "$version"
download_version "$version"
else
# Wrap it in printf to remove info's default line ending
printf "%s" "$(log_info "info" "$version is not installed, install it now? [y/N] ")"
read_user_input response

if [ "$response" = "y" ]; then
ACTIVATE=false
install "$version"
download_version "$version"
else
error_and_abort "$version is not installed"
fi
Expand Down Expand Up @@ -802,10 +810,9 @@ if [ $# -eq 0 ]; then
else
while test $# -ne 0; do
case $1 in
install|run|remove|prune|which|list|list-all|self-upgrade) __cmd=$1;;
install|set|download|run|remove|prune|which|list|list-all|self-upgrade) __cmd=$1;;
-h|--help|help) display_help; exit;;
-v|--version) display_g_version; exit;;
-d|--download) ACTIVATE=false;;
-q|--quiet) QUIET=true;;
-c|--no-color) NO_COLOR=true;;
-y|--non-interactive) NON_INTERACTIVE=true;;
Expand All @@ -821,7 +828,9 @@ else
# The unquoted expansions here are intentional.
# shellcheck disable=SC2086
case $__cmd in
install) install $__cmd_args;;
install) install_version $__cmd_args;;
download) download_version $__cmd_args;;
set) set_version $__cmd_args;;
run) run_with_version $__cmd_args;;
remove) remove_versions $__cmd_args;;
prune) prune_versions;;
Expand Down