From cb5439157217a15537e45588f3d42334e62f0b1a Mon Sep 17 00:00:00 2001 From: Thomas Goldbrunner Date: Wed, 28 Jun 2017 13:26:42 +0200 Subject: [PATCH 1/3] gimme plugin: Initial commit --- plugins/gimme/LICENSE | 21 ++++++ plugins/gimme/README.md | 17 +++++ plugins/gimme/gimme.plugin.zsh | 117 +++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 plugins/gimme/LICENSE create mode 100644 plugins/gimme/README.md create mode 100644 plugins/gimme/gimme.plugin.zsh diff --git a/plugins/gimme/LICENSE b/plugins/gimme/LICENSE new file mode 100644 index 000000000..f6e1dc76c --- /dev/null +++ b/plugins/gimme/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Thomas Goldbrunner + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/plugins/gimme/README.md b/plugins/gimme/README.md new file mode 100644 index 000000000..095a36b9f --- /dev/null +++ b/plugins/gimme/README.md @@ -0,0 +1,17 @@ +### Gimme plugin for oh-my-zsh + +[Oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh/) plugin making life a +bit easier, when you use [gimme](https://github.com/travis-ci/gimme/) to manage +[go](https://golang.org) installations. + +Provides following functions/aliases: + - ```go-versions```: Alias for ```gimme -l``` + - ```install-gimme```: Download latest version of gimme to ~/bin + - ```load-go```: Load the go version specified by the first argument. + If called without argument, the 'stable' version will be loaded. + - ```remove-go```: Remove the go version specified by the first argument from + ~/.gimme/* + - Completions for ```gimme``` and ```load-go``` + +```gimme``` is assumed to be in ```~/bin```, so make +sure to add this folder to the $PATH variable. diff --git a/plugins/gimme/gimme.plugin.zsh b/plugins/gimme/gimme.plugin.zsh new file mode 100644 index 000000000..913cf38c3 --- /dev/null +++ b/plugins/gimme/gimme.plugin.zsh @@ -0,0 +1,117 @@ +# Load a go version. +# If no version is provided, 'stable' will be loaded. +load-go() { + # check if gimme is available (unfortunately eval does not return a non-zero + # value if the command is not found, therefore we need to check before) + gimme &>/dev/null + if [[ $? == 127 ]] ; then + echo "gimme was not found in your PATH. +Run install-gimme and make sure to add ~/bin to your PATH." + return 1 + fi + if [[ "$#" == 0 ]] ; then + eval "$(gimme stable)" + else + eval "$(gimme ${*})" + fi + return $? +} + +# alias for gimme -l +alias go-versions='gimme -l' + +# download latest version of gimme +install-gimme() { + if ! [ -d ~/bin ] ; then + mkdir ~/bin || return 1 + fi + curl -sL -o ~/bin/gimme https://raw.githubusercontent.com/travis-ci/gimme/master/gimme + chmod +x ~/bin/gimme || return 1 + return 0 +} + +# remove go version from ~/.gimme/* +remove-go() { + if [[ "$#" != 1 ]] ; then + echo "Usage: remove-go GO_VERSION" + return 0 + else + if [ "$1" = "stable" ] ; then + if ! [ -e ~/.gimme/versions/stable ] ; then + echo "go version stable is not installed" + return 0 + fi + version="$(cat ~/.gimme/versions/stable)" || return 1 + rm ~/.gimme/versions/stable || return 1 + else + version="$1" + fi + if ! [ $(ls ~/.gimme/versions | grep "$version") ] ; then + echo "go version $version is not installed" + return 0 + else + rm -r ~/.gimme/versions/go"$version"* || return 1 + rm ~/.gimme/envs/go"$version"* || return 1 + fi + return 0 + fi +} + +# gimme completion +__gimme_completion() { + local version_1 version_2 version_3 + version_1="1.8.3" + version_2="1.7.6" + version_3="1.6.4" + local context state state_descr line + typeset -a go_versions + go_versions+=( + '(help version force list tip '$version_1' '$version_2' '$version_3' \ + )stable[install latest stable go version]' + '(help version force list stable '$version_1' '$version_2' '$version_3' \ + )tip[install development version (master branch) of go]' + '(help version force list stable tip '$version_2' '$version_3' \ + )'$version_1'[install go version '$version_1']' + '(help version force list stable tip '$version_1' '$version_3' \ + )'$version_2'[install go version '$version_2']' + '(help version force list stable tip '$version_1' '$version_2' \ + )'$version_3'[install go version '$version_3']' + ) + typeset -a flags + flags+=( + '(version force list stable tip '$version_1' '$version_2' \ + '$version_3')help[show help text and exit]' + '(help force list stable tip '$version_1' '$version_2' \ + '$version_3')version[show the gimme version only and exit]' + '(help version list)force[remove the existing go installation if present prior to install]' + '(help version force stable tip '$version_1' '$version_2' \ + '$version_3')list[list installed go versions and exit]' + ) + _values -w 'flags go_versions' ${flags[@]} ${go_versions[@]} + return +} + +__load-go_completion() { + local version_1 version_2 version_3 + version_1="1.8.3" + version_2="1.7.6" + version_3="1.6.4" + local context state state_descr line + typeset -a go_versions + go_versions+=( + '(tip '$version_1' '$version_2' '$version_3' \ + )stable[latest stable go version]' + '(stable '$version_1' '$version_2' '$version_3' \ + )tip[development version (master branch) of go]' + '(stable tip '$version_2' '$version_3' \ + )'$version_1'[go version '$version_1']' + '(stable tip '$version_1' '$version_3' \ + )'$version_2'[go version '$version_2']' + '(stable tip '$version_1' '$version_2' \ + )'$version_3'[go version '$version_3']' + ) + _values 'go_versions' ${go_versions[@]} +} + +compdef __gimme_completion gimme +compdef __load-go_completion load-go From 826bd61fb8fa9959daa810aced8ff8d2f2782293 Mon Sep 17 00:00:00 2001 From: Thomas Goldbrunner Date: Thu, 27 Jul 2017 12:52:38 +0200 Subject: [PATCH 2/3] Accept only one argument for load-go --- plugins/gimme/gimme.plugin.zsh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/gimme/gimme.plugin.zsh b/plugins/gimme/gimme.plugin.zsh index 913cf38c3..3b387e434 100644 --- a/plugins/gimme/gimme.plugin.zsh +++ b/plugins/gimme/gimme.plugin.zsh @@ -9,7 +9,10 @@ load-go() { Run install-gimme and make sure to add ~/bin to your PATH." return 1 fi - if [[ "$#" == 0 ]] ; then + if [[ "$#" > 1 ]] ; then + echo "Usage: load-go GO_VERSION" + return 0 + elif [[ "$#" == 0 ]] ; then eval "$(gimme stable)" else eval "$(gimme ${*})" From dddc7bfedb813ffde4c8d632f5416cfa8c09dda5 Mon Sep 17 00:00:00 2001 From: Thomas Goldbrunner Date: Thu, 27 Jul 2017 12:53:49 +0200 Subject: [PATCH 3/3] Enhancements for remove-go - add completion for remove-go - remove-go can now handle the "tip" version --- plugins/gimme/README.md | 2 +- plugins/gimme/gimme.plugin.zsh | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/plugins/gimme/README.md b/plugins/gimme/README.md index 095a36b9f..0d5eae989 100644 --- a/plugins/gimme/README.md +++ b/plugins/gimme/README.md @@ -11,7 +11,7 @@ Provides following functions/aliases: If called without argument, the 'stable' version will be loaded. - ```remove-go```: Remove the go version specified by the first argument from ~/.gimme/* - - Completions for ```gimme``` and ```load-go``` + - Completions for ```gimme```, ```load-go``` and ```remove-go``` ```gimme``` is assumed to be in ```~/bin```, so make sure to add this folder to the $PATH variable. diff --git a/plugins/gimme/gimme.plugin.zsh b/plugins/gimme/gimme.plugin.zsh index 3b387e434..97c493f53 100644 --- a/plugins/gimme/gimme.plugin.zsh +++ b/plugins/gimme/gimme.plugin.zsh @@ -39,7 +39,16 @@ remove-go() { echo "Usage: remove-go GO_VERSION" return 0 else - if [ "$1" = "stable" ] ; then + if [ "$1" = "tip" ] ; then + if ! [ -e ~/.gimme/versions/go ] ; then + echo "go version tip is not installed" + return 0 + fi + rm -rf ~/.gimme/versions/go || return 1 + rm ~/.gimme/envs/gotip.env || return 1 + rm ~/.gimme/envs/go.git.* || return 1 + return 0 + elif [ "$1" = "stable" ] ; then if ! [ -e ~/.gimme/versions/stable ] ; then echo "go version stable is not installed" return 0 @@ -116,5 +125,19 @@ __load-go_completion() { _values 'go_versions' ${go_versions[@]} } +__remove-go_completion() { + local context state state_descr line + typeset -a installed_versions + if [ "$(ls ~/.gimme/versions | grep stable)" ] ; then + installed_versions+=('stable') + fi + if [ -d ~/.gimme/versions/go ] ; then + installed_versions+=('tip') + fi + installed_versions+=($(ls ~/.gimme/versions | egrep -oZ '[0-9].[0-9].[0-9]')) + _values -w 'installed_versions' ${installed_versions[@]} +} + compdef __gimme_completion gimme compdef __load-go_completion load-go +compdef __remove-go_completion remove-go