Skip to content

Commit

Permalink
fix: version check before using specific go version
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitcharolia committed Jul 27, 2023
1 parent 8ffa68a commit 56ce611
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func main() {
installGoVersion(*installVersion)
} else if *useVersion != "" {
useGoVersion(*useVersion)
return
} else if *help {
flag.Usage()
} else {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)

Expand All @@ -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.
Expand Down

0 comments on commit 56ce611

Please sign in to comment.