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

fix: should not show install failed versions when list, close #45 #46

Merged
merged 1 commit into from
Sep 3, 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
11 changes: 11 additions & 0 deletions internal/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,14 @@ func checkStringExistsFile(filename, value string) (bool, error) {

return false, scanner.Err()
}

func checkInstalled(targetDir string) bool {
if _, err := os.Stat(filepath.Join(targetDir, unpackedOkay)); err == nil {
return true
}
return false
}

func setInstalled(targetDir string) error {
return os.WriteFile(filepath.Join(targetDir, unpackedOkay), nil, 0644)
}
17 changes: 10 additions & 7 deletions internal/commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,11 @@ func symlink(ver string) error {
func install(version string) error {
targetDir := goupVersionDir(version)

if _, err := os.Stat(filepath.Join(targetDir, unpackedOkay)); err == nil {
logger.Printf("%s: already downloaded in %v", version, targetDir)
if checkInstalled(targetDir) {
logger.Printf("%s: already installed in %v", version, targetDir)
return nil
}

if err := os.MkdirAll(targetDir, 0755); err != nil {
return err
}
goURL := versionArchiveURL(version)
res, err := http.Head(goURL)
if err != nil {
Expand All @@ -190,6 +187,11 @@ func install(version string) error {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("server returned %v checking size of %v", http.StatusText(res.StatusCode), goURL)
}

if err := os.MkdirAll(targetDir, 0755); err != nil {
return err
}

base := path.Base(goURL)
archiveFile := filepath.Join(targetDir, base)
if fi, err := os.Stat(archiveFile); err != nil || fi.Size() != res.ContentLength {
Expand Down Expand Up @@ -219,10 +221,11 @@ func install(version string) error {
if err := unpackArchive(targetDir, archiveFile); err != nil {
return fmt.Errorf("extracting archive %v: %v", archiveFile, err)
}
if err := os.WriteFile(filepath.Join(targetDir, unpackedOkay), nil, 0644); err != nil {

if err := setInstalled(targetDir); err != nil {
return err
}
logger.Printf("Success: %s downloaded in %v", version, targetDir)
logger.Printf("Success: %s installed in %v", version, targetDir)
return nil
}

Expand Down
10 changes: 9 additions & 1 deletion internal/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ type goVer struct {
}

func listGoVers() ([]goVer, error) {
files, err := os.ReadDir(GoupDir())
baseDir := GoupDir()
files, err := os.ReadDir(baseDir)
if err != nil {
return nil, err
}
Expand All @@ -78,7 +79,14 @@ func listGoVers() ([]goVer, error) {

var vers []goVer
for _, file := range files {
if !file.IsDir() {
continue
}
if strings.HasPrefix(file.Name(), "go") {
// will not set installed for the gotip, so should not check installed for it
if file.Name() != "gotip" && !checkInstalled(filepath.Join(baseDir, file.Name())) {
continue
}
vers = append(vers, goVer{
Ver: strings.TrimPrefix(file.Name(), "go"),
Current: current == file.Name(),
Expand Down