Skip to content

Commit

Permalink
chore: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan committed May 13, 2024
1 parent 1ffe578 commit bd0d4fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
9 changes: 6 additions & 3 deletions internal/env/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package env

import (
"fmt"
"github.com/version-fox/vfox/internal/logger"
"github.com/version-fox/vfox/internal/util"
"os"
"path/filepath"
Expand All @@ -43,12 +45,13 @@ func (p *Paths) Merge(other *Paths) *Paths {
}

// ToBinPaths returns a BinPaths from Paths which contains only executable files.
func (p *Paths) ToBinPaths() *Paths {
func (p *Paths) ToBinPaths() (*Paths, error) {
bins := NewPaths(EmptyPaths)
for _, path := range p.Slice() {
dir, err := os.ReadDir(path)
if err != nil {
return nil
logger.Debugf("Failed to read bin paths:%s: %v", path, err)
return nil, fmt.Errorf("failed to read bin paths:%s: %w", path, err)
}
for _, d := range dir {
if d.IsDir() {
Expand All @@ -60,7 +63,7 @@ func (p *Paths) ToBinPaths() *Paths {
}
}
}
return bins
return bins, nil
}

// NewPaths returns a new Paths.
Expand Down
18 changes: 13 additions & 5 deletions internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,10 @@ func (b *Sdk) Use(version Version, scope UseScope) error {
return fmt.Errorf("failed to read tool versions, err:%w", err)
}

bins := keys.Paths.ToBinPaths()
bins, err := keys.Paths.ToBinPaths()
if err != nil {
return err
}
for _, bin := range bins.Slice() {
binShim := shim.NewShim(bin, b.sdkManager.PathMeta.GlobalShimsPath)
if err = binShim.Generate(); err != nil {
Expand Down Expand Up @@ -428,7 +431,10 @@ func (b *Sdk) useInHook(version Version, scope UseScope) error {
if err != nil {
return err
}
binPaths := envKeys.Paths.ToBinPaths()
binPaths, err := envKeys.Paths.ToBinPaths()
if err != nil {
return err
}

sdkEnv := SdkEnv{
Sdk: b,
Expand Down Expand Up @@ -732,9 +738,11 @@ func (b *Sdk) ClearCurrentEnv() error {
return err
}
fmt.Println("Cleaning up the shims...")
for _, p := range keys.Paths.ToBinPaths().Slice() {
if err = shim.NewShim(p, b.sdkManager.PathMeta.GlobalShimsPath).Clear(); err != nil {
return err
if paths, err := keys.Paths.ToBinPaths(); err == nil {
for _, p := range paths.Slice() {
if err = shim.NewShim(p, b.sdkManager.PathMeta.GlobalShimsPath).Clear(); err != nil {
return err
}
}
}

Expand Down

0 comments on commit bd0d4fd

Please sign in to comment.