Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Jul 11, 2022
1 parent d1aef53 commit 3a79471
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
46 changes: 41 additions & 5 deletions core/module/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/clivern/goenv/core/service"
"github.com/clivern/goenv/core/util"
)

// Golang type
Expand Down Expand Up @@ -253,13 +254,48 @@ func (g *Golang) GetVersions() []string {
}

// GetInstalledVersions returns a list of installed versions
func (g *Golang) GetInstalledVersions() []string {
return []string{}
func (g *Golang) GetInstalledVersions() ([]string, error) {

path := fmt.Sprintf("%s/%s", g.RootPath, g.ReleasesDir)

result, err := g.FileSystem.GetSubDirectoriesNames(path)

releases := []string{}

if err != nil {
return releases, fmt.Errorf(
"Unable to list directory %s: %s",
path,
err.Error(),
)
}

for i := 0; i < len(result); i++ {
if !strings.Contains(result[i], "go") {
continue
}

releases = append(releases, strings.TrimPrefix(result[i], "go"))
}

return releases, nil
}

// CreateShim create a new shim
func (g *Golang) CreateShim() error {
return nil
// ValidateVersion validates if a version is valid
func (g *Golang) ValidateVersion(version string) bool {
return util.InArray(version, g.GetVersions())
}

// ValidateInstalledVersion validates if an installed version is valid
func (g *Golang) ValidateInstalledVersion(version string) (bool, error) {

versions, err := g.GetInstalledVersions()

if err != nil {
return false, err
}

return util.InArray(version, versions), nil
}

// Rehash gets a list of binaries under a certain
Expand Down
38 changes: 38 additions & 0 deletions core/service/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,41 @@ func (fs *FileSystem) ReadFile(path string) (string, error) {
func (fs *FileSystem) Rename(old, new string) error {
return os.Rename(old, new)
}

// GetSubDirectoriesNames gets a list of sub directories
func (fs *FileSystem) GetSubDirectoriesNames(path string) ([]string, error) {
result := []string{}

files, err := ioutil.ReadDir(path)

if err != nil {
return result, err
}

for _, fileInfo := range files {
if fileInfo.IsDir() {
result = append(result, fileInfo.Name())
}
}

return result, nil
}

// GetDirectoryFileNames get a list of files inside a directory
func (fs *FileSystem) GetDirectoryFileNames(path string) ([]string, error) {
result := []string{}

files, err := ioutil.ReadDir(path)

if err != nil {
return result, err
}

for _, fileInfo := range files {
if !fileInfo.IsDir() {
result = append(result, fileInfo.Name())
}
}

return result, nil
}

0 comments on commit 3a79471

Please sign in to comment.