Skip to content

Commit

Permalink
fix: should not show install failed versions when list, close #45 (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenzou authored Sep 3, 2023
1 parent fca4c7f commit 1f08e43
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
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 @@ -172,14 +172,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 @@ -191,6 +188,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 @@ -220,10 +222,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

0 comments on commit 1f08e43

Please sign in to comment.