From d7ff49e303e1e7627b4ca063f528b882ba0737e5 Mon Sep 17 00:00:00 2001 From: Ankit Charolia Date: Thu, 27 Jul 2023 12:27:23 +0200 Subject: [PATCH 1/5] fix: version check before using specific go version --- main.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index b19516b..9d26f32 100755 --- a/main.go +++ b/main.go @@ -78,6 +78,7 @@ func main() { installGoVersion(*installVersion) } else if *useVersion != "" { useGoVersion(*useVersion) + return } else if *help { flag.Usage() } else { @@ -254,6 +255,7 @@ func uninstallGoVersion(version string) { func isInstalled(version string) bool { versions := listInstalledVersions() for _, v := range versions { + v = strings.TrimSpace(strings.TrimPrefix(v, "* ")) if v == version { return true } @@ -264,6 +266,12 @@ func isInstalled(version string) bool { // useGoVersion sets the specified Go version as the active version to use. func useGoVersion(version string) { + // Check if the specified Go version is installed + if !isInstalled(version) { + fmt.Printf("Go version %s is not installed. Please install it first.\n", version) + return + } + // Get the installation path for the specified Go version goPath := filepath.Join(os.Getenv("HOME"), ".go", version) @@ -278,7 +286,13 @@ func useGoVersion(version string) { // Update the Go version in the ~/.bashrc file updateGoVersionInBashrc(version) - fmt.Printf("Using Go version %s.\nPlease make sure to execute: source ~/.bashrc\n", version) + // ANSI escape code for red color + redColor := "\033[31m" + // ANSI escape code to reset color to default + resetColor := "\033[0m" + + message := fmt.Sprintf("Using Go version %s.%s\nPlease make sure to execute: source ~/.bashrc\n%s", version, redColor, resetColor) + fmt.Print(message) } // updateGoVersionInBashrc updates the Go version in the ~/.bashrc file. From 94d879af010168aa91361f7a28605b762207b513 Mon Sep 17 00:00:00 2001 From: Ankit Charolia Date: Thu, 27 Jul 2023 12:35:19 +0200 Subject: [PATCH 2/5] update README.md --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 51415f1..0112e4b 100644 --- a/README.md +++ b/README.md @@ -69,5 +69,9 @@ Installed Golang versions: 1.20.4 ``` -## License -This project is licensed under the MIT License - see the LICENSE file for details. \ No newline at end of file +## Supported Shell +* Bash +* Zsh (WIP) + +## Support +Feel free to create an Issue/Pull Request if you find any bug with `goenv` \ No newline at end of file From 4d67acd28f02037e3454cc26e3da4e6bfd7c2b99 Mon Sep 17 00:00:00 2001 From: Ankit Charolia Date: Thu, 27 Jul 2023 13:03:02 +0200 Subject: [PATCH 3/5] check if .go directory exists and return empty array if no go version installed --- main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/main.go b/main.go index 9d26f32..07b3f79 100755 --- a/main.go +++ b/main.go @@ -54,6 +54,7 @@ func main() { versions := listInstalledVersions() if len(versions) == 0 { fmt.Println("No installed Golang versions found.") + return } else { fmt.Println("Installed Golang versions:") for _, version := range versions { @@ -205,6 +206,21 @@ func extractTarGz(src io.Reader, dest string) error { // listInstalledVersions lists all installed Golang versions. func listInstalledVersions() []string { installPath := filepath.Join(os.Getenv("HOME"), ".go") + + // Check if the .go directory exists + _, err := os.Stat(installPath) + if err != nil { + // If the .go directory doesn't exist, create it and return an empty list + if os.IsNotExist(err) { + err = os.Mkdir(installPath, os.ModePerm) + if err != nil { + log.Fatalf("Failed to create .go directory: %v", err) + } + return []string{} // Return an empty list to indicate no Golang versions are installed + } + log.Fatalf("Failed to read directory: %v", err) + } + activeVersion := getCurrentGoVersion() fileInfos, err := os.ReadDir(installPath) if err != nil { From fb676209a98171c794d64dd689680f75671116bb Mon Sep 17 00:00:00 2001 From: Ankit Charolia Date: Fri, 28 Jul 2023 22:48:54 +0200 Subject: [PATCH 4/5] fix uninstallGoVersion go function --- main.go | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 07b3f79..67bd584 100755 --- a/main.go +++ b/main.go @@ -65,10 +65,6 @@ func main() { } if *uninstallVersion != "" { - if !isInstalled(*uninstallVersion) { - fmt.Printf("Go version %s is not installed.\n", *uninstallVersion) - return - } uninstallGoVersion(*uninstallVersion) return } @@ -258,13 +254,37 @@ func getCurrentGoVersion() string { // uninstallGoVersion uninstalls a specific Golang version. func uninstallGoVersion(version string) { - installPath := filepath.Join(os.Getenv("HOME"), ".go", version) - err := os.RemoveAll(installPath) - if err != nil { - log.Fatalf("Failed to uninstall Go version: %v", err) - } + if isInstalled(version) { + if version == getCurrentGoVersion() { + // If the version to be uninstalled is the currently active version, + // switch to another installed version before uninstalling it. + versions := listInstalledVersions() + for _, v := range versions { + v = strings.TrimSpace(strings.TrimPrefix(v, "* ")) + if v != version { + useGoVersion(v) + break + } + } + fmt.Printf("Switched to Go version %s (currently active) before uninstalling.\n", version) + } - fmt.Printf("Go version %s has been uninstalled.\n", version) + installPath := filepath.Join(os.Getenv("HOME"), ".go", version) + err := os.RemoveAll(installPath) + if err != nil { + log.Fatalf("Failed to uninstall Go version: %v", err) + } + + fmt.Printf("Go version %s has been uninstalled.\n", version) + } else { + if version == getCurrentGoVersion() { + fmt.Printf("Cannot uninstall Go version %s because it is currently active.\n", version) + fmt.Printf("To uninstall, please switch to another installed version first.\n") + } else { + fmt.Printf("Go version %s is not installed. Please install it first.\n", version) + } + + } } // isInstalled checks if a specific Golang version is already installed. From 311b154e00b9671b678fb30371e84b812c1d6153 Mon Sep 17 00:00:00 2001 From: Ankit Charolia Date: Fri, 28 Jul 2023 22:59:11 +0200 Subject: [PATCH 5/5] docs: update README.md --- README.md | 2 +- main.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 0112e4b..2f631c9 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Installed Golang versions: ## Supported Shell * Bash -* Zsh (WIP) +* Zsh (WIP) [WORKAROUND: Add `export PATH=$HOME/.go//bin:$PATH` to `zshrc` file] ## Support Feel free to create an Issue/Pull Request if you find any bug with `goenv` \ No newline at end of file diff --git a/main.go b/main.go index 67bd584..20ab63b 100755 --- a/main.go +++ b/main.go @@ -283,7 +283,6 @@ func uninstallGoVersion(version string) { } else { fmt.Printf("Go version %s is not installed. Please install it first.\n", version) } - } }