-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathlist.go
More file actions
195 lines (166 loc) · 5.38 KB
/
list.go
File metadata and controls
195 lines (166 loc) · 5.38 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
187
188
189
190
191
192
193
194
195
package aitools
import (
"errors"
"fmt"
"maps"
"slices"
"strings"
"text/tabwriter"
"github.com/databricks/cli/libs/aitools/installer"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/log"
"github.com/spf13/cobra"
)
// listSkillsFn is the function used to render the skills list.
// It is a package-level var so tests can replace the data-fetching layer.
var listSkillsFn = defaultListSkills
func NewListCmd() *cobra.Command {
var scopeFlag string
var projectFlag, globalFlag bool
cmd := &cobra.Command{
Use: "list",
Short: "List installed AI tools components",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
// Reject the legacy --project --global combination here so it
// doesn't silently degrade to --scope=both. Users who want both
// scopes should use --scope=both (the new explicit spelling).
if projectFlag && globalFlag && scopeFlag == "" {
return errors.New("cannot use --global and --project together")
}
projectFlag, globalFlag, err := parseScopeFlag(scopeFlag, projectFlag, globalFlag, true)
if err != nil {
return err
}
// list: empty scope = show both. --scope=both also lands here.
var scope string
switch {
case projectFlag && !globalFlag:
scope = installer.ScopeProject
case globalFlag && !projectFlag:
scope = installer.ScopeGlobal
}
return listSkillsFn(cmd, scope)
},
}
cmd.Flags().StringVar(&scopeFlag, "scope", "", "Scope to show: project, global, or both (default: both)")
cmd.Flags().BoolVar(&projectFlag, "project", false, "Show only project-scoped skills")
cmd.Flags().BoolVar(&globalFlag, "global", false, "Show only globally-scoped skills")
markScopeBoolsDeprecated(cmd)
return cmd
}
func defaultListSkills(cmd *cobra.Command, scope string) error {
ctx := cmd.Context()
ref, explicit, err := installer.GetSkillsRef(ctx)
if err != nil {
return err
}
src := &installer.GitHubManifestSource{}
manifest, ref, err := installer.FetchSkillsManifestWithFallback(ctx, src, ref, !explicit)
if err != nil {
return fmt.Errorf("failed to fetch manifest: %w", err)
}
// Load global state.
var globalState *installer.InstallState
if scope != installer.ScopeProject {
globalDir, gErr := installer.GlobalSkillsDir(ctx)
if gErr == nil {
globalState, err = installer.LoadState(globalDir)
if err != nil {
log.Debugf(ctx, "Could not load global install state: %v", err)
}
}
}
// Load project state.
var projectState *installer.InstallState
if scope != installer.ScopeGlobal {
projectDir, pErr := installer.ProjectSkillsDir(ctx)
if pErr == nil {
projectState, err = installer.LoadState(projectDir)
if err != nil {
log.Debugf(ctx, "Could not load project install state: %v", err)
}
}
}
// Build sorted list of skill names.
names := slices.Sorted(maps.Keys(manifest.Skills))
version := strings.TrimPrefix(ref, "v")
cmdio.LogString(ctx, "Available skills (v"+version+"):")
cmdio.LogString(ctx, "")
var buf strings.Builder
tw := tabwriter.NewWriter(&buf, 0, 4, 2, ' ', 0)
fmt.Fprintln(tw, " NAME\tVERSION\tINSTALLED")
bothScopes := globalState != nil && projectState != nil
globalCount := 0
projectCount := 0
for _, name := range names {
meta := manifest.Skills[name]
tag := ""
if meta.Experimental {
tag = " [experimental]"
}
installedStr := installedStatus(name, meta.Version, globalState, projectState, bothScopes)
if globalState != nil {
if _, ok := globalState.Skills[name]; ok {
globalCount++
}
}
if projectState != nil {
if _, ok := projectState.Skills[name]; ok {
projectCount++
}
}
fmt.Fprintf(tw, " %s%s\tv%s\t%s\n", name, tag, meta.Version, installedStr)
}
tw.Flush()
cmdio.LogString(ctx, buf.String())
// Summary line.
switch {
case bothScopes:
cmdio.LogString(ctx, fmt.Sprintf("%d/%d skills installed (global), %d/%d (project)", globalCount, len(names), projectCount, len(names)))
case projectState != nil:
cmdio.LogString(ctx, fmt.Sprintf("%d/%d skills installed (project)", projectCount, len(names)))
case scope == installer.ScopeProject:
cmdio.LogString(ctx, fmt.Sprintf("%d/%d skills installed (project)", 0, len(names)))
default:
cmdio.LogString(ctx, fmt.Sprintf("%d/%d skills installed (global)", globalCount, len(names)))
}
return nil
}
// installedStatus returns the display string for a skill's installation status.
func installedStatus(name, latestVersion string, globalState, projectState *installer.InstallState, bothScopes bool) string {
globalVer := ""
projectVer := ""
if globalState != nil {
globalVer = globalState.Skills[name]
}
if projectState != nil {
projectVer = projectState.Skills[name]
}
if globalVer == "" && projectVer == "" {
return "not installed"
}
// If both scopes have the skill, show the project version (takes precedence).
if bothScopes && globalVer != "" && projectVer != "" {
return versionLabel(projectVer, latestVersion) + " (project, global)"
}
if projectVer != "" {
label := versionLabel(projectVer, latestVersion)
if bothScopes {
return label + " (project)"
}
return label
}
label := versionLabel(globalVer, latestVersion)
if bothScopes {
return label + " (global)"
}
return label
}
// versionLabel formats version with update status.
func versionLabel(installed, latest string) string {
if installed == latest {
return "v" + installed + " (up to date)"
}
return "v" + installed + " (update available)"
}