Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix version check #5

Merged
merged 5 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
## Supported Shell
* Bash
* Zsh (WIP) [WORKAROUND: Add `export PATH=$HOME/.go/<Installed GO Version>/bin:$PATH` to `zshrc` file]

## Support
Feel free to create an Issue/Pull Request if you find any bug with `goenv`
71 changes: 60 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -64,10 +65,6 @@ func main() {
}

if *uninstallVersion != "" {
if !isInstalled(*uninstallVersion) {
fmt.Printf("Go version %s is not installed.\n", *uninstallVersion)
return
}
uninstallGoVersion(*uninstallVersion)
return
}
Expand All @@ -78,6 +75,7 @@ func main() {
installGoVersion(*installVersion)
} else if *useVersion != "" {
useGoVersion(*useVersion)
return
} else if *help {
flag.Usage()
} else {
Expand Down Expand Up @@ -204,6 +202,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 {
Expand Down Expand Up @@ -241,19 +254,43 @@ 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.
func isInstalled(version string) bool {
versions := listInstalledVersions()
for _, v := range versions {
v = strings.TrimSpace(strings.TrimPrefix(v, "* "))
if v == version {
return true
}
Expand All @@ -264,6 +301,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)

Expand All @@ -278,7 +321,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.
Expand Down