-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathai_plugin_info.go
More file actions
165 lines (145 loc) · 5.37 KB
/
Copy pathai_plugin_info.go
File metadata and controls
165 lines (145 loc) · 5.37 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
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
// SPDX-License-Identifier: Apache-2.0
package app
import (
"encoding/json"
"fmt"
"maps"
"os"
"slices"
"sort"
"strings"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/stacklok/toolhive/pkg/plugins"
)
var (
aiPluginInfoScope string
aiPluginInfoFormat string
aiPluginInfoProjectRoot string
)
var aiPluginInfoCmd = &cobra.Command{
Use: "info [plugin-name]",
Short: "Show details of an AI-tool plugin",
Long: `Display detailed information about a plugin, including metadata, version, and installation status.`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: completeAIPluginNames,
PreRunE: chainPreRunE(
validateAIPluginScope(&aiPluginInfoScope),
ValidateFormat(&aiPluginInfoFormat),
),
RunE: aiPluginInfoCmdFunc,
}
func init() {
aiPluginCmd.AddCommand(aiPluginInfoCmd)
aiPluginInfoCmd.Flags().StringVar(&aiPluginInfoScope, "scope", "", "Filter by scope (user, project)")
AddFormatFlag(aiPluginInfoCmd, &aiPluginInfoFormat)
aiPluginInfoCmd.Flags().StringVar(&aiPluginInfoProjectRoot, "project-root", "", "Project root path for project-scoped plugins")
}
func aiPluginInfoCmdFunc(cmd *cobra.Command, args []string) error {
c := newAIPluginClient(cmd.Context())
projectRoot, err := absProjectRoot(aiPluginInfoProjectRoot)
if err != nil {
return err
}
info, err := c.Info(cmd.Context(), plugins.InfoOptions{
Name: args[0],
Scope: plugins.Scope(aiPluginInfoScope),
ProjectRoot: projectRoot,
})
if err != nil {
return formatAIPluginError("get plugin info", err)
}
switch aiPluginInfoFormat {
case FormatJSON:
data, err := json.MarshalIndent(info, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal JSON: %w", err)
}
fmt.Println(string(data))
default:
printAIPluginInfoText(info)
}
return nil
}
func printAIPluginInfoText(info *plugins.PluginInfo) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintf(w, "Name:\t%s\n", info.Metadata.Name)
_, _ = fmt.Fprintf(w, "Version:\t%s\n", info.Metadata.Version)
switch {
case info.Provenance != nil && info.Provenance.Provisional:
_, _ = fmt.Fprintf(w, "Signed by:\t%s (provisional)\n", info.Provenance.SignerIdentity)
_, _ = fmt.Fprintf(w, "Cert issuer:\t%s\n", info.Provenance.CertIssuer)
case info.Provenance != nil:
_, _ = fmt.Fprintf(w, "Signed by:\t%s\n", info.Provenance.SignerIdentity)
_, _ = fmt.Fprintf(w, "Cert issuer:\t%s\n", info.Provenance.CertIssuer)
case info.Unsigned:
_, _ = fmt.Fprintf(w, "Signed by:\t(unsigned — explicit exception)\n")
case info.TrustUnrecorded:
_, _ = fmt.Fprintf(w, "Signed by:\t(trust unrecorded — run 'thv ai-plugin sync')\n")
}
_, _ = fmt.Fprintf(w, "Description:\t%s\n", info.Metadata.Description)
if s := info.InstalledPlugin; s != nil {
_, _ = fmt.Fprintf(w, "Scope:\t%s\n", s.Scope)
_, _ = fmt.Fprintf(w, "Status:\t%s\n", s.Status)
_, _ = fmt.Fprintf(w, "Reference:\t%s\n", s.Reference)
_, _ = fmt.Fprintf(w, "Installed At:\t%s\n", s.InstalledAt.Format("2006-01-02 15:04:05"))
if len(s.Clients) > 0 {
_, _ = fmt.Fprintf(w, "Clients:\t%s\n", strings.Join(s.Clients, ", "))
}
if len(s.Components) > 0 {
managed, declared := splitComponentInventory(s.Components)
if len(managed) > 0 {
_, _ = fmt.Fprintf(w, "Components:\t%s\n", formatComponentInventory(managed))
}
if len(declared) > 0 {
_, _ = fmt.Fprintf(w, "Declared (not managed by ToolHive):\t%s\n", formatComponentInventory(declared))
}
}
}
if len(info.UnmaterializedComponents) > 0 {
_, _ = fmt.Fprintln(w, "\nNot activated on this client (declared by the plugin, but the client does not load them):")
for _, clientType := range slices.Sorted(maps.Keys(info.UnmaterializedComponents)) {
types := info.UnmaterializedComponents[clientType]
labels := make([]string, 0, len(types))
for _, t := range types {
labels = append(labels, string(t))
}
_, _ = fmt.Fprintf(w, " %s:\t%s\n", clientType, strings.Join(labels, ", "))
}
}
if len(info.ProjectScopeDegradedClients) > 0 {
degraded := append([]string(nil), info.ProjectScopeDegradedClients...)
sort.Strings(degraded)
_, _ = fmt.Fprintf(w, "\nInstalled at user scope instead of project scope (client cannot scope per-project):\t%s\n",
strings.Join(degraded, ", "))
}
_ = w.Flush()
}
// formatComponentInventory renders a ComponentInventory (map[string]int) as a
// sorted, space-separated "key=count" sequence for deterministic output.
func formatComponentInventory(inv plugins.ComponentInventory) string {
keys := slices.Sorted(maps.Keys(inv))
parts := make([]string, 0, len(keys))
for _, k := range keys {
parts = append(parts, fmt.Sprintf("%s=%d", k, inv[k]))
}
return strings.Join(parts, " ")
}
// splitComponentInventory separates managed components (commands, agents,
// skills, hooks) from declared-but-unmanaged components (mcpServers,
// lspServers). MCP/LSP servers are declared in the plugin manifest but are
// not materialized or lifecycle-managed by ToolHive.
func splitComponentInventory(inv plugins.ComponentInventory) (managed, declared plugins.ComponentInventory) {
managed = plugins.ComponentInventory{}
declared = plugins.ComponentInventory{}
for k, v := range inv {
switch k {
case string(plugins.ComponentMCP), string(plugins.ComponentLSP):
declared[k] = v
default:
managed[k] = v
}
}
return managed, declared
}