Skip to content

Commit

Permalink
list-local: new command
Browse files Browse the repository at this point in the history
  • Loading branch information
ekalinin committed Jul 21, 2020
1 parent dde286f commit 7ab601b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/listLocal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"os"
"strconv"

"github.com/ekalinin/pbvm/utils"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

// listLocalCmd represents the listLocal command
var listLocalCmd = &cobra.Command{
Use: "list-local",
Short: "List local (previously installed) versions",
Long: `Shows list of installed versions.`,
Run: func(cmd *cobra.Command, args []string) {
// version, install date (folder stat), active?
versions, err := utils.ListInstalledVersions(pbName)
if err != nil {
panic(err)
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Version", "Install date", "Active"})

for _, v := range versions {
table.Append([]string{
v.Version,
v.Date.Format(pbDateFormat),
strconv.FormatBool(v.Active),
})
}
table.SetBorder(false)
table.Render()
},
}

func init() {
rootCmd.AddCommand(listLocalCmd)
}
62 changes: 62 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/google/go-github/v32/github"
)
Expand Down Expand Up @@ -228,6 +230,44 @@ func IsInstalledVersion(app, version string) (bool, string, error) {
return true, versionDir, nil
}

// InstalledVersion describes installed version
type InstalledVersion struct {
Version string
Date time.Time
Active bool
}

// ListInstalledVersions returns a slice of installed versions
func ListInstalledVersions(app string) ([]InstalledVersion, error) {
versionsDir, err := GetHomeVersionsDir(app)
if err != nil {
return nil, err
}

files, err := ioutil.ReadDir(versionsDir)
if err != nil {
return nil, err
}

res := []InstalledVersion{}
for _, f := range files {
if !f.IsDir() {
continue
}
active, err := IsActiveVersion(app, f.Name())
if err != nil {
return nil, err
}
res = append(res, InstalledVersion{
Version: f.Name(),
Date: f.ModTime(),
Active: active,
})
}

return res, nil
}

// ActivateVersion activates certain version
func ActivateVersion(app, version string) error {

Expand Down Expand Up @@ -255,6 +295,28 @@ func ActivateVersion(app, version string) error {
return nil
}

// IsActiveVersion returns bool if version is active
func IsActiveVersion(app, version string) (bool, error) {
activeDir, err := GetHomeActiveDir(app)
if err != nil {
return false, err
}

l, err := os.Readlink(path.Join(activeDir, "bin"))
if err != nil {
return false, err
}

// converting
// "/home/user/.pbvm/versions/v3.12.3/bin" -> "v3.12.3"
activeVer := path.Base(path.Dir(l))
if activeVer == version {
return true, nil
}

return false, nil
}

// FilterAsset finds an asset which is need to be downloaded
func FilterAsset(release *github.RepositoryRelease) *github.ReleaseAsset {
arch := GetArch()
Expand Down

0 comments on commit 7ab601b

Please sign in to comment.