-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
186 lines (155 loc) · 4.58 KB
/
list.go
File metadata and controls
186 lines (155 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cmd
import (
"fmt"
"github.com/CodingWithCalvin/dtvem.cli/src/internal/config"
"github.com/CodingWithCalvin/dtvem.cli/src/internal/runtime"
"github.com/CodingWithCalvin/dtvem.cli/src/internal/tui"
"github.com/CodingWithCalvin/dtvem.cli/src/internal/ui"
"github.com/spf13/cobra"
)
// Version indicator emojis
const (
globalIndicator = "🌐"
localIndicator = "📍"
)
var listCmd = &cobra.Command{
Use: "list [runtime]",
Short: "List installed versions",
Long: `List all installed versions of a specific runtime, or all runtimes if none specified.
Examples:
dtvem list # List all installed versions
dtvem list python # List installed Python versions
dtvem list node # List installed Node.js versions`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
listAllRuntimes()
} else {
listSingleRuntime(args[0])
}
},
}
// listAllRuntimes lists installed versions for all runtimes
func listAllRuntimes() {
ui.Debug("Listing installed versions for all runtimes")
providers := runtime.GetAll()
ui.Debug("Found %d registered providers", len(providers))
if len(providers) == 0 {
ui.Info("No runtime providers registered")
return
}
hasAny := false
for _, provider := range providers {
ui.Debug("Checking provider: %s", provider.Name())
versions, err := provider.ListInstalled()
if err != nil {
ui.Debug("Error listing versions for %s: %v", provider.Name(), err)
ui.Error(" %s: %v", provider.DisplayName(), err)
continue
}
ui.Debug("Found %d installed versions for %s", len(versions), provider.Name())
if len(versions) == 0 {
continue
}
hasAny = true
runtimeName := provider.Name()
globalVersion, _ := provider.GlobalVersion()
localVersion, _ := config.LocalVersion(runtimeName)
// Create table for this runtime with title
table := tui.NewTable("Version", "Status")
table.SetTitle(provider.DisplayName())
for _, v := range versions {
version := v.String()
status := getVersionStatus(version, globalVersion, localVersion)
isActive := isVersionActive(version, globalVersion, localVersion)
if isActive {
table.AddActiveRow(version, status)
} else {
table.AddRow(version, status)
}
}
fmt.Println(table.Render())
}
if !hasAny {
ui.Info("No versions installed")
}
}
// listSingleRuntime lists installed versions for a specific runtime
func listSingleRuntime(runtimeName string) {
provider, err := runtime.Get(runtimeName)
if err != nil {
ui.Error("%v", err)
ui.Info("Available runtimes: %v", runtime.List())
return
}
versions, err := provider.ListInstalled()
if err != nil {
ui.Error("%v", err)
return
}
if len(versions) == 0 {
ui.Info("No versions installed")
return
}
globalVersion, _ := provider.GlobalVersion()
localVersion, _ := config.LocalVersion(runtimeName)
// Create table with title
table := tui.NewTable("Version", "Status")
table.SetTitle(provider.DisplayName())
for _, v := range versions {
version := v.String()
status := getVersionStatus(version, globalVersion, localVersion)
isActive := isVersionActive(version, globalVersion, localVersion)
if isActive {
table.AddActiveRow(version, status)
} else {
table.AddRow(version, status)
}
}
fmt.Println(table.Render())
}
// getVersionStatus returns a status string for a version (global, local, or empty)
func getVersionStatus(version, globalVersion, localVersion string) string {
isGlobal := version == globalVersion
isLocal := version == localVersion
var parts []string
if isLocal {
parts = append(parts, localIndicator+" local")
}
if isGlobal {
parts = append(parts, globalIndicator+" global")
}
if len(parts) == 0 {
return ""
}
status := ""
for i, p := range parts {
if i > 0 {
status += ", "
}
status += p
}
return status
}
// getVersionStatusWithLifecycle returns a status string that combines the
// global/local indicators with a color-coded lifecycle label (e.g., "Active LTS").
func getVersionStatusWithLifecycle(version, globalVersion, localVersion, lifecycleStatus string) string {
base := getVersionStatus(version, globalVersion, localVersion)
colored := tui.RenderLifecycleStatus(lifecycleStatus)
if colored == "" {
return base
}
if base == "" {
return colored
}
return base + " · " + colored
}
// isVersionActive returns true if this version is the currently active one
func isVersionActive(version, globalVersion, localVersion string) bool {
isGlobal := version == globalVersion
isLocal := version == localVersion
return isLocal || (isGlobal && localVersion == "")
}
func init() {
rootCmd.AddCommand(listCmd)
}