Code Monkey home page Code Monkey logo

g's Introduction

g

Simple go version manager, gluten-free.

screencast

Why?

Existing version managers build go from source, have too many dependencies, pollute the PATH, and/or require you to use a specific shell environment. g aims to be as unobtrusive and portable as possible.

g is inspired by tj/n - which I have contributed to in the past - and borrows some of its code.

Features

  • Run any go version on demand.
  • Single portable shell script that ideally lives where your go binaries live.
  • Works no matter what shell you use as long as $GOPATH and $GOROOT are exported which...
  • ...is idiomatic to go and not specific to g.
  • No need to source functions in your shell config.
  • Downloads pre-built binaries so it is fast and...
  • ...requires no git, no mercurial, no gcc, no xcode, etc.
  • curl and wget first-class support alike.
  • Colorful UI and interactive but safe to pipe and use in automated scripts.
  • self-upgrade command to keep up-to-date.
  • Option to get go beta versions.
  • Get started in golang with a single line using the install script.
  • The install script detects name collisions and helps you set an alternative name.

Requirements

Not strictly necessary, but highly recommended, to completely remove any previous go installation — just to prevent any weird outcome.

Single-line Installation

IMPORTANT: Before you continue, I encourage you to read the install script; never trust someone telling you to run random commands.

That said, you can install g with a single command:

curl -sSL https://git.io/g-install | sh -s

If you use wget instead:

wget -qO- https://git.io/g-install | sh -s

That will download the g script, put it inside $GOPATH/bin/, give it execution rights with chmod, and configure your default shell's initialization file, setting the GOPATH & GOROOT environment variables and adding $GOPATH/bin to the PATH.

Then you will prompted to install the latest version of go; you can skip this step and install your preferred version with g later.

NOTE: You must restart your current shell session for it to read these new env vars in order to use g or go.

Shell support

The install script currently supports the following shells:

  • bash
  • zsh
  • fish
  • ash
  • dash
  • csh
  • tcsh

That doesn't mean you cannot use g with other shells, just proceed with the manual installation.

The install script is going to select your default shell for configuration. You might see what your default shell is by running:

echo $SHELL

If you wish to configure a diff shell, you might pass it as arguments:

curl -sSL https://git.io/g-install | sh -s -- fish

You might as well configure several shells, but that's usually not required:

curl -sSL https://git.io/g-install | sh -s -- fish bash zsh

Use the -y option to skip the prompts and assume "yes" for everything:

curl -sSL https://git.io/g-install | sh -s -- -y

Changing defaults

By default, these go environment variables are used:

GOROOT: $HOME/.go
GOPATH: $HOME/go

$GOPATH/bin is added to the PATH and there's where g is copied to.

You might set those variables before running the install script. For example, in bash and zsh:

export GOROOT=~/.local/share/golang
export GOPATH=~/MyProjects/go-projects
curl -sSL https://git.io/g-install | sh -s

In fish:

set -gx GOROOT ~/.local/share/golang
set -gx GOPATH ~/MyProjects/go-projects
curl -sSL https://git.io/g-install | sh -s

Manual Installation

  1. Make sure to export the $GOPATH & $GOROOT environment variables and add $GOPATH/bin to your PATH.
  2. Grab a copy of the ./bin/g script and put it anywhere available in your PATH — inside $GOPATH/bin/ is a good option.
  3. Give the script execution rights with chmod +x $GOPATH/bin/g.
  4. Restart your shell session to make sure the env variables are loaded.

Upgrading

g includes a self-upgrade command you can run to get the latest version of g. This command simply runs the install script once again.

Usage

  Usage: g [COMMAND] [options] [args]

  Commands:

    g                         Open interactive UI with downloaded versions
    g install latest          Download and set the latest go release
    g install <version>       Download and set go <version>
    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)
    g prune                   Remove all versions except the current version
    g list                    Output downloaded go versions
    g list-all                Output all available, remote go versions
    g self-upgrade            Upgrades g to the latest version
    g help                    Display help information, same as g --help

  Options:

    -h, --help                Display help information and exit
    -v, --version             Output current version of g and exit
    -q, --quiet               Suppress almost all output
    -c, --no-color            Force disabled color output
    -y, --non-interactive     Prevent prompts
    -o, --os                  Override operating system
    -a, --arch                Override system architecture
    -u, --unstable            Include unstable versions in list

Uninstall

g is a single portable shell script that acts like a binary that's available in your $PATH. If you setup g with the install script, its removal should be pretty straight forward.

First of all, get some info about your system:

echo $SHELL
echo $GOROOT
echo $GOPATH
command -v g

You will notice the g file lives inside your $GOPATH/bin/ directory. If you only want to remove g and keep your currently installed go binaries and related files, simply delete g:

# If you're using bash, zsh, or other POSIX shell:
rm "$(command -v g)"

# If you're using fish:
rm (command -v g)

Now, if you want to remove everything, first be sure to backup your projects inside $GOROOT, if any. Then remove everything inside $GOROOT and $GOPATH:

rm -r $GOPATH $GOROOT

Then open your shell config file with your text editor of choice and look of for a line that includes g-install to remove it. These are the locations usually used by the install script:

# bash on MacOS
~/.bash_profile

# bash on linux/BSD
~/.bashrc

# zsh
~/.zshrc

# fish shell
~/.config/fish/config.fish

# csh
~/.cshrc

# tcsh
~/.tcshrc

# For ash and dash, check your $ENV var
echo $ENV

At this point you would have removed g and go entirely.

TODO

  • Improve docs a bit more
  • Make g and g-install POSIX compliant
  • Add support for more shells
  • 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
  • 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
  • Explore feature to configure shells to autoload go versions based on mod.go
  • Test it on diff platforms
  • Crete a test setup with docker and Github actions
  • Add more tests

The alternatives (and why I prefer g)

  • moovweb/gvm
    • Too many features.
    • Builds from source, i.e, requires gcc, make, xcode, etc.
    • Requires git and mercurial.
    • Requires bootstrapping, i.e., install go 1.4 first.
    • Specific to a shell, i.e., doesn't work with fishshell.
  • syndbg/goenv
    • Too many features and weird DX.
    • Uses an hard-coded list of go versions...
    • ...thus you have to upgrade goenv before installing a new version of go.
    • Adds a lot to your env and PATH.
  • hit9/oo
    • Pretty much same as gvm.
  • asdf-golang
  • andrewkroh/gvm
    • Bash, batch and powershell support only.
    • Cumbersome to use.
  • MakeNowJust/govm
    • Builds from source.
    • Requires python 2.

Contributing

Please read CONTRIBUTING.md. ♥

Acknowledgments

License

MIT

g's People

Contributors

aitbw avatar alvinmatias69 avatar jimeh avatar joelanford avatar stefanmaric avatar wintermi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

g's Issues

[question] naming of the tool - conflict with other commands

Thanks for the nice tool that support fish! Yey!

Just small feedback on the naming of the tool. Single character naming is I think not very fortunate because it might collide with user's aliases or some other utilities. In this case, I have a conflict with git aliases (https://github.com/simnalamburt/cgitc) and I can imagine there will be quiet some people who might have same problem.

Do you know if simply renaming the installed binary will break things or not?

Git IO issues

I am unable to reach git.io. The download links may have to be changed

Install failed: gzip error

To Reproduce

Steps to reproduce the behavior:

  1. Install with command: curl -sSL https://git.io/g-install | sh -s
  2. Install go: g install 1.22.1

Error output

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

Environment

Windows MSYS2 zsh

Additional context

image

Can't install versions bellow 1.4.3 on macOS

Whenever I try installing any version of Go lower than 1.5, I get the following error:

tar: Unrecognized archive format
tar: Error exit delayed from previous errors.

I believe this is because the script tries downloading from https://dl.google.com/go/go1.4.darwin-amd64.tar.gz, but if you go to https://golang.org/dl/ , there are no such links that follow the format that the script expects. It seems as if they only started following that format with 1.5 and later.

provide a flag to override dotfile_for_shell

Hi @stefanmaric thanks for this version manager.

Is it possible to provide a flag to the installer to specify the dotfile for shell.

I'm using the installer in an ansible role (https://galaxy.ansible.com/damianoneill/smg). Ansible uses a non-login shell over ssh, therefore for bash this will always select ~/.bashrc where ideally I want to update the dotfile for the login shell i.e. ~/.bash_profile.

Therefore if you could provide a way for me to overload the get_dotfile_for_shell response that would be fantastic.

Alternatively, if you have another suggestion that would be great.

Thanks,
Damian.

Allow installing this with the `yes` command on Linux

When installing this using a script it would be nice to be able to do the following:

wget -O g-install.sh https://git.io/g-install
sudo chmod ug+x g-install.sh
yes | ./g-install.sh

The yes command prints out "y" to the input stream of the command it is piped to. This is to prevent having to manually type "y" whilst installing g and the latest version of Go.

The yes command works with most other scripts, including the installer for n (Node version manager).

Invalid version when install go version < 1.16 on Mac M1 Apple Silicon

Describe the bug

Can not install/download Go version below 1.16. i.e: 1.13 and 1.15

To Reproduce

Steps to reproduce the behavior:

  1. check all that avail versions using g list-all. Version 1.13 and 1.15 exist on the list.
  2. execute g install 1.13

Error output

selected: 1.13

ERROR: invalid version 1.13

Environment

Please give us more info about the system you're getting issues at by running the following commands (copy & paste into your terminal and hit ENTER) and replacing the content here with the output of your terminal:

echo Architecuture: arm64
echo Full uname: Darwin my-mac.local 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan  5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T8101 arm64
echo Dollar-sign zero expansion: -zsh
echo SHELL: /bin/zsh
echo /etc/shells content:
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.
/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

echo go location: /Users/myusername/go/bin/go
go version go1.16 darwin/arm64
echo g location: /Users/myusername/go/bin/g
0.9.0
echo GOROOT: /Users/myusername/.go
echo GOPATH:  /Users/myusername/go

Additional context

Add any other context about the problem here.

add website link to CLI help

g is such a vague command name and there is no way to find out what the tool is, even when running g --help. Please add the repo URL and possibly a description.

ERROR: invalid version

Describe the bug

ERROR: invalid version

To Reproduce

g install latest

Error output

ERROR: invalid version

Environment

Please give us more info about the system you're getting issues at by running the following commands (copy & paste into your terminal and hit ENTER) and replacing the content here with the output of your terminal:

echo Architecuture: x86_64
echo Full uname: Linux Cyshall-Desktop 5.15.79.1-microsoft-standard-WSL2 #1 SMP Wed Nov 23 01:01:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
echo Dollar-sign zero expansion: "/usr/bin/zsh"
echo SHELL: "/usr/bin/zsh"
echo /etc/shells content:

/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/sh
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen
/bin/zsh
/usr/bin/zsh

echo go location: go version go1.20 linux/amd64
echo g location: /root/.go/bin/g && 0.9.1
echo GOROOT: "/root/.g/goT"
echo GOPATH: "/root/.go"


**Additional context**

Add any other context about the problem here.

Support go env as fallback for setting GOPATH and GOROOT in env

Sorry, I opened a PR without reading the contributing guidelines.

I'm proposing to support using go env as fallback for explicitly setting GOPATH and GOROOT. Go will read those values from a config file, e.g. ~/.config/go/env`, and thus setting them in the environment is not required.

[question] ls as an alias `g list-all`

It would be great to keep the usage of g similar to n. For example, n ls lists all the available node versions, but the corresponding command in g is g list-all. Is there any reason why ?

tmp: parameter not set

Recently, when running g self-upgrade I get the following error:

/home/[username]/go/bin/g: 748: /home/[username]/go/bin/g: tmp: parameter not set

Content of my .bashrc:

[...]
export GOPATH="$HOME/go"; export GOROOT="$HOME/.go"; export PATH="$GOPATH/bin:$PATH"; # g-install: do NOT edit, see https://github.com/stefanmaric/g

Is there anything I can add to help debugging this?

GOPATH default value is wrong

Even though README says that default values are:

GOROOT: $HOME/.go
GOPATH: $HOME/go

Upon g installation, the GOPATH variable in my config.fish is set to $HOME/.go, which then mess up everything, because upon installation of any version the g binary is removed. Not sure if it is only Fish specific or affects also other shells.

When I have corrected manually this error, everything worked as expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.