Skip to content

Commit 22a4203

Browse files
authored
Add thv skill builds command to list locally-built OCI artifacts (#4499)
1 parent 27e21d0 commit 22a4203

File tree

16 files changed

+618
-0
lines changed

16 files changed

+618
-0
lines changed

cmd/thv/app/skill_builds.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package app
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"os"
10+
"text/tabwriter"
11+
12+
"github.com/spf13/cobra"
13+
14+
"github.com/stacklok/toolhive/pkg/skills"
15+
)
16+
17+
var skillBuildsFormat string
18+
19+
var skillBuildsCmd = &cobra.Command{
20+
Use: "builds",
21+
Short: "List locally-built skill artifacts",
22+
Long: `List all locally-built OCI skill artifacts stored in the local OCI store.`,
23+
PreRunE: chainPreRunE(
24+
ValidateFormat(&skillBuildsFormat),
25+
),
26+
RunE: skillBuildsCmdFunc,
27+
}
28+
29+
func init() {
30+
skillCmd.AddCommand(skillBuildsCmd)
31+
32+
AddFormatFlag(skillBuildsCmd, &skillBuildsFormat)
33+
}
34+
35+
func skillBuildsCmdFunc(cmd *cobra.Command, _ []string) error {
36+
c := newSkillClient(cmd.Context())
37+
38+
builds, err := c.ListBuilds(cmd.Context())
39+
if err != nil {
40+
return formatSkillError("list builds", err)
41+
}
42+
43+
switch skillBuildsFormat {
44+
case FormatJSON:
45+
if builds == nil {
46+
builds = []skills.LocalBuild{}
47+
}
48+
data, err := json.MarshalIndent(builds, "", " ")
49+
if err != nil {
50+
return fmt.Errorf("failed to marshal JSON: %w", err)
51+
}
52+
fmt.Println(string(data))
53+
default:
54+
if len(builds) == 0 {
55+
fmt.Println("No locally-built skill artifacts found")
56+
return nil
57+
}
58+
printSkillBuildsText(builds)
59+
}
60+
61+
return nil
62+
}
63+
64+
func printSkillBuildsText(builds []skills.LocalBuild) {
65+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
66+
_, _ = fmt.Fprintln(w, "TAG\tDIGEST\tNAME\tVERSION")
67+
68+
for _, b := range builds {
69+
digest := b.Digest
70+
if len(digest) > 19 {
71+
digest = digest[:19] + "..."
72+
}
73+
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
74+
b.Tag,
75+
digest,
76+
b.Name,
77+
b.Version,
78+
)
79+
}
80+
81+
_ = w.Flush()
82+
}

docs/cli/thv_skill.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/cli/thv_skill_builds.md

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/server/docs.go

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/server/swagger.json

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/server/swagger.yaml

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)