Skip to content

Commit 8d55051

Browse files
Merge pull request #5 from ankitcharolia/feature/fix-version-check
Feature/fix version check
2 parents 8ffa68a + 311b154 commit 8d55051

2 files changed

Lines changed: 66 additions & 13 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,9 @@ Installed Golang versions:
6969
1.20.4
7070
```
7171

72-
## License
73-
This project is licensed under the MIT License - see the LICENSE file for details.
72+
## Supported Shell
73+
* Bash
74+
* Zsh (WIP) [WORKAROUND: Add `export PATH=$HOME/.go/<Installed GO Version>/bin:$PATH` to `zshrc` file]
75+
76+
## Support
77+
Feel free to create an Issue/Pull Request if you find any bug with `goenv`

main.go

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func main() {
5454
versions := listInstalledVersions()
5555
if len(versions) == 0 {
5656
fmt.Println("No installed Golang versions found.")
57+
return
5758
} else {
5859
fmt.Println("Installed Golang versions:")
5960
for _, version := range versions {
@@ -64,10 +65,6 @@ func main() {
6465
}
6566

6667
if *uninstallVersion != "" {
67-
if !isInstalled(*uninstallVersion) {
68-
fmt.Printf("Go version %s is not installed.\n", *uninstallVersion)
69-
return
70-
}
7168
uninstallGoVersion(*uninstallVersion)
7269
return
7370
}
@@ -78,6 +75,7 @@ func main() {
7875
installGoVersion(*installVersion)
7976
} else if *useVersion != "" {
8077
useGoVersion(*useVersion)
78+
return
8179
} else if *help {
8280
flag.Usage()
8381
} else {
@@ -204,6 +202,21 @@ func extractTarGz(src io.Reader, dest string) error {
204202
// listInstalledVersions lists all installed Golang versions.
205203
func listInstalledVersions() []string {
206204
installPath := filepath.Join(os.Getenv("HOME"), ".go")
205+
206+
// Check if the .go directory exists
207+
_, err := os.Stat(installPath)
208+
if err != nil {
209+
// If the .go directory doesn't exist, create it and return an empty list
210+
if os.IsNotExist(err) {
211+
err = os.Mkdir(installPath, os.ModePerm)
212+
if err != nil {
213+
log.Fatalf("Failed to create .go directory: %v", err)
214+
}
215+
return []string{} // Return an empty list to indicate no Golang versions are installed
216+
}
217+
log.Fatalf("Failed to read directory: %v", err)
218+
}
219+
207220
activeVersion := getCurrentGoVersion()
208221
fileInfos, err := os.ReadDir(installPath)
209222
if err != nil {
@@ -241,19 +254,43 @@ func getCurrentGoVersion() string {
241254

242255
// uninstallGoVersion uninstalls a specific Golang version.
243256
func uninstallGoVersion(version string) {
244-
installPath := filepath.Join(os.Getenv("HOME"), ".go", version)
245-
err := os.RemoveAll(installPath)
246-
if err != nil {
247-
log.Fatalf("Failed to uninstall Go version: %v", err)
248-
}
257+
if isInstalled(version) {
258+
if version == getCurrentGoVersion() {
259+
// If the version to be uninstalled is the currently active version,
260+
// switch to another installed version before uninstalling it.
261+
versions := listInstalledVersions()
262+
for _, v := range versions {
263+
v = strings.TrimSpace(strings.TrimPrefix(v, "* "))
264+
if v != version {
265+
useGoVersion(v)
266+
break
267+
}
268+
}
269+
fmt.Printf("Switched to Go version %s (currently active) before uninstalling.\n", version)
270+
}
249271

250-
fmt.Printf("Go version %s has been uninstalled.\n", version)
272+
installPath := filepath.Join(os.Getenv("HOME"), ".go", version)
273+
err := os.RemoveAll(installPath)
274+
if err != nil {
275+
log.Fatalf("Failed to uninstall Go version: %v", err)
276+
}
277+
278+
fmt.Printf("Go version %s has been uninstalled.\n", version)
279+
} else {
280+
if version == getCurrentGoVersion() {
281+
fmt.Printf("Cannot uninstall Go version %s because it is currently active.\n", version)
282+
fmt.Printf("To uninstall, please switch to another installed version first.\n")
283+
} else {
284+
fmt.Printf("Go version %s is not installed. Please install it first.\n", version)
285+
}
286+
}
251287
}
252288

253289
// isInstalled checks if a specific Golang version is already installed.
254290
func isInstalled(version string) bool {
255291
versions := listInstalledVersions()
256292
for _, v := range versions {
293+
v = strings.TrimSpace(strings.TrimPrefix(v, "* "))
257294
if v == version {
258295
return true
259296
}
@@ -264,6 +301,12 @@ func isInstalled(version string) bool {
264301
// useGoVersion sets the specified Go version as the active version to use.
265302
func useGoVersion(version string) {
266303

304+
// Check if the specified Go version is installed
305+
if !isInstalled(version) {
306+
fmt.Printf("Go version %s is not installed. Please install it first.\n", version)
307+
return
308+
}
309+
267310
// Get the installation path for the specified Go version
268311
goPath := filepath.Join(os.Getenv("HOME"), ".go", version)
269312

@@ -278,7 +321,13 @@ func useGoVersion(version string) {
278321
// Update the Go version in the ~/.bashrc file
279322
updateGoVersionInBashrc(version)
280323

281-
fmt.Printf("Using Go version %s.\nPlease make sure to execute: source ~/.bashrc\n", version)
324+
// ANSI escape code for red color
325+
redColor := "\033[31m"
326+
// ANSI escape code to reset color to default
327+
resetColor := "\033[0m"
328+
329+
message := fmt.Sprintf("Using Go version %s.%s\nPlease make sure to execute: source ~/.bashrc\n%s", version, redColor, resetColor)
330+
fmt.Print(message)
282331
}
283332

284333
// updateGoVersionInBashrc updates the Go version in the ~/.bashrc file.

0 commit comments

Comments
 (0)