├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── cli ├── sdk.go └── sdk_test.go ├── cmd ├── check.go ├── pull.go ├── pull_test.go ├── version.go └── version_test.go ├── env ├── sdkman.go ├── sdkman_test.go ├── vardir.go └── vardir_test.go ├── features ├── pull.feature └── version.feature ├── go.mod ├── go.sum ├── godog_test.go ├── sdk.go ├── steps ├── cli_steps.go ├── env_steps.go └── version_steps.go └── txt └── colours.go /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/go,intellij+all 3 | 4 | ### Go ### 5 | # Binaries for programs and plugins 6 | *.exe 7 | *.exe~ 8 | *.dll 9 | *.so 10 | *.dylib 11 | 12 | # Test binary, build with `go test -c` 13 | *.test 14 | 15 | # Output of the go coverage tool, specifically when used with LiteIDE 16 | *.out 17 | 18 | ### Go Patch ### 19 | /vendor/ 20 | /Godeps/ 21 | 22 | ### Intellij+all ### 23 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 24 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 25 | 26 | # User-specific stuff 27 | .idea/**/workspace.xml 28 | .idea/**/tasks.xml 29 | .idea/**/usage.statistics.xml 30 | .idea/**/dictionaries 31 | .idea/**/shelf 32 | 33 | # Generated files 34 | .idea/**/contentModel.xml 35 | 36 | # Sensitive or high-churn files 37 | .idea/**/dataSources/ 38 | .idea/**/dataSources.ids 39 | .idea/**/dataSources.local.xml 40 | .idea/**/sqlDataSources.xml 41 | .idea/**/dynamic.xml 42 | .idea/**/uiDesigner.xml 43 | .idea/**/dbnavigator.xml 44 | 45 | # Gradle 46 | .idea/**/gradle.xml 47 | .idea/**/libraries 48 | 49 | # Gradle and Maven with auto-import 50 | # When using Gradle or Maven with auto-import, you should exclude module files, 51 | # since they will be recreated, and may cause churn. Uncomment if using 52 | # auto-import. 53 | # .idea/modules.xml 54 | # .idea/*.iml 55 | # .idea/modules 56 | 57 | # CMake 58 | cmake-build-*/ 59 | 60 | # Mongo Explorer plugin 61 | .idea/**/mongoSettings.xml 62 | 63 | # File-based project format 64 | *.iws 65 | 66 | # IntelliJ 67 | out/ 68 | 69 | # mpeltonen/sbt-idea plugin 70 | .idea_modules/ 71 | 72 | # JIRA plugin 73 | atlassian-ide-plugin.xml 74 | 75 | # Cursive Clojure plugin 76 | .idea/replstate.xml 77 | 78 | # Crashlytics plugin (for Android Studio and IntelliJ) 79 | com_crashlytics_export_strings.xml 80 | crashlytics.properties 81 | crashlytics-build.properties 82 | fabric.properties 83 | 84 | # Editor-based Rest Client 85 | .idea/httpRequests 86 | 87 | ### Intellij+all Patch ### 88 | # Ignores the whole .idea folder and all .iml files 89 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 90 | 91 | .idea/ 92 | 93 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 94 | 95 | *.iml 96 | modules.xml 97 | .idea/misc.xml 98 | *.ipr 99 | 100 | sdk 101 | 102 | # End of https://www.gitignore.io/api/go,intellij+all 103 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | go: "1.11" 4 | os: "linux" 5 | dist: trusty 6 | sudo: false 7 | install: true 8 | 9 | script: 10 | - env GO111MODULE=on go test --godog.tags=~wip 11 | - env GO111MODULE=on go test -v sdk/cli 12 | - env GO111MODULE=on go test -v sdk/cmd 13 | - env GO111MODULE=on go test -v sdk/env 14 | - env GO111MODULE=on go build 15 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing 2 | 3 | We greatly value feedback and contributions. We have a simple process in place to facilitate this: 4 | 5 | ### Discussion 6 | 7 | We try to minimise Github Issues and Pull Requests by first having a conversation in the following [Gitter Room](https://gitter.im/sdkman/golang-cli). 8 | 9 | ### Templates 10 | 11 | After accepting a new feature or confirming that a bug was found, an Issue or PR may be opened. Please ensure that the appropriate template is read and completed prior to any new submissions. 12 | 13 | **Unfortunately we will close any Github Issues or PRs that have not followed this process of prior discussion.** -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Please read this prior to deleting!** 2 | 3 | We require a conversation to take place in the Golang CLI Development [Gitter Room](https://gitter.im/sdkman/golang-cli) before raising a new Github Issue. 4 | 5 | Please note that **issues will be closed immediately if prior discussion did not take place**. 6 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Please do not delete anything below this line!** 2 | 3 | To raise a new Pull Request, the following prerequisites need to be met. Please mark with `[x]` before proceeding: 4 | 5 | - [ ] conversation was held in the Golang CLI Development [Gitter Room](https://gitter.im/sdkman/golang-cli). 6 | - [ ] test coverage was added (Cucumber or go test as appropriate). -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SDKMAN! Golang CLI 2 | 3 | [![Build Status](https://travis-ci.org/sdkman/sdk.svg?branch=master)](https://travis-ci.org/sdkman/sdk) 4 | 5 | **This project has been downgraded to a mere spike, the learnings of which will lead to the next phase of exploration of the CLI. We are also looking at Rust as an alternative implementation solution.** 6 | 7 | The current bash implementation is still completely fit for purpose and remains in full production use. 8 | 9 | ### Living Documentation 10 | 11 | SDKMAN's development is always driven by tests. More than that, we use Cucumber to describe the behaviour of the CLI in plain English. We do so using Cucumber features, all of which can be found under the [_features/_](features) folder of this repo. These Features form a body of Living Documentation that evolves with the software implementation. 12 | 13 | The Cucumber Features are backed by Step Definitions, snippets of matched code that are invoked in order as the Cucumber Feature are 14 | 15 | 16 | ### Development 17 | 18 | #### Prerequisites 19 | 20 | Ensure that [Go is installed](https://golang.org/doc/install) on your system. 21 | 22 | Optionally, install Godog for running the Cucumber specifications directly: 23 | 24 | $ go get github.com/DATA-DOG/godog/cmd/godog 25 | 26 | #### Running the tests 27 | 28 | To run all tests using Go's builtin test support (unit and cukes): 29 | 30 | $ cd path/to/the/repo/sdk 31 | $ go test --godog.format=pretty 32 | 33 | If Godog was installed earlier, run the Cucumber specs directly in isolation with the following command: 34 | 35 | $ godog 36 | 37 | ### Run 38 | 39 | To kick the tyres before building: 40 | 41 | $ go run sdk.go version 42 | 43 | ### Build 44 | 45 | To build and run the binary executable: 46 | 47 | $ go build 48 | $ ./sdk 49 | -------------------------------------------------------------------------------- /cli/sdk.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "sdk/cmd" 7 | "sdk/env" 8 | "sdk/txt" 9 | ) 10 | 11 | // Sdk is the entry point used for delegating to the individual commands 12 | // as well as performing and propagating error handling. 13 | func Sdk(args []string, sdkmanDir string, sdkmanApi string) (int, error) { 14 | 15 | if len(args) == 0 { 16 | return 1, errors.New(txt.Error("No command specified.")) 17 | 18 | } else { 19 | var command = args[0] 20 | var output string 21 | var err error 22 | 23 | switch command { 24 | case "version": 25 | version := env.GetVersion(sdkmanDir) 26 | output = cmd.Version(version) 27 | case "pull": 28 | fmt.Print(txt.Info("Pulling available version... ")) 29 | output, err = cmd.Pull(sdkmanApi) 30 | env.SetRemoteVersion("localhost", output, sdkmanDir) 31 | output = txt.InfoF("SDKMAN %s now available for installation...", output) 32 | default: 33 | return 1, errors.New(txt.ErrorF("No such command: %s", command)) 34 | } 35 | 36 | fmt.Println(output) 37 | return 0, err 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /cli/sdk_test.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "github.com/stretchr/testify/assert" 5 | "testing" 6 | ) 7 | 8 | func TestSdkWithNoArgs(t *testing.T) { 9 | noArgs := []string{} 10 | code, err := Sdk(noArgs, "", "") 11 | assert.Equal(t, 1, code, "Expected return code 1") 12 | assert.Error(t, err, "Expected error for no args invocation of Sdk()") 13 | } 14 | 15 | func TestSdkWithInvalidArgs(t *testing.T) { 16 | invalidArg := []string{"invalid"} 17 | code, err := Sdk(invalidArg, "", "") 18 | assert.Equal(t, 1, code, "Expected return code 1") 19 | assert.Error(t, err, "Expected error for invalid command invocation of Sdk()") 20 | } 21 | -------------------------------------------------------------------------------- /cmd/check.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | func check(e error) { 4 | if e != nil { 5 | panic(e) 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /cmd/pull.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "encoding/json" 5 | "io/ioutil" 6 | "net/http" 7 | "time" 8 | ) 9 | 10 | type Remote struct { 11 | Version string 12 | } 13 | 14 | // Pull fetches a remote version from a host resource 15 | func Pull(host string) (string, error) { 16 | timeout := time.Duration(2 * time.Second) 17 | client := http.Client{ 18 | Timeout: timeout, 19 | } 20 | 21 | resp, err := client.Get(host) 22 | if err != nil { 23 | return "", err 24 | } 25 | 26 | bytes, err := ioutil.ReadAll(resp.Body) 27 | if err != nil { 28 | return "", err 29 | } 30 | 31 | var response Remote 32 | err = json.Unmarshal(bytes, &response) 33 | if err != nil { 34 | return "", err 35 | } 36 | 37 | return response.Version, nil 38 | } 39 | -------------------------------------------------------------------------------- /cmd/pull_test.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | "github.com/stretchr/testify/assert" 6 | "net/http" 7 | "net/http/httptest" 8 | "testing" 9 | "time" 10 | ) 11 | 12 | func TestPull(t *testing.T) { 13 | expected := "1.0.0" 14 | 15 | var response = fmt.Sprintf(`{"version": "%s"}`, expected) 16 | var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 17 | w.Write([]byte(response)) 18 | w.WriteHeader(http.StatusOK) 19 | })) 20 | defer apiStub.Close() 21 | 22 | actual, err := Pull(apiStub.URL) 23 | check(err) 24 | 25 | assert.Equal(t, expected, actual) 26 | } 27 | 28 | func TestPullMalformedJson(t *testing.T) { 29 | var response = "invalid json" 30 | var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 31 | w.Write([]byte(response)) 32 | w.WriteHeader(http.StatusOK) 33 | })) 34 | defer apiStub.Close() 35 | 36 | _, err := Pull(apiStub.URL) 37 | 38 | assert.Error(t, err) 39 | } 40 | 41 | func TestPullHttpGetConnectionRefused(t *testing.T) { 42 | actual, err := Pull("http://localhost:0") 43 | 44 | assert.Equal(t, "", actual) 45 | assert.Error(t, err, "Connection refused") 46 | } 47 | 48 | func TestPullHttpGetReadTimeout(t *testing.T) { 49 | var response = fmt.Sprint(`{"version": "too late"}`) 50 | var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 51 | time.Sleep(3 * time.Second) 52 | w.Write([]byte(response)) 53 | w.WriteHeader(http.StatusOK) 54 | })) 55 | defer apiStub.Close() 56 | 57 | actual, err := Pull(apiStub.URL) 58 | 59 | assert.Equal(t, "", actual) 60 | assert.Error(t, err) 61 | } 62 | 63 | func TestPullHttpGetNonOkStatus(t *testing.T) { 64 | var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 65 | w.WriteHeader(http.StatusInternalServerError) 66 | })) 67 | defer apiStub.Close() 68 | 69 | actual, err := Pull(apiStub.URL) 70 | 71 | assert.Equal(t, "", actual) 72 | assert.Error(t, err) 73 | } 74 | -------------------------------------------------------------------------------- /cmd/version.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "sdk/txt" 5 | ) 6 | 7 | // Version formats the version output message 8 | func Version(version string) string { 9 | return txt.InfoF("SDKMAN %s", version) 10 | } 11 | -------------------------------------------------------------------------------- /cmd/version_test.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "github.com/stretchr/testify/assert" 5 | "testing" 6 | ) 7 | 8 | func TestVersionOut(t *testing.T) { 9 | output := Version("1.0.0") 10 | assert.Equal(t, "SDKMAN 1.0.0", output) 11 | } 12 | -------------------------------------------------------------------------------- /env/sdkman.go: -------------------------------------------------------------------------------- 1 | package env 2 | 3 | import ( 4 | "os" 5 | "path/filepath" 6 | 7 | "github.com/mitchellh/go-homedir" 8 | ) 9 | 10 | var sdkmanServiceEnv = "SDKMAN_API" 11 | 12 | var sdkmanDirEnv = "SDKMAN_DIR" 13 | 14 | var baseFolder = ".sdkman" 15 | 16 | // SdkmanDir infers the sdkman directory based on the presence of the SDKMAN_DIR 17 | // environment variable. If not present, the `$HOME/.sdkman` is used. 18 | func SdkmanDir() string { 19 | sdkmanDir := os.Getenv(sdkmanDirEnv) 20 | if 0 < len(sdkmanDir) { 21 | return sdkmanDir 22 | } 23 | d, err := homedir.Dir() 24 | check(err) 25 | 26 | return filepath.Join(d, baseFolder) 27 | } 28 | 29 | // SdkmanApi infers the sdkman service host based on the presence of the SDKMAN_API 30 | // environment variable. If not present, the https://api.sdkman.io/3 service is used. 31 | func SdkmanApi() string { 32 | sdkmanApi := os.Getenv(sdkmanServiceEnv) 33 | if 0 < len(sdkmanApi) { 34 | return sdkmanApi 35 | } 36 | 37 | return "https://api.sdkman.io/3" 38 | } 39 | -------------------------------------------------------------------------------- /env/sdkman_test.go: -------------------------------------------------------------------------------- 1 | package env 2 | 3 | import ( 4 | "github.com/mitchellh/go-homedir" 5 | "github.com/stretchr/testify/assert" 6 | "io/ioutil" 7 | "os" 8 | "path/filepath" 9 | "testing" 10 | ) 11 | 12 | func TestSdkmanDirNoEnvVar(t *testing.T) { 13 | os.Unsetenv(sdkmanDirEnv) 14 | 15 | actual := SdkmanDir() 16 | 17 | homeDir, err := homedir.Dir() 18 | check(err) 19 | expected := filepath.Join(homeDir, baseFolder) 20 | 21 | assert.Equal(t, expected, actual, "no sdkman dir default value when SDKMAN_DIR env var unset") 22 | } 23 | 24 | func TestSdkmanDirEnvVarSet(t *testing.T) { 25 | sdkmanDir, err := ioutil.TempDir("/tmp", "sdkman-") 26 | check(err) 27 | 28 | os.Setenv(sdkmanDirEnv, sdkmanDir) 29 | 30 | actual := SdkmanDir() 31 | 32 | expected := sdkmanDir 33 | 34 | assert.Equal(t, expected, actual, "sdkman dir value not read from SDKMAN_DIR env var") 35 | } 36 | 37 | func TestSdkmanApiNoEnvVar(t *testing.T) { 38 | os.Unsetenv(sdkmanServiceEnv) 39 | 40 | expected := "https://api.sdkman.io/3" 41 | 42 | actual := SdkmanApi() 43 | 44 | assert.Equal(t, expected, actual, "no sdkman api default value when SDKMAN_API env var unset") 45 | } 46 | 47 | func TestSdkmanApiEnvVarSet(t *testing.T) { 48 | expected := "https://api.sdkman.io/2" 49 | os.Setenv(sdkmanServiceEnv, expected) 50 | 51 | actual := SdkmanApi() 52 | 53 | assert.Equal(t, expected, actual) 54 | } 55 | -------------------------------------------------------------------------------- /env/vardir.go: -------------------------------------------------------------------------------- 1 | package env 2 | 3 | import ( 4 | "io/ioutil" 5 | "os" 6 | "path/filepath" 7 | ) 8 | 9 | // SetVersion writes the sdkman version 10 | func SetVersion(version string, sdkmanDir string) { 11 | versionFile := filepath.Join(sdkmanDir, "var", "version") 12 | err := ioutil.WriteFile(versionFile, []byte(version), os.ModePerm) 13 | check(err) 14 | } 15 | 16 | // GetVersion reads the sdkman version 17 | func GetVersion(sdkmanDir string) string { 18 | versionFile := filepath.Join(sdkmanDir, "var", "version") 19 | version, err := ioutil.ReadFile(versionFile) 20 | check(err) 21 | return string(version) 22 | } 23 | 24 | //SetRemoteVersion writes the sdkman remote version for a given domain 25 | func SetRemoteVersion(domain string, version string, sdkmanDir string) { 26 | domainDir := filepath.Join(sdkmanDir, "var", domain) 27 | err := os.MkdirAll(domainDir, os.ModePerm) 28 | check(err) 29 | 30 | versionFile := filepath.Join(domainDir, "version") 31 | err = ioutil.WriteFile(versionFile, []byte(version), os.ModePerm) 32 | check(err) 33 | } 34 | 35 | func check(err error) { 36 | if err != nil { 37 | panic(err) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /env/vardir_test.go: -------------------------------------------------------------------------------- 1 | package env 2 | 3 | import ( 4 | "github.com/stretchr/testify/assert" 5 | "io/ioutil" 6 | "os" 7 | "path/filepath" 8 | "testing" 9 | ) 10 | 11 | func TestSetVersion(t *testing.T) { 12 | sdkmanDir, err := ioutil.TempDir("/tmp", "sdkman-") 13 | check(err) 14 | defer os.RemoveAll(sdkmanDir) 15 | 16 | err = os.Mkdir(sdkmanDir+"/var", 0755) 17 | check(err) 18 | 19 | expected := "1.0.0" 20 | SetVersion(expected, sdkmanDir) 21 | 22 | versionFile := filepath.Join(sdkmanDir, "var", "version") 23 | content, err := ioutil.ReadFile(versionFile) 24 | check(err) 25 | 26 | actual := string(content) 27 | 28 | assert.Equal(t, expected, actual) 29 | } 30 | 31 | func TestGetVersion(t *testing.T) { 32 | 33 | sdkmanDir, err := ioutil.TempDir("/tmp", "sdkman-") 34 | check(err) 35 | defer os.RemoveAll(sdkmanDir) 36 | 37 | err = os.Mkdir(sdkmanDir+"/var", 0755) 38 | check(err) 39 | 40 | expected := "1.0.0" 41 | versionFile := filepath.Join(sdkmanDir, "var", "version") 42 | err = ioutil.WriteFile(versionFile, []byte(expected), os.ModePerm) 43 | check(err) 44 | 45 | actual := GetVersion(sdkmanDir) 46 | assert.Equal(t, expected, actual) 47 | } 48 | 49 | func TestSetRemoteVersion(t *testing.T) { 50 | domain := "localhost" 51 | expected := "1.0.0" 52 | 53 | sdkmanDir, err := ioutil.TempDir("/tmp", "sdkman-") 54 | check(err) 55 | defer os.RemoveAll(sdkmanDir) 56 | 57 | SetRemoteVersion(domain, expected, sdkmanDir) 58 | 59 | file := filepath.Join(sdkmanDir, "var", domain, "version") 60 | content, err := ioutil.ReadFile(file) 61 | check(err) 62 | 63 | actual := string(content) 64 | 65 | assert.Equal(t, expected, actual) 66 | } 67 | -------------------------------------------------------------------------------- /features/pull.feature: -------------------------------------------------------------------------------- 1 | Feature: Pull 2 | 3 | As a Software Developer 4 | I want SDKMAN to be up-to-date 5 | So that I can access the latest SDKs 6 | 7 | https://github.com/sdkman/sdk/issues/4 8 | 9 | Versions of SDKMAN prior to 6.0.0 were very reliant on being connected to the internet. For most this has not 10 | been a problem, but for those with limited bandwidth or unreliable internet connectivity this has rendered the 11 | CLI unusable. 12 | 13 | One of the steps that was taken previously was to introduce an offline mode, allowing the CLI to continue working 14 | even when no internet connection was present. The intent was to cause the CLI to degrade gracefully when: 15 | a) no internet connectivity was available 16 | b) when the `sdk offline enable` command was issued 17 | 18 | Despite this being a noble effort, the implementation of this was troublesome due to the limitations of bash 19 | and curl. To date this functionality still has serious issues. 20 | 21 | This leads us to re-evaluating our options given a blank canvas. Looking at tools like Debian's Apt and DVCS systems 22 | like Git, they all have functions that explicitly pull down remote state. In the case of Apt we have an `apt update`, 23 | in the case of Git we have `git pull`. 24 | 25 | The proposal is to follow this line of thinking, preventing continual API access on every command. Instead, an 26 | explicit command such as `sdk pull` or `sdk update` should be issued in order to fetch remote state. If the sdk 27 | command has not been updated in while, it should warn the user that their local state is out of date. 28 | 29 | Scenario: The current available Version is pulled 30 | Given the internet is reachable 31 | And an initialised environment 32 | And the installed sdkman version is "5.7.2" 33 | And the available sdkman version is "6.0.0" 34 | When I enter "sdk pull version" 35 | Then the exit code is 0 36 | And the pulled version state is "6.0.0" 37 | And I see "Pulling available version..." 38 | And I see "SDKMAN 6.0.0 now available for installation..." 39 | -------------------------------------------------------------------------------- /features/version.feature: -------------------------------------------------------------------------------- 1 | Feature: Version 2 | 3 | As a software developer 4 | I need to know the sdkman version 5 | So that I can stay up to date 6 | 7 | Scenario: Show the current version of sdkman 8 | Given the internet is reachable 9 | And an initialised environment 10 | And the installed sdkman version is "6.0.0" 11 | When I enter "sdk version" 12 | Then I see "SDKMAN 6.0.0" 13 | And the exit code is 0 14 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module sdk 2 | 3 | require ( 4 | github.com/DATA-DOG/godog v0.7.7-0.20180903180505-c65ea2b545e1 5 | github.com/davecgh/go-spew v1.1.1 // indirect 6 | github.com/fatih/color v1.7.0 7 | github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d 8 | github.com/mattn/go-colorable v0.0.9 // indirect 9 | github.com/mattn/go-isatty v0.0.4 // indirect 10 | github.com/mitchellh/go-homedir v1.0.0 11 | github.com/pmezard/go-difflib v1.0.0 // indirect 12 | github.com/stretchr/testify v1.2.2 13 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e // indirect 14 | ) 15 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/DATA-DOG/godog v0.7.7-0.20180903180505-c65ea2b545e1 h1:VC7wkcWmmGATZbKCS65YU+8go0vlDonmdimO+DEVyiE= 2 | github.com/DATA-DOG/godog v0.7.7-0.20180903180505-c65ea2b545e1/go.mod h1:z2OZ6a3X0/YAKVqLfVzYBwFt3j6uSt3Xrqa7XTtcQE0= 3 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 4 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= 6 | github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= 7 | github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d h1:cVtBfNW5XTHiKQe7jDaDBSh/EVM4XLPutLAGboIXuM0= 8 | github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0= 9 | github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= 10 | github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= 11 | github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= 12 | github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= 13 | github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0= 14 | github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 15 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 16 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 17 | github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 18 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 19 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= 20 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 21 | -------------------------------------------------------------------------------- /godog_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "github.com/DATA-DOG/godog" 6 | "github.com/DATA-DOG/godog/colors" 7 | "github.com/fatih/color" 8 | "os" 9 | "sdk/steps" 10 | "testing" 11 | ) 12 | 13 | var opt = godog.Options{Output: colors.Colored(os.Stdout)} 14 | 15 | func init() { 16 | godog.BindFlags("godog.", flag.CommandLine, &opt) 17 | } 18 | 19 | // integrate godog with `go test` 20 | func TestMain(m *testing.M) { 21 | 22 | color.NoColor = true 23 | 24 | flag.Parse() 25 | opt.Paths = flag.Args() 26 | 27 | status := godog.RunWithOptions("godogs", func(s *godog.Suite) { 28 | steps.CliContext(s) 29 | steps.EnvContext(s) 30 | steps.VersionFeatureContext(s) 31 | }, opt) 32 | 33 | if st := m.Run(); st > status { 34 | status = st 35 | } 36 | os.Exit(status) 37 | } 38 | -------------------------------------------------------------------------------- /sdk.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "sdk/cli" 7 | "sdk/env" 8 | ) 9 | 10 | func main() { 11 | 12 | sdkmanDir := env.SdkmanDir() 13 | 14 | sdkmanApi := env.SdkmanApi() 15 | 16 | exit, err := cli.Sdk(os.Args[1:], sdkmanDir, sdkmanApi) 17 | if err != nil { 18 | fmt.Println(err) 19 | os.Exit(1) 20 | } 21 | os.Exit(exit) 22 | } 23 | -------------------------------------------------------------------------------- /steps/cli_steps.go: -------------------------------------------------------------------------------- 1 | package steps 2 | 3 | import ( 4 | "fmt" 5 | "github.com/DATA-DOG/godog" 6 | "github.com/kami-zh/go-capturer" 7 | "sdk/cli" 8 | "strings" 9 | ) 10 | 11 | var stdout string 12 | var exitCode int 13 | 14 | func iEnter(command string) error { 15 | commandLine := strings.Split(command, " ") 16 | args := commandLine[1:] 17 | 18 | stdout = strings.TrimSuffix(capturer.CaptureStdout(func() { 19 | exitCode, _ = cli.Sdk(args, sdkmanDir, sdkmanApi) 20 | }), "\n") 21 | 22 | return nil 23 | } 24 | 25 | func iSee(expected string) error { 26 | 27 | if !strings.Contains(stdout, expected) { 28 | return fmt.Errorf("stdandard out '%s' did not contain '%s'", stdout, expected) 29 | } 30 | return nil 31 | } 32 | 33 | func theExitCodeIs(expected int) error { 34 | 35 | if exitCode != expected { 36 | return fmt.Errorf("expected %d but was %d", expected, exitCode) 37 | } 38 | return nil 39 | } 40 | 41 | func CliContext(s *godog.Suite) { 42 | s.Step(`^I enter "(.*)"$`, iEnter) 43 | s.Step(`^I see "(.*)"$`, iSee) 44 | s.Step(`^the exit code is (\d+)$`, theExitCodeIs) 45 | } 46 | -------------------------------------------------------------------------------- /steps/env_steps.go: -------------------------------------------------------------------------------- 1 | package steps 2 | 3 | import ( 4 | "github.com/DATA-DOG/godog" 5 | "io/ioutil" 6 | "os" 7 | "path/filepath" 8 | "sdk/env" 9 | ) 10 | 11 | var tmpFolder = "/tmp" 12 | var prefix = "sdkman-" 13 | 14 | var sdkmanApi string 15 | var sdkmanDir string 16 | var varDir string 17 | 18 | func theInternetIsReachable() error { 19 | //Internet availability not relevant yet.. 20 | return nil 21 | } 22 | 23 | func anInitialisedEnvironment() error { 24 | 25 | var err error 26 | sdkmanDir, err = ioutil.TempDir(tmpFolder, prefix) 27 | check(err) 28 | 29 | varDir = filepath.Join(sdkmanDir, "var") 30 | err = os.Mkdir(varDir, 0755) 31 | check(err) 32 | 33 | env.SetVersion("0.0.1", sdkmanDir) 34 | 35 | return nil 36 | } 37 | 38 | func EnvContext(s *godog.Suite) { 39 | s.Step(`^the internet is reachable$`, theInternetIsReachable) 40 | s.Step(`^an initialised environment$`, anInitialisedEnvironment) 41 | 42 | //clean up old temp dir(s) before starting new test 43 | s.BeforeScenario(func(interface{}) { 44 | dirs, err := filepath.Glob(tmpFolder + "/" + prefix + "*") 45 | check(err) 46 | for _, d := range dirs { 47 | if err := os.RemoveAll(d); err != nil { 48 | panic(err) 49 | } 50 | } 51 | }) 52 | } 53 | 54 | func check(e error) { 55 | if e != nil { 56 | panic(e) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /steps/version_steps.go: -------------------------------------------------------------------------------- 1 | package steps 2 | 3 | import ( 4 | "fmt" 5 | "github.com/DATA-DOG/godog" 6 | "io/ioutil" 7 | "net/http" 8 | "net/http/httptest" 9 | "path/filepath" 10 | "sdk/env" 11 | ) 12 | 13 | var apiStub *httptest.Server 14 | 15 | func theInstalledSdkmanVersionIs(version string) error { 16 | env.SetVersion(version, sdkmanDir) 17 | return nil 18 | } 19 | 20 | func theAvailableSdkmanVersionIs(version string) error { 21 | var response = fmt.Sprintf(`{"version": "%s"}`, version) 22 | apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 23 | w.Write([]byte(response)) 24 | w.WriteHeader(http.StatusOK) 25 | })) 26 | 27 | sdkmanApi = apiStub.URL 28 | 29 | return nil 30 | } 31 | 32 | func thePulledVersionStateIs(version string) error { 33 | 34 | file := filepath.Join(varDir, "localhost", "version") 35 | bytes, err := ioutil.ReadFile(file) 36 | check(err) 37 | 38 | actual := string(bytes) 39 | if actual != version { 40 | return fmt.Errorf("Expected %s but was %s", version, actual) 41 | } 42 | return nil 43 | } 44 | 45 | func VersionFeatureContext(s *godog.Suite) { 46 | s.BeforeScenario(func(i interface{}) { 47 | apiStub = nil 48 | }) 49 | s.Step(`^the installed sdkman version is "(.*)"$`, theInstalledSdkmanVersionIs) 50 | s.Step(`^the available sdkman version is "([^"]*)"$`, theAvailableSdkmanVersionIs) 51 | s.Step(`^the pulled version state is "([^"]*)"$`, thePulledVersionStateIs) 52 | s.AfterScenario(func(interface{}, error) { 53 | if apiStub != nil { 54 | fmt.Print("About to shut down apiStub server...\n") 55 | apiStub.Close() 56 | fmt.Print("Successfully shut down apiStub...\n") 57 | } 58 | }) 59 | } 60 | -------------------------------------------------------------------------------- /txt/colours.go: -------------------------------------------------------------------------------- 1 | package txt 2 | 3 | import "github.com/fatih/color" 4 | 5 | var yellow = color.New(color.FgHiYellow) 6 | var Info = yellow.SprintFunc() 7 | var InfoF = yellow.SprintfFunc() 8 | 9 | var red = color.New(color.FgRed) 10 | var Error = red.SprintFunc() 11 | var ErrorF = red.SprintfFunc() 12 | 13 | var green = color.New(color.FgGreen) 14 | var Success = green.SprintFunc() 15 | var SuccessF = green.SprintfFunc() 16 | 17 | var BroadcastF = color.New(color.FgCyan).SprintfFunc() 18 | --------------------------------------------------------------------------------