Skip to content

Commit 0cd08b2

Browse files
committed
feat(cli): display installed and available releases separately
Refs: #7
1 parent 6eb674b commit 0cd08b2

3 files changed

Lines changed: 64 additions & 9 deletions

File tree

internal/cli/command.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ type ListCommand struct {
196196
}
197197

198198
func (cmd *ListCommand) Execute(args []string) error {
199-
if !pathx.Exists(cmd.appOptions.CachePath()) {
199+
if !pathx.Exists(cmd.appOptions.CachePath) {
200200
return fmt.Errorf("cache path does not exist: %s",
201-
cmd.appOptions.CachePath())
201+
cmd.appOptions.CachePath)
202202
}
203-
cachePath := cmd.appOptions.CachePath()
203+
cachePath := cmd.appOptions.CachePath
204204
releaseCacher := cache.NewFileCacher(cachePath, "nvimm_releases.json")
205205
gt, err := protocol.NewGithubTransport()
206206
if err != nil {
@@ -234,7 +234,27 @@ func (cmd *ListCommand) Execute(args []string) error {
234234
return fmt.Errorf("failed to process releases: %w", err)
235235
}
236236

237-
for _, info := range releases {
237+
installed := releases.Installed(cmd.appOptions.Path)
238+
239+
fmt.Println("Installed versions")
240+
if len(installed) == 0 {
241+
fmt.Println(" no releases installed")
242+
}
243+
244+
for _, info := range installed {
245+
if info.Stable == true {
246+
fmt.Printf(" %s (stable)\n", info.CleanTagName())
247+
continue
248+
}
249+
fmt.Printf(" %s\n", info.CleanTagName())
250+
}
251+
252+
available := releases.Available(installed)
253+
fmt.Println("\nAvailable versions")
254+
if len(available) == 0 {
255+
fmt.Println(" no releases installed")
256+
}
257+
for _, info := range available {
238258
if info.Stable == true {
239259
fmt.Printf("%s (stable)\n", info.CleanTagName())
240260
continue

internal/cli/options_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestOptions(t *testing.T) {
3232
os.Setenv("NVIMM_CONFIG_DIR", "/etc/nvimm")
3333
os.Setenv("NVIMM_CONFIG_FILE_NAME", "nvimm.yaml")
3434
os.Setenv("NVIMM_PATH", "/opt/nvimm")
35-
os.Setenv("NVIMM_CACHE_SUB_DIR", "the_cache")
35+
os.Setenv("NVIMM_CACHE_PATH", "/opt/nvim/cache")
3636
defer os.Unsetenv("NVIMM_CONFIG_DIR")
3737
defer os.Unsetenv("NVIMM_CONFIG_FILE_NAME")
3838
defer os.Unsetenv("NVIMM_PATH")
@@ -61,7 +61,7 @@ func TestOptions(t *testing.T) {
6161
assert.Equal(t, filepath.Join(os.Getenv("NVIMM_CONFIG_DIR"),
6262
os.Getenv("NVIMM_CONFIG_FILE_NAME")), opts.ConfigPath)
6363
assert.Equal(t, os.Getenv("NVIMM_PATH"), opts.Path)
64-
assert.Equal(t, filepath.Join(opts.Path, opts.CacheSubDir), opts.CachePath())
64+
assert.Equal(t, opts.CachePath, opts.CachePath)
6565
})
6666

6767
t.Run("should get default values", func(t *testing.T) {
@@ -92,13 +92,14 @@ func TestOptions(t *testing.T) {
9292
}
9393

9494
assert.Equal(t, expectedConfigPath, opts.ConfigPath)
95-
userCahceDir, err := os.UserCacheDir()
95+
userHomeDir, err := os.UserHomeDir()
9696

9797
if err != nil {
9898
t.Fatalf("error getting user cache dir: %v", err)
9999
}
100-
assert.Equal(t, filepath.Join(userCahceDir, "nvimm"), opts.Path)
100+
assert.Equal(t, filepath.Join(userHomeDir, ".nvimm"), opts.Path)
101101
})
102+
102103
t.Run("should create paths if does not exists", func(t *testing.T) {
103104
var opts AppOptions
104105
dir, err := os.MkdirTemp("", "nvimm-test-")
@@ -139,6 +140,6 @@ func TestOptions(t *testing.T) {
139140
assert.DirExists(t, opts.ConfigDir)
140141
assert.FileExists(t, opts.ConfigPath)
141142
assert.DirExists(t, opts.Path)
142-
assert.DirExists(t, opts.CachePath())
143+
assert.DirExists(t, opts.CachePath)
143144
})
144145
}

internal/release/info.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,40 @@ func (rs *Releases) Get(release string) (*Info, error) {
3131
}
3232
return nil, fmt.Errorf("release %s does not exists", release)
3333
}
34+
35+
// Installed returns a list of releases that are present in the specified path.
36+
func (rs *Releases) Installed(path string) []Info {
37+
releases := *rs
38+
installed := []Info{}
39+
for _, info := range releases {
40+
if pathx.Exists(filepath.Join(path, info.CleanTagName())) {
41+
installed = append(installed, info)
42+
}
43+
}
44+
return installed
45+
}
46+
47+
// Available returns a list of releases that are not present in the installed
48+
// releases.
49+
func (rs *Releases) Available(installed []Info) []Info {
50+
releases := *rs
51+
installedDict := map[string]bool{}
52+
for _, info := range installed {
53+
installedDict[info.CleanTagName()] = true
54+
}
55+
available := []Info{}
56+
for _, info := range releases {
57+
ok := installedDict[info.CleanTagName()]
58+
if !ok {
59+
available = append(available, info)
60+
}
61+
}
62+
return available
63+
}
64+
65+
// Process unmarshals the provided JSON data into the Releases struct. It also
66+
// identifies the stable release and marks the corresponding Info entries
67+
// accordingly.
3468
func (rs *Releases) Process(data []byte) error {
3569
err := json.Unmarshal(data, &rs)
3670
if err != nil {

0 commit comments

Comments
 (0)