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

Conversation

oniraf
Copy link
Contributor

@oniraf oniraf commented Dec 8, 2019

  • add set command to switch between go version,
  • add download command to replace g install <version> --download

suggestion from #8

@stefanmaric stefanmaric changed the base branch from master to next December 9, 2019 12:10
@stefanmaric stefanmaric self-requested a review December 9, 2019 13:28
Copy link
Owner

@stefanmaric stefanmaric left a comment

Choose a reason for hiding this comment

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

Hi @feualpha, thanks for your contribution!

Before we continue, let's make the PR against the next branch. I already changed the PR but you will need to rebase and fix a conflict in the README

After thinking about it for a while, I've changed my mind about the approach to the "new" API:

  1. Let's better use set instead of use. The latter sounds more like to what the current g run command does.
  2. Let's create another command: download, which will only download the given version but not set it.
  3. The install command, should download and set the given version without doubting (no --download flag).

To achieve this, we will need to create a new function that only downloads, let's call it download_version and rename the current download function to download_file for clarity, since that's what the function does.

Some extra bits:

  • As a side effect, the ACTIVATE variable should go away entirely.
  • The activate function which is called by the set command, should check if the version actually exists in the .versions dir and throw an "Version X is not available. Use 'g install X' to install it."
  • To normalize the naming of the functions, let's name/rename them set_version, install_version and download_version

The rationale here is that install should keep doing what it currently does and should be the main command users will use 99% of the time, but we should provide "porcelain" commands as escape hatches and discovery tools. Also, the deletion of the --download flag in favor of function/command composition is more elegant.

Let me know if something isn't clear

@oniraf
Copy link
Contributor Author

oniraf commented Dec 14, 2019

hi @stefanmaric i have push some changes, please check it out.
also please check the command line output. i'm not sure how it should be done.

@stefanmaric
Copy link
Owner

check the command line output

You're right, it is a bit messed up now. Will leave you some suggestions.

Copy link
Owner

@stefanmaric stefanmaric left a comment

Choose a reason for hiding this comment

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

@feualpha thanks for your patience.

We almost done. I left a few outstanding tweaks, but otherwise this is looking pretty good.

bin/g Outdated
@@ -569,6 +573,8 @@ activate() {
done

ln -sf "$GOROOT/bin/"* "$GOPATH/bin/"

log_info "set" "$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 remove this one.

bin/g Outdated
echo
log_info "installing" "$version"
log_info "downloading" "$version"
Copy link
Owner

Choose a reason for hiding this comment

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

Let's re-rephrase to "selected"; this log has been important mainly to denote what version was resolved when using the latest keyword.

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.

install() {
install_version() {
version=$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

@stefanmaric
Copy link
Owner

@feualpha and also: can you squash all these changes once ready and craft a concise commit title and body to summarize the changes?

@@ -556,11 +556,15 @@ open_interactive_ui() {
# Activate <version>
#

activate() {
set_version() {
version=$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.

This changes implement the suggestion from issue stefanmaric#8 to add specific
command to switch between go version. previously it can be done by using
`g` interactive mode or by using `g install <version>`. the problem
comes from `g install <version>` command, because this command is also used to
download the specified go version. therefore `g set <version>` is added.

This changes also add `g download <version>` to download the specified go version without activating it.
this command replace `g install <version> --download`. the goal of this change is to make the
install function to be more conceise.
@stefanmaric
Copy link
Owner

stefanmaric commented Dec 16, 2019

@feaulpha thanks for your patience and your contribution! ❤️

I'll be releasing this along some other things sometime soon. Stay tuned!

@stefanmaric stefanmaric mentioned this pull request Dec 16, 2019
@oniraf oniraf deleted the use_command branch December 17, 2019 00:41
stefanmaric added a commit that referenced this pull request Dec 18, 2019
- Warn users about existing go installations
- Improve self-upgrade script
- Improve previous installation detection on g-install
- Make self-upgrade throw if g was not installed via g-install
- Add alias collision detection and setup helper (#11, thanks @alvinmatias69)
- Add `download` and `set` commands (#12, thanks @feualpha)
  * BREAKING: Remove the `--download` option
- Add Makefile with lint and format targets for better DX
- Fix shellcheck lint errors
- Format source code using make format
- Improve and update docs to reflect latest contributions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants