Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Vaumoron <dvaumoron@gmail.com>
  • Loading branch information
dvaumoron committed Jul 14, 2024
1 parent 71eb141 commit 7b0e8fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
11 changes: 5 additions & 6 deletions cmd/tenv/subcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ package main
import (
"bytes"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/tofuutils/tenv/v2/config"
"github.com/tofuutils/tenv/v2/pkg/loghelper"
"github.com/tofuutils/tenv/v2/versionmanager"
"github.com/tofuutils/tenv/v2/versionmanager/lastuse"
"github.com/tofuutils/tenv/v2/versionmanager/semantic"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -184,7 +182,7 @@ func newListCmd(conf *config.Config, versionManager versionmanager.VersionManage
Run: func(_ *cobra.Command, _ []string) {
conf.InitDisplayer(false)

installPath, versions, err := versionManager.ListLocal(reverseOrder)
datedVersions, err := versionManager.ListLocal(reverseOrder)
if err != nil {
loghelper.StdDisplay(err.Error())

Expand All @@ -199,8 +197,9 @@ func newListCmd(conf *config.Config, versionManager versionmanager.VersionManage
usedVersion := string(bytes.TrimSpace(data))

nilTime := time.Time{}
for _, version := range versions {
useDate := lastuse.Read(filepath.Join(installPath, version), conf.Displayer)
for _, datedVersion := range datedVersions {
useDate := datedVersion.UseDate
version := datedVersion.Version
noUseDate := useDate == nilTime
switch {
case usedVersion == version:
Expand All @@ -216,7 +215,7 @@ func newListCmd(conf *config.Config, versionManager versionmanager.VersionManage
}
}
if conf.DisplayVerbose {
loghelper.StdDisplay(loghelper.Concat("found ", strconv.Itoa(len(versions)), " ", versionManager.FolderName, " version(s) managed by tenv."))
loghelper.StdDisplay(loghelper.Concat("found ", strconv.Itoa(len(datedVersions)), " ", versionManager.FolderName, " version(s) managed by tenv."))
}
},
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/tenv/textui.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ func (m itemModel) View() string {
}

func uninstallUI(versionManager versionmanager.VersionManager) error {
_, versions, err := versionManager.ListLocal(false)
datedVersions, err := versionManager.ListLocal(false)
if err != nil {
return err
}

items := make([]list.Item, 0, len(versions))
for _, version := range versions {
items = append(items, item(version))
items := make([]list.Item, 0, len(datedVersions))
for _, datedVersion := range datedVersions {
items = append(items, item(datedVersion.Version))
}

// shared object
Expand Down
41 changes: 31 additions & 10 deletions versionmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"path/filepath"
"slices"
"strings"
"time"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-version"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/tofuutils/tenv/v2/pkg/lockfile"
"github.com/tofuutils/tenv/v2/pkg/loghelper"
"github.com/tofuutils/tenv/v2/pkg/reversecmp"
"github.com/tofuutils/tenv/v2/versionmanager/lastuse"
"github.com/tofuutils/tenv/v2/versionmanager/semantic"
flatparser "github.com/tofuutils/tenv/v2/versionmanager/semantic/parser/flat"
iacparser "github.com/tofuutils/tenv/v2/versionmanager/semantic/parser/iac"
Expand All @@ -50,6 +52,11 @@ type ReleaseInfoRetriever interface {
ListReleases() ([]string, error)
}

type DatedVersion struct {
UseDate time.Time
Version string
}

type VersionManager struct {
conf *config.Config
constraintEnvName string
Expand Down Expand Up @@ -107,14 +114,15 @@ func (m VersionManager) Evaluate(requestedVersion string, proxyCall bool) (strin
}

if !m.conf.ForceRemote {
_, versions, err := m.ListLocal(predicateInfo.ReverseOrder)
datedVersions, err := m.ListLocal(predicateInfo.ReverseOrder)
if err != nil {
m.conf.Displayer.Flush(proxyCall)

return "", err
}

for _, version := range versions {
for _, datedVersion := range datedVersions {
version := datedVersion.Version
if predicateInfo.Predicate(version) {
m.conf.Displayer.Display("Found compatible version installed locally : " + version)
m.conf.Displayer.Flush(proxyCall)
Expand Down Expand Up @@ -154,28 +162,36 @@ func (m VersionManager) InstallPath() (string, error) {
return dirPath, os.MkdirAll(dirPath, 0o755)
}

func (m VersionManager) ListLocal(reverseOrder bool) (string, []string, error) {
func (m VersionManager) ListLocal(reverseOrder bool) ([]DatedVersion, error) {
installPath, err := m.InstallPath()
if err != nil {
return "", nil, err
return nil, err
}

entries, err := os.ReadDir(installPath)
if err != nil {
return "", nil, err
return nil, err
}

versions := make([]string, 0, len(entries))
versions := make([]DatedVersion, 0, len(entries))
for _, entry := range entries {
if entry.IsDir() {
versions = append(versions, entry.Name())
version := entry.Name()
versions = append(versions, DatedVersion{
UseDate: lastuse.Read(filepath.Join(installPath, version), m.conf.Displayer),
Version: version,
})
}
}

cmpFunc := reversecmp.Reverser[string](semantic.CmpVersion, reverseOrder)
cmpDatedVersion := func(dv1 DatedVersion, dv2 DatedVersion) int {
return semantic.CmpVersion(dv1.Version, dv2.Version)
}

cmpFunc := reversecmp.Reverser[DatedVersion](cmpDatedVersion, reverseOrder)
slices.SortFunc(versions, cmpFunc)

return installPath, versions, nil
return versions, nil
}

func (m VersionManager) ListRemote(reverseOrder bool) ([]string, error) {
Expand Down Expand Up @@ -299,11 +315,16 @@ func (m VersionManager) Uninstall(requestedVersion string) error {
return nil
}

_, versions, err := m.ListLocal(true)
datedVersions, err := m.ListLocal(true)
if err != nil {
return err
}

versions := make([]string, 0, len(datedVersions))
for _, datedVersion := range datedVersions {
versions = append(versions, datedVersion.Version)
}

selected, err := semantic.SelectVersionsToUninstall(requestedVersion, installPath, versions, m.conf.Displayer)
if err != nil {
return err
Expand Down

0 comments on commit 7b0e8fd

Please sign in to comment.