Skip to content

Commit

Permalink
check if .go directory exists and return empty array if no go version…
Browse files Browse the repository at this point in the history
… installed
  • Loading branch information
ankitcharolia committed Jul 27, 2023
1 parent 5734776 commit 67b2a28
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 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 Down Expand Up @@ -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 {
Expand Down

0 comments on commit 67b2a28

Please sign in to comment.