├── .github └── workflows │ ├── go.yml │ ├── release.yml │ └── releasev2.yml ├── .gitignore ├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── internal ├── lsremote.go ├── lsremote_test.go └── use.go ├── main.go └── v2 ├── go.mod ├── go.sum ├── internal ├── lsremote.go ├── lsremote_test.go └── use.go └── main.go /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: Go 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | build: 11 | name: Build 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Set up Go 1.x 15 | uses: actions/setup-go@v5 16 | with: 17 | go-version: ^1.23 18 | 19 | - name: Check out code into the Go module directory 20 | uses: actions/checkout@v4 21 | 22 | - name: Get dependencies 23 | run: | 24 | go get -v -t -d ./... 25 | if [ -f Gopkg.toml ]; then 26 | curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh 27 | dep ensure 28 | fi 29 | 30 | - name: Build 31 | run: go build -v main.go 32 | 33 | - name: Test 34 | run: go test -v ./... 35 | 36 | - name: Build 37 | run: go build -v main.go 38 | working-directory: v2 39 | 40 | - name: Test 41 | run: go test -v ./... 42 | working-directory: v2 43 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | on: 3 | push: 4 | tags: 5 | - "v1.[0-9]+.[0-9]+" 6 | jobs: 7 | goreleaser: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Checkout 11 | uses: actions/checkout@v1 12 | with: 13 | fetch-depth: 1 14 | - name: Setup Go 15 | uses: actions/setup-go@v1 16 | with: 17 | go-version: 1.16 18 | - name: Run GoReleaser 19 | uses: goreleaser/goreleaser-action@v1 20 | with: 21 | version: latest 22 | args: release --rm-dist 23 | env: 24 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 25 | -------------------------------------------------------------------------------- /.github/workflows/releasev2.yml: -------------------------------------------------------------------------------- 1 | name: release-v2 2 | on: 3 | push: 4 | tags: 5 | - "v2.[0-9]+.[0-9]+-**" 6 | 7 | permissions: 8 | contents: write 9 | 10 | jobs: 11 | goreleaser: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v4 16 | with: 17 | fetch-depth: 1 18 | - name: Setup Go 19 | uses: actions/setup-go@v5 20 | with: 21 | go-version: 1.23 22 | - name: Run GoReleaser 23 | uses: goreleaser/goreleaser-action@v6 24 | with: 25 | distribution: goreleaser 26 | version: '~> v2' 27 | args: release --clean 28 | env: 29 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/go 3 | # Edit at https://www.gitignore.io/?templates=go 4 | 5 | ### Go ### 6 | # Binaries for programs and plugins 7 | *.exe 8 | *.exe~ 9 | *.dll 10 | *.so 11 | *.dylib 12 | 13 | # Test binary, built with `go test -c` 14 | *.test 15 | 16 | # Output of the go coverage tool, specifically when used with LiteIDE 17 | *.out 18 | 19 | # Dependency directories (remove the comment below to include it) 20 | # vendor/ 21 | 22 | ### Go Patch ### 23 | /vendor/ 24 | /Godeps/ 25 | 26 | goswitch 27 | 28 | # End of https://www.gitignore.io/api/go 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [2020] [Akito Ito] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # goswitch 2 | 3 | go version switching utility. 4 | goswitch is just a wrapper for `go get` and `ln -s`. 5 | 6 | ## How to use 7 | 8 | ### Prerequisites 9 | 10 | - Go 1.13+ 11 | 12 | ### Installing 13 | 14 | ``` 15 | $ go get -u github.com/akito0107/goswitch 16 | ``` 17 | 18 | ### Example 19 | 20 | 1. check current version. 21 | 22 | ``` 23 | $ go version 24 | go version go1.13.10 darwin/amd64 25 | ``` 26 | 27 | 2. show all available versions by `goswitch ls-remote` 28 | 29 | ``` 30 | $ goswitch ls-remote 31 | 32 | available versions: 33 | 34 | go1 35 | go1.2.2 36 | go1.3, go1.3.1, go1.3.2, go1.3.3 37 | go1.4, go1.4.1, go1.4.2, go1.4.3 38 | go1.5, go1.5.1, go1.5.2, go1.5.3, go1.5.4 39 | go1.6, go1.6.1, go1.6.2, go1.6.3, go1.6.4 40 | go1.7, go1.7.1, go1.7.3, go1.7.4, go1.7.5, go1.7.6 41 | go1.8, go1.8.1, go1.8.2, go1.8.3, go1.8.4, go1.8.5, go1.8.6, go1.8.7 42 | go1.9, go1.9.1, go1.9.2, go1.9.3, go1.9.4, go1.9.5, go1.9.6, go1.9.7 43 | go1.10, go1.10.1, go1.10.2, go1.10.3, go1.10.4, go1.10.5, go1.10.6, go1.10.7, go1.10.8 44 | go1.11, go1.11.1, go1.11.2, go1.11.3, go1.11.4, go1.11.5, go1.11.6, go1.11.7, go1.11.8, go1.11.9, go1.11.10, go1.11.11, go1.11.12, go1.11.13 45 | go1.12, go1.12.1, go1.12.2, go1.12.3, go1.12.4, go1.12.5, go1.12.6, go1.12.7, go1.12.8, go1.12.9, go1.12.10, go1.12.11, go1.12.12, go1.12.13, go1.12.14, go1.12.15, go1.12.16, go1.12.17 46 | go1.13, go1.13.1, go1.13.2, go1.13.3, go1.13.4, go1.13.5, go1.13.6, go1.13.7, go1.13.8, go1.13.9, go1.13.10 47 | go1.14, go1.14.1, go1.14.2 48 | ``` 49 | 50 | 3. switch go version by `goswitch use` 51 | 52 | ``` 53 | $ goswitch use go1.14.2 54 | 2020/04/09 11:22:24 start go get... 55 | 2020/04/09 11:22:24 go get golang.org/dl/go1.14.2 56 | go: downloading golang.org/dl v0.0.0-20200408221700-d6f4cf58dce2 57 | go: found golang.org/dl/go1.14.2 in golang.org/dl v0.0.0-20200408221700-d6f4cf58dce2 58 | 2020/04/09 11:22:27 go get finished. start download 59 | Downloaded 0.0% ( 14448 / 125040726 bytes) ... 60 | .... 61 | Downloaded 100.0% (125040726 / 125040726 bytes) 62 | Unpacking /Users/akito.ito/sdk/go1.14.2/go1.14.2.darwin-amd64.tar.gz ... 63 | Success. You may now run 'go1.14.2' 64 | 2020/04/09 11:23:23 download finished. 65 | 2020/04/09 11:23:23 switch go version 66 | ``` 67 | 68 | 4. check current version 69 | 70 | ``` 71 | $ go version 72 | go version go1.14.2 darwin/amd64 73 | ``` 74 | 75 | 5. (option) using system go version 76 | 77 | You can use `system` verison go. 78 | 79 | ``` 80 | $ goswitch use system 81 | ``` 82 | 83 | `goswitch use system` just remove `$GOBIN/go` symlink. 84 | 85 | ## Options 86 | 87 | ```sh 88 | $ goswitch -h 89 | NAME: 90 | goswitch - go version switching utility 91 | 92 | USAGE: 93 | goswitch [global options] command [command options] [arguments...] 94 | 95 | COMMANDS: 96 | use switch current go version 97 | ls-remote show all available versions 98 | help, h Shows a list of commands or help for one command 99 | 100 | GLOBAL OPTIONS: 101 | --help, -h show help (default: false) 102 | ``` 103 | 104 | ```sh 105 | $ goswitch ls-remote -h 106 | NAME: 107 | goswitch ls-remote - show all available versions 108 | 109 | USAGE: 110 | goswitch ls-remote [command options][arguments...] 111 | 112 | OPTIONS: 113 | --use-github, -g use github tags (default: false) 114 | --help, -h show help (default: false) 115 | ``` 116 | 117 | ## License 118 | 119 | This project is licensed under the Apache License 2.0 License - see the [LICENSE](LICENSE) file for details 120 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/akito0107/goswitch 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/google/go-github/v32 v32.1.0 7 | github.com/urfave/cli/v2 v2.1.1 8 | golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect 9 | golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 10 | ) 11 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= 3 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 4 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 5 | github.com/google/go-github/v32 v32.1.0 h1:GWkQOdXqviCPx7Q7Fj+KyPoGm4SwHRh8rheoPhd27II= 6 | github.com/google/go-github/v32 v32.1.0/go.mod h1:rIEpZD9CTDQwDK9GDrtMTycQNA4JU3qBsCizh3q2WCI= 7 | github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= 8 | github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= 9 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 10 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 11 | github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= 12 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 13 | github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= 14 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 15 | github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= 16 | github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= 17 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= 18 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 19 | golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E= 20 | golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 21 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 22 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 23 | golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk= 24 | golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 25 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 26 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 27 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 28 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 29 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 30 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 31 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 32 | -------------------------------------------------------------------------------- /internal/lsremote.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "fmt" 7 | "log" 8 | "net/http" 9 | "sort" 10 | "strconv" 11 | "strings" 12 | 13 | "github.com/google/go-github/v32/github" 14 | "golang.org/x/net/html" 15 | ) 16 | 17 | type goversion string 18 | 19 | func (g goversion) String() string { 20 | return string(g) 21 | } 22 | 23 | func (g goversion) Major() int { 24 | return 1 25 | } 26 | 27 | func (g goversion) Minor() int { 28 | vs := strings.SplitN(string(g), ".", 3) 29 | if len(vs) == 1 { 30 | return 0 31 | } 32 | i, err := strconv.Atoi(trimExtra(vs[1])) 33 | if err != nil { 34 | log.Panic(err) 35 | } 36 | return i 37 | } 38 | 39 | func (g goversion) RCVersion() int { 40 | vs := strings.SplitN(string(g), "rc", 2) 41 | if len(vs) == 1 { 42 | return 1000 43 | } 44 | 45 | i, err := strconv.Atoi(vs[1]) 46 | if err != nil { 47 | log.Panic(err) 48 | } 49 | 50 | return i 51 | } 52 | 53 | func (g goversion) BetaVersion() int { 54 | vs := strings.SplitN(string(g), "beta", 2) 55 | if len(vs) == 1 { 56 | return 1000 57 | } 58 | i, err := strconv.Atoi(vs[1]) 59 | if err != nil { 60 | log.Panic(err) 61 | } 62 | return i 63 | } 64 | 65 | func (g goversion) Patch() int { 66 | vs := strings.SplitN(string(g), ".", 3) 67 | if len(vs) != 3 { 68 | return 0 69 | } 70 | i, err := strconv.Atoi(trimExtra(vs[2])) 71 | if err != nil { 72 | log.Panic(err) 73 | } 74 | return i 75 | } 76 | 77 | func trimExtra(v string) string { 78 | extras := []string{"beta", "rc"} 79 | for _, extra := range extras { 80 | index := strings.Index(v, extra) 81 | if index >= 0 { 82 | return v[:index] 83 | } 84 | } 85 | return v 86 | } 87 | 88 | const initialPage = 4 89 | const perPage = 30 // max 90 | 91 | func LSRemoteGH(c context.Context) error { 92 | client := github.NewClient(nil) 93 | 94 | var versions []goversion 95 | page := initialPage 96 | for { 97 | tags, next, err := fetchNextTags(c, client, page) 98 | if err != nil { 99 | return fmt.Errorf("fetchTags failed: %w", err) 100 | } 101 | if next == 0 { 102 | break 103 | } 104 | versions = append(versions, tags...) 105 | page = next 106 | } 107 | 108 | versions = sortVersions(versions) 109 | printVersions(versions) 110 | 111 | return nil 112 | } 113 | 114 | func fetchNextTags(c context.Context, client *github.Client, nextPage int) ([]goversion, int, error) { 115 | tags, resp, err := client.Repositories.ListTags(c, "golang", "go", &github.ListOptions{ 116 | Page: nextPage, 117 | PerPage: perPage, 118 | }) 119 | if err != nil { 120 | return nil, 0, fmt.Errorf("page %d fetch failed: %w", nextPage, err) 121 | } 122 | 123 | var result []goversion 124 | 125 | for _, t := range tags { 126 | if strings.HasPrefix(t.GetName(), "go1") { 127 | result = append(result, goversion(t.GetName())) 128 | } 129 | } 130 | 131 | return result, resp.NextPage, nil 132 | } 133 | 134 | const goDLURL = "https://golang.org/dl/" 135 | 136 | func LSRemote(c context.Context) error { 137 | req, err := http.NewRequestWithContext(c, "GET", goDLURL, nil) 138 | if err != nil { 139 | return fmt.Errorf("create request failed: %w", err) 140 | } 141 | 142 | res, err := http.DefaultClient.Do(req) 143 | if err != nil { 144 | return fmt.Errorf("http request failed: %w", err) 145 | } 146 | defer res.Body.Close() 147 | 148 | doc, err := html.Parse(res.Body) 149 | if err != nil { 150 | return fmt.Errorf("html parse failed: %w", err) 151 | } 152 | 153 | var f func(*html.Node) 154 | 155 | var versions []goversion 156 | 157 | f = func(n *html.Node) { 158 | if n.Type == html.ElementNode && n.Data == "h3" { 159 | maybeVersion := n.FirstChild.Data 160 | 161 | if strings.HasPrefix(maybeVersion, "go1") { 162 | vs := strings.SplitN(maybeVersion, " ", 2) 163 | versions = append(versions, goversion(vs[0])) 164 | } 165 | } 166 | for ch := n.FirstChild; ch != nil; ch = ch.NextSibling { 167 | f(ch) 168 | } 169 | } 170 | 171 | f(doc) 172 | 173 | if len(versions) == 0 { 174 | panic("Maybe the DOM structure has changed. Would you please file an issue https://github.com/akito0107/goswitch/issues/new") 175 | } 176 | 177 | versions = sortVersions(versions) 178 | printVersions(versions) 179 | 180 | return nil 181 | } 182 | 183 | func sortVersions(versions []goversion) []goversion { 184 | sort.Slice(versions, func(i, j int) bool { 185 | if versions[i].Minor() != versions[j].Minor() { 186 | return versions[i].Minor() < versions[j].Minor() 187 | } 188 | if versions[i].Patch() != versions[j].Patch() { 189 | return versions[i].Patch() < versions[j].Patch() 190 | } 191 | if versions[i].BetaVersion() != versions[j].BetaVersion() { 192 | return versions[i].BetaVersion() < versions[j].BetaVersion() 193 | } 194 | return versions[i].RCVersion() < versions[j].RCVersion() 195 | }) 196 | 197 | return versions 198 | } 199 | 200 | func printVersions(versions []goversion) { 201 | m := make(map[goversion]struct{}) 202 | 203 | var vs []goversion 204 | for _, v := range versions { 205 | _, ok := m[v] 206 | if ok { 207 | continue 208 | } 209 | m[v] = struct{}{} 210 | vs = append(vs, v) 211 | } 212 | 213 | currentMinor := vs[0].Minor() 214 | 215 | fmt.Println() 216 | fmt.Println("available versions:") 217 | fmt.Println() 218 | fmt.Println("system") 219 | 220 | var buf bytes.Buffer 221 | for i, v := range vs { 222 | if currentMinor != v.Minor() { 223 | fmt.Fprintln(&buf) 224 | currentMinor = v.Minor() 225 | } else if i != 0 { 226 | fmt.Fprintf(&buf, ", ") 227 | } 228 | 229 | fmt.Fprintf(&buf, "%s", v) 230 | } 231 | 232 | fmt.Println(buf.String()) 233 | } 234 | -------------------------------------------------------------------------------- /internal/lsremote_test.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | ) 7 | 8 | func Test_sortVersions(t *testing.T) { 9 | 10 | cases := []struct { 11 | name string 12 | in []goversion 13 | expect []goversion 14 | }{ 15 | { 16 | name: "minor version", 17 | in: []goversion{"1.13", "1.11", "1.12"}, 18 | expect: []goversion{"1.11", "1.12", "1.13"}, 19 | }, 20 | { 21 | name: "patch version", 22 | in: []goversion{"1.13.1", "1.13.3", "1.13.2"}, 23 | expect: []goversion{"1.13.1", "1.13.2", "1.13.3"}, 24 | }, 25 | { 26 | name: "with rc", 27 | in: []goversion{"1.13rc1", "1.13.1", "1.13"}, 28 | expect: []goversion{"1.13rc1", "1.13", "1.13.1"}, 29 | }, 30 | { 31 | name: "with beta", 32 | in: []goversion{"1.13beta1", "1.13.1", "1.13"}, 33 | expect: []goversion{"1.13beta1", "1.13", "1.13.1"}, 34 | }, 35 | { 36 | name: "complex case", 37 | in: []goversion{"1.13rc1", "1.13beta1", "1.13.1", "1.13", "1.1", "1.14rc1", "1.13rc2"}, 38 | expect: []goversion{"1.1", "1.13beta1", "1.13rc1", "1.13rc2", "1.13", "1.13.1", "1.14rc1"}, 39 | }, 40 | { 41 | name: "patch with rc", 42 | in: []goversion{"go1.9", "go1.9.2", "go1.9.2rc2"}, 43 | expect: []goversion{"go1.9", "go1.9.2rc2", "go1.9.2"}, 44 | }, 45 | } 46 | 47 | for _, c := range cases { 48 | t.Run(c.name, func(t *testing.T) { 49 | act := sortVersions(c.in) 50 | if !reflect.DeepEqual(act, c.expect) { 51 | t.Errorf("must be sorted as %v but %v", c.expect, act) 52 | } 53 | }) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /internal/use.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "fmt" 7 | "go/build" 8 | "log" 9 | "os" 10 | "os/exec" 11 | "path/filepath" 12 | "strings" 13 | ) 14 | 15 | const goGetURL = "golang.org/dl/" 16 | 17 | func Use(ctx context.Context, version string) error { 18 | 19 | gobin := gobinPath() 20 | symlinkPath := filepath.Join(gobin, "go") 21 | 22 | if version == "system" { 23 | log.Println("using system go") 24 | log.Println("removing symlink...") 25 | return rmSymlink(symlinkPath) 26 | } 27 | 28 | if !versionExists(version) { 29 | v, err := currentGoVersion(ctx) 30 | if err != nil { 31 | return fmt.Errorf("get current go version: %w", err) 32 | } 33 | var cmd *exec.Cmd 34 | if v.Major() > 1 || (v.Major() == 1 && v.Minor() >= 17) { 35 | cmd = exec.CommandContext(ctx, "go", "install", goGetURL+version+"@latest") 36 | } else { 37 | cmd = exec.CommandContext(ctx, "go", "get", goGetURL+version) 38 | cmd.Env = append(os.Environ(), "GO111MODULE=off") 39 | } 40 | sub := cmd.Args[1] 41 | log.Printf("start go %s...", sub) 42 | log.Print(strings.Join(cmd.Args, " ")) 43 | 44 | cmd.Stdout = log.Writer() 45 | cmd.Stderr = log.Writer() 46 | 47 | if err := cmd.Run(); err != nil { 48 | return fmt.Errorf("go %s failed: %w", sub, err) 49 | } 50 | log.Printf("go %s finished. start download", sub) 51 | } else { 52 | log.Printf("already exists version: %s, start download\n", version) 53 | } 54 | 55 | dlcmd := exec.CommandContext(ctx, version, "download") 56 | 57 | dlcmd.Stdout = log.Writer() 58 | dlcmd.Stderr = log.Writer() 59 | 60 | if err := dlcmd.Run(); err != nil { 61 | return fmt.Errorf("%s download failed: %w", version, err) 62 | } 63 | 64 | log.Println("download finished.") 65 | log.Println("switch go version") 66 | 67 | if err := rmSymlink(symlinkPath); err != nil { 68 | return err 69 | } 70 | 71 | if err := os.Symlink(filepath.Join(gobin, version), symlinkPath); err != nil { 72 | return fmt.Errorf("create symlink failed: %w", err) 73 | } 74 | 75 | return nil 76 | } 77 | 78 | func rmSymlink(symlinkPath string) error { 79 | if info, err := os.Lstat(symlinkPath); err == nil { 80 | if info.Mode()&os.ModeSymlink != os.ModeSymlink { 81 | return fmt.Errorf("go command is not a symlink but %s, goswitch use symlink. please delete or make a symlink", info.Mode()) 82 | } 83 | 84 | if err := os.Remove(symlinkPath); err != nil { 85 | return fmt.Errorf("remove go link failed: %w", err) 86 | } 87 | } 88 | 89 | return nil 90 | } 91 | 92 | func gobinPath() string { 93 | gobin := os.Getenv("GOBIN") 94 | 95 | if gobin == "" { 96 | gobin = filepath.Join(build.Default.GOPATH, "bin") 97 | } 98 | 99 | return gobin 100 | } 101 | 102 | func versionExists(version string) bool { 103 | gobin := gobinPath() 104 | _, err := os.Stat(filepath.Join(gobin, version)) 105 | 106 | return err == nil 107 | } 108 | 109 | func currentGoVersion(ctx context.Context) (goversion, error) { 110 | b, err := exec.CommandContext(ctx, "go", "env", "GOVERSION").Output() 111 | if err != nil { 112 | return "", fmt.Errorf("exec go env GOVERSION: %w", err) 113 | } 114 | return goversion(bytes.TrimSuffix(b, []byte("\n"))), nil 115 | } 116 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/urfave/cli/v2" 8 | 9 | "github.com/akito0107/goswitch/internal" 10 | ) 11 | 12 | func main() { 13 | app := &cli.App{ 14 | Name: "goswitch", 15 | Usage: "go version switching utility", 16 | Commands: []*cli.Command{ 17 | { 18 | Name: "use", 19 | Usage: "switch current go version", 20 | Action: func(c *cli.Context) error { 21 | v := c.Args().Get(0) 22 | return internal.Use(c.Context, v) 23 | }, 24 | }, 25 | { 26 | Name: "ls-remote", 27 | Usage: "show all available versions", 28 | Flags: []cli.Flag{ 29 | &cli.BoolFlag{ 30 | Name: "use-github", 31 | Aliases: []string{"g"}, 32 | Usage: "use github tags", 33 | Required: false, 34 | }, 35 | }, 36 | Action: func(c *cli.Context) error { 37 | if c.Bool("use-github") { 38 | return internal.LSRemoteGH(c.Context) 39 | } 40 | return internal.LSRemote(c.Context) 41 | }, 42 | }, 43 | }, 44 | } 45 | 46 | if err := app.Run(os.Args); err != nil { 47 | log.Fatal(err) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /v2/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/akito0107/goswitch/v2 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/google/go-github/v32 v32.1.0 7 | github.com/google/go-querystring v1.1.0 // indirect 8 | github.com/pmezard/go-difflib v1.0.0 // indirect 9 | github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect 10 | github.com/urfave/cli/v2 v2.27.4 11 | golang.org/x/net v0.29.0 12 | gopkg.in/yaml.v2 v2.2.2 // indirect 13 | ) 14 | -------------------------------------------------------------------------------- /v2/go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= 3 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= 4 | github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= 5 | github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= 6 | github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 7 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 8 | github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 9 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 10 | github.com/google/go-github/v32 v32.1.0 h1:GWkQOdXqviCPx7Q7Fj+KyPoGm4SwHRh8rheoPhd27II= 11 | github.com/google/go-github/v32 v32.1.0/go.mod h1:rIEpZD9CTDQwDK9GDrtMTycQNA4JU3qBsCizh3q2WCI= 12 | github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= 13 | github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= 14 | github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= 15 | github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= 16 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 17 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 18 | github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= 19 | github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 20 | github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= 21 | github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 22 | github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= 23 | github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 24 | github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= 25 | github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= 26 | github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8= 27 | github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ= 28 | github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= 29 | github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= 30 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 31 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 32 | golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E= 33 | golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 34 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 35 | golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= 36 | golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= 37 | golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= 38 | golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= 39 | golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= 40 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 41 | golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 42 | golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 43 | golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 44 | golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 45 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 46 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 47 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 48 | golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk= 49 | golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 50 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 51 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 52 | golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 53 | golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= 54 | golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= 55 | golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= 56 | golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= 57 | golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= 58 | golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= 59 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 60 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 61 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 62 | golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 63 | golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= 64 | golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 65 | golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 66 | golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 67 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 68 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 69 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 70 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 71 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 72 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 73 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 74 | golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 75 | golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 76 | golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 77 | golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 78 | golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 79 | golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= 80 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 81 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 82 | golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= 83 | golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= 84 | golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= 85 | golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= 86 | golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= 87 | golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8= 88 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 89 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 90 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 91 | golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 92 | golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= 93 | golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 94 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 95 | golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 96 | golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= 97 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 98 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 99 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 100 | golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= 101 | golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= 102 | golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= 103 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 104 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 105 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 106 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 107 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 108 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 109 | -------------------------------------------------------------------------------- /v2/internal/lsremote.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "fmt" 7 | "log" 8 | "net/http" 9 | "sort" 10 | "strconv" 11 | "strings" 12 | 13 | "github.com/google/go-github/v32/github" 14 | "golang.org/x/net/html" 15 | ) 16 | 17 | type goversion string 18 | 19 | func (g goversion) String() string { 20 | return string(g) 21 | } 22 | 23 | func (g goversion) Major() int { 24 | return 1 25 | } 26 | 27 | func (g goversion) Minor() int { 28 | vs := strings.SplitN(string(g), ".", 3) 29 | if len(vs) == 1 { 30 | return 0 31 | } 32 | i, err := strconv.Atoi(trimExtra(vs[1])) 33 | if err != nil { 34 | log.Panic(err) 35 | } 36 | return i 37 | } 38 | 39 | func (g goversion) RCVersion() int { 40 | vs := strings.SplitN(string(g), "rc", 2) 41 | if len(vs) == 1 { 42 | return 1000 43 | } 44 | 45 | i, err := strconv.Atoi(vs[1]) 46 | if err != nil { 47 | log.Panic(err) 48 | } 49 | 50 | return i 51 | } 52 | 53 | func (g goversion) BetaVersion() int { 54 | vs := strings.SplitN(string(g), "beta", 2) 55 | if len(vs) == 1 { 56 | return 1000 57 | } 58 | i, err := strconv.Atoi(vs[1]) 59 | if err != nil { 60 | log.Panic(err) 61 | } 62 | return i 63 | } 64 | 65 | func (g goversion) Patch() int { 66 | vs := strings.SplitN(string(g), ".", 3) 67 | if len(vs) != 3 { 68 | return 0 69 | } 70 | i, err := strconv.Atoi(trimExtra(vs[2])) 71 | if err != nil { 72 | log.Panic(err) 73 | } 74 | return i 75 | } 76 | 77 | func trimExtra(v string) string { 78 | extras := []string{"beta", "rc"} 79 | for _, extra := range extras { 80 | index := strings.Index(v, extra) 81 | if index >= 0 { 82 | return v[:index] 83 | } 84 | } 85 | return v 86 | } 87 | 88 | const initialPage = 4 89 | const perPage = 30 // max 90 | 91 | func LSRemoteGH(c context.Context) error { 92 | client := github.NewClient(nil) 93 | 94 | var versions []goversion 95 | page := initialPage 96 | for { 97 | tags, next, err := fetchNextTags(c, client, page) 98 | if err != nil { 99 | return fmt.Errorf("fetchTags failed: %w", err) 100 | } 101 | if next == 0 { 102 | break 103 | } 104 | versions = append(versions, tags...) 105 | page = next 106 | } 107 | 108 | versions = sortVersions(versions) 109 | printVersions(versions) 110 | 111 | return nil 112 | } 113 | 114 | func fetchNextTags(c context.Context, client *github.Client, nextPage int) ([]goversion, int, error) { 115 | tags, resp, err := client.Repositories.ListTags(c, "golang", "go", &github.ListOptions{ 116 | Page: nextPage, 117 | PerPage: perPage, 118 | }) 119 | if err != nil { 120 | return nil, 0, fmt.Errorf("page %d fetch failed: %w", nextPage, err) 121 | } 122 | 123 | var result []goversion 124 | 125 | for _, t := range tags { 126 | if strings.HasPrefix(t.GetName(), "go1") { 127 | result = append(result, goversion(t.GetName())) 128 | } 129 | } 130 | 131 | return result, resp.NextPage, nil 132 | } 133 | 134 | const goDLURL = "https://golang.org/dl/" 135 | 136 | func LSRemote(c context.Context) error { 137 | req, err := http.NewRequestWithContext(c, "GET", goDLURL, nil) 138 | if err != nil { 139 | return fmt.Errorf("create request failed: %w", err) 140 | } 141 | 142 | res, err := http.DefaultClient.Do(req) 143 | if err != nil { 144 | return fmt.Errorf("http request failed: %w", err) 145 | } 146 | defer res.Body.Close() 147 | 148 | doc, err := html.Parse(res.Body) 149 | if err != nil { 150 | return fmt.Errorf("html parse failed: %w", err) 151 | } 152 | 153 | var f func(*html.Node) 154 | 155 | var versions []goversion 156 | 157 | f = func(n *html.Node) { 158 | if n.Type == html.ElementNode && n.Data == "h3" { 159 | maybeVersion := n.FirstChild.Data 160 | 161 | if strings.HasPrefix(maybeVersion, "go1") { 162 | vs := strings.SplitN(maybeVersion, " ", 2) 163 | versions = append(versions, goversion(vs[0])) 164 | } 165 | } 166 | for ch := n.FirstChild; ch != nil; ch = ch.NextSibling { 167 | f(ch) 168 | } 169 | } 170 | 171 | f(doc) 172 | 173 | if len(versions) == 0 { 174 | panic("Maybe the DOM structure has changed. Would you please file an issue https://github.com/akito0107/goswitch/issues/new") 175 | } 176 | 177 | versions = sortVersions(versions) 178 | printVersions(versions) 179 | 180 | return nil 181 | } 182 | 183 | func sortVersions(versions []goversion) []goversion { 184 | sort.Slice(versions, func(i, j int) bool { 185 | if versions[i].Minor() != versions[j].Minor() { 186 | return versions[i].Minor() < versions[j].Minor() 187 | } 188 | if versions[i].Patch() != versions[j].Patch() { 189 | return versions[i].Patch() < versions[j].Patch() 190 | } 191 | if versions[i].BetaVersion() != versions[j].BetaVersion() { 192 | return versions[i].BetaVersion() < versions[j].BetaVersion() 193 | } 194 | return versions[i].RCVersion() < versions[j].RCVersion() 195 | }) 196 | 197 | return versions 198 | } 199 | 200 | func printVersions(versions []goversion) { 201 | m := make(map[goversion]struct{}) 202 | 203 | var vs []goversion 204 | for _, v := range versions { 205 | _, ok := m[v] 206 | if ok { 207 | continue 208 | } 209 | m[v] = struct{}{} 210 | vs = append(vs, v) 211 | } 212 | 213 | currentMinor := vs[0].Minor() 214 | 215 | fmt.Println() 216 | fmt.Println("available versions:") 217 | fmt.Println() 218 | fmt.Println("system") 219 | 220 | var buf bytes.Buffer 221 | for i, v := range vs { 222 | if currentMinor != v.Minor() { 223 | fmt.Fprintln(&buf) 224 | currentMinor = v.Minor() 225 | } else if i != 0 { 226 | fmt.Fprintf(&buf, ", ") 227 | } 228 | 229 | fmt.Fprintf(&buf, "%s", v) 230 | } 231 | 232 | fmt.Println(buf.String()) 233 | } 234 | -------------------------------------------------------------------------------- /v2/internal/lsremote_test.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "reflect" 5 | "testing" 6 | ) 7 | 8 | func Test_sortVersions(t *testing.T) { 9 | 10 | cases := []struct { 11 | name string 12 | in []goversion 13 | expect []goversion 14 | }{ 15 | { 16 | name: "minor version", 17 | in: []goversion{"1.13", "1.11", "1.12"}, 18 | expect: []goversion{"1.11", "1.12", "1.13"}, 19 | }, 20 | { 21 | name: "patch version", 22 | in: []goversion{"1.13.1", "1.13.3", "1.13.2"}, 23 | expect: []goversion{"1.13.1", "1.13.2", "1.13.3"}, 24 | }, 25 | { 26 | name: "with rc", 27 | in: []goversion{"1.13rc1", "1.13.1", "1.13"}, 28 | expect: []goversion{"1.13rc1", "1.13", "1.13.1"}, 29 | }, 30 | { 31 | name: "with beta", 32 | in: []goversion{"1.13beta1", "1.13.1", "1.13"}, 33 | expect: []goversion{"1.13beta1", "1.13", "1.13.1"}, 34 | }, 35 | { 36 | name: "complex case", 37 | in: []goversion{"1.13rc1", "1.13beta1", "1.13.1", "1.13", "1.1", "1.14rc1", "1.13rc2"}, 38 | expect: []goversion{"1.1", "1.13beta1", "1.13rc1", "1.13rc2", "1.13", "1.13.1", "1.14rc1"}, 39 | }, 40 | { 41 | name: "patch with rc", 42 | in: []goversion{"go1.9", "go1.9.2", "go1.9.2rc2"}, 43 | expect: []goversion{"go1.9", "go1.9.2rc2", "go1.9.2"}, 44 | }, 45 | } 46 | 47 | for _, c := range cases { 48 | t.Run(c.name, func(t *testing.T) { 49 | act := sortVersions(c.in) 50 | if !reflect.DeepEqual(act, c.expect) { 51 | t.Errorf("must be sorted as %v but %v", c.expect, act) 52 | } 53 | }) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /v2/internal/use.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "bytes" 5 | "context" 6 | "fmt" 7 | "go/build" 8 | "log" 9 | "os" 10 | "os/exec" 11 | "path/filepath" 12 | "strings" 13 | ) 14 | 15 | const goGetURL = "golang.org/dl/" 16 | 17 | func Use(ctx context.Context, version string) error { 18 | 19 | gobin := gobinPath() 20 | symlinkPath := filepath.Join(gobin, "go") 21 | 22 | if version == "system" { 23 | log.Println("using system go") 24 | log.Println("removing symlink...") 25 | return rmSymlink(symlinkPath) 26 | } 27 | 28 | if !versionExists(version) { 29 | v, err := currentGoVersion(ctx) 30 | if err != nil { 31 | return fmt.Errorf("get current go version: %w", err) 32 | } 33 | var cmd *exec.Cmd 34 | if v.Major() > 1 || (v.Major() == 1 && v.Minor() >= 17) { 35 | cmd = exec.CommandContext(ctx, "go", "install", goGetURL+version+"@latest") 36 | } else { 37 | cmd = exec.CommandContext(ctx, "go", "get", goGetURL+version) 38 | cmd.Env = append(os.Environ(), "GO111MODULE=off") 39 | } 40 | sub := cmd.Args[1] 41 | log.Printf("start go %s...", sub) 42 | log.Print(strings.Join(cmd.Args, " ")) 43 | 44 | cmd.Stdout = log.Writer() 45 | cmd.Stderr = log.Writer() 46 | 47 | if err := cmd.Run(); err != nil { 48 | return fmt.Errorf("go %s failed: %w", sub, err) 49 | } 50 | log.Printf("go %s finished. start download", sub) 51 | } else { 52 | log.Printf("already exists version: %s, start download\n", version) 53 | } 54 | 55 | dlcmd := exec.CommandContext(ctx, version, "download") 56 | 57 | dlcmd.Stdout = log.Writer() 58 | dlcmd.Stderr = log.Writer() 59 | 60 | if err := dlcmd.Run(); err != nil { 61 | return fmt.Errorf("%s download failed: %w", version, err) 62 | } 63 | 64 | log.Println("download finished.") 65 | log.Println("switch go version") 66 | 67 | if err := rmSymlink(symlinkPath); err != nil { 68 | return err 69 | } 70 | 71 | if err := os.Symlink(filepath.Join(gobin, version), symlinkPath); err != nil { 72 | return fmt.Errorf("create symlink failed: %w", err) 73 | } 74 | 75 | return nil 76 | } 77 | 78 | func rmSymlink(symlinkPath string) error { 79 | if info, err := os.Lstat(symlinkPath); err == nil { 80 | if info.Mode()&os.ModeSymlink != os.ModeSymlink { 81 | return fmt.Errorf("go command is not a symlink but %s, goswitch use symlink. please delete or make a symlink", info.Mode()) 82 | } 83 | 84 | if err := os.Remove(symlinkPath); err != nil { 85 | return fmt.Errorf("remove go link failed: %w", err) 86 | } 87 | } 88 | 89 | return nil 90 | } 91 | 92 | func gobinPath() string { 93 | gobin := os.Getenv("GOBIN") 94 | 95 | if gobin == "" { 96 | gobin = filepath.Join(build.Default.GOPATH, "bin") 97 | } 98 | 99 | return gobin 100 | } 101 | 102 | func versionExists(version string) bool { 103 | gobin := gobinPath() 104 | _, err := os.Stat(filepath.Join(gobin, version)) 105 | 106 | return err == nil 107 | } 108 | 109 | func currentGoVersion(ctx context.Context) (goversion, error) { 110 | b, err := exec.CommandContext(ctx, "go", "env", "GOVERSION").Output() 111 | if err != nil { 112 | return "", fmt.Errorf("exec go env GOVERSION: %w", err) 113 | } 114 | return goversion(bytes.TrimSuffix(b, []byte("\n"))), nil 115 | } 116 | -------------------------------------------------------------------------------- /v2/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/urfave/cli/v2" 8 | 9 | "github.com/akito0107/goswitch/v2/internal" 10 | ) 11 | 12 | func main() { 13 | app := &cli.App{ 14 | Name: "goswitch", 15 | Usage: "go version switching utility", 16 | Commands: []*cli.Command{ 17 | { 18 | Name: "use", 19 | Usage: "switch current go version", 20 | Action: func(c *cli.Context) error { 21 | v := c.Args().Get(0) 22 | return internal.Use(c.Context, v) 23 | }, 24 | }, 25 | { 26 | Name: "ls-remote", 27 | Usage: "show all available versions", 28 | Flags: []cli.Flag{ 29 | &cli.BoolFlag{ 30 | Name: "use-github", 31 | Aliases: []string{"g"}, 32 | Usage: "use github tags", 33 | Required: false, 34 | }, 35 | }, 36 | Action: func(c *cli.Context) error { 37 | if c.Bool("use-github") { 38 | log.Println("Using github releases is now enabled by default, so this option no longer has any effect.") 39 | } 40 | return internal.LSRemoteGH(c.Context) 41 | }, 42 | }, 43 | }, 44 | } 45 | 46 | if err := app.Run(os.Args); err != nil { 47 | log.Fatal(err) 48 | } 49 | } 50 | --------------------------------------------------------------------------------