Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions cmd/grounds/commands/bundle/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package bundle

import (
"context"
"net/http"
"os"
"time"

"github.com/spf13/cobra"

"github.com/groundsgg/grounds-cli/internal/api"
"github.com/groundsgg/grounds-cli/internal/auth"
"github.com/groundsgg/grounds-cli/internal/config"
)

func NewBundleCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "bundle",
Short: "Inspect available platform-test bundles",
}
cmd.AddCommand(newList(), newShow())
return cmd
}

// Mirrors cluster/cluster.go's buildClient.
func buildClient(_ context.Context, cmd *cobra.Command) (*api.Client, error) {
cfg, err := config.Load("")
if err != nil {
return nil, err
}
ts := api.NewEnvTokenSource()
if ts == nil {
ts = &auth.FileTokenSource{
Store: auth.NewStore(cfg.Dir),
Device: defaultDeviceClient(),
}
}
c := api.New(cfg.APIURL, ts)
if p := projectIDFlag(cmd); p != "" {
c.ProjectID = p
}
return c, nil
}

func projectIDFlag(cmd *cobra.Command) string {
if cmd != nil {
if p, _ := cmd.Flags().GetString("project"); p != "" {
return p
}
}
return os.Getenv("GROUNDS_PROJECT")
}

func defaultDeviceClient() *auth.DeviceClient {
return &auth.DeviceClient{
Issuer: "https://account.grounds.gg/realms/grounds",
ClientID: "grounds-cli",
HTTP: &http.Client{Timeout: 30 * time.Second},
}
}
49 changes: 49 additions & 0 deletions cmd/grounds/commands/bundle/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package bundle

import (
"context"
"fmt"
"text/tabwriter"

"github.com/spf13/cobra"
)

func newList() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List released platform-bundle versions",
Long: `Lists released library-platform-bundle versions, newest first.
Drafts and prereleases are filtered out. The version with (latest) is
the same one 'grounds cluster up --bundle main' would track today.`,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
c, err := buildClient(ctx, cmd)
if err != nil {
return err
}
releases, err := c.ListBundleReleases(ctx)
if err != nil {
return err
}
if len(releases) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "no released bundles found")
return nil
}
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "VERSION\tPUBLISHED\tURL")
for _, r := range releases {
marker := ""
if r.IsLatest {
marker = " (latest)"
}
fmt.Fprintf(w, "%s%s\t%s\t%s\n",
r.Version, marker,
r.PublishedAt.Format("2006-01-02"),
r.HtmlURL,
)
}
return w.Flush()
},
}
return cmd
}
68 changes: 68 additions & 0 deletions cmd/grounds/commands/bundle/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package bundle

import (
"context"
"fmt"
"sort"
"text/tabwriter"

"github.com/spf13/cobra"
)

func newShow() *cobra.Command {
cmd := &cobra.Command{
Use: "show <ref>",
Short: "Show the components in a bundle",
Long: `Fetches the parsed bundle.yaml at the given ref and prints the
component table. <ref> accepts the same shapes as 'cluster up --bundle':
semver, "v…", the full release tag, or "main" for the latest commit.

Examples:

grounds bundle show 0.4.0
grounds bundle show main`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
c, err := buildClient(ctx, cmd)
if err != nil {
return err
}
b, err := c.GetBundle(ctx, args[0])
if err != nil {
return err
}
out := cmd.OutOrStdout()
fmt.Fprintf(out, "Bundle: %s\n", b.Metadata.Version)
if b.Metadata.Description != "" {
fmt.Fprintf(out, "About: %s\n", b.Metadata.Description)
}
fmt.Fprintln(out)
fmt.Fprintln(out, "Components:")
w := tabwriter.NewWriter(out, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, " NAME\tTYPE\tIMAGE\tCHART\tDEVSPACE")

names := make([]string, 0, len(b.Components))
for k := range b.Components {
names = append(names, k)
}
sort.Strings(names)
for _, name := range names {
comp := b.Components[name]
devspace := "-"
if comp.Devspace != nil && comp.Devspace.Workflow != "" {
devspace = comp.Devspace.Workflow
}
marker := name
if comp.Optional {
marker = name + " (optional)"
}
fmt.Fprintf(w, " %s\t%s\t%s:%s\t%s\t%s\n",
marker, comp.Type, comp.Image, comp.Version, comp.Chart.Version, devspace,
)
}
return w.Flush()
},
}
return cmd
}
2 changes: 2 additions & 0 deletions cmd/grounds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/groundsgg/grounds-cli/cmd/grounds/commands"
"github.com/groundsgg/grounds-cli/cmd/grounds/commands/bundle"
"github.com/groundsgg/grounds-cli/cmd/grounds/commands/cluster"
"github.com/groundsgg/grounds-cli/cmd/grounds/commands/logs"
"github.com/groundsgg/grounds-cli/cmd/grounds/commands/preview"
Expand All @@ -29,6 +30,7 @@ func main() {
root.AddCommand(commands.NewLogoutCommand())
root.AddCommand(commands.NewDoctorCommand())
root.AddCommand(commands.NewInitCommand())
root.AddCommand(bundle.NewBundleCommand())
root.AddCommand(cluster.NewClusterCommand())
root.AddCommand(logs.NewLogsCommand())
root.AddCommand(push.NewPushCommand())
Expand Down
73 changes: 73 additions & 0 deletions internal/api/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package api

import (
"context"
"net/http"
"net/url"
"time"
)

// BundleRelease mirrors one entry of GET /v1/bundle/releases.
type BundleRelease struct {
Version string `json:"version"`
PublishedAt time.Time `json:"publishedAt"`
HtmlURL string `json:"htmlUrl"`
IsLatest bool `json:"isLatest"`
}

type bundleReleasesResponse struct {
Releases []BundleRelease `json:"releases"`
}

// ListBundleReleases returns released library-platform-bundle versions
// (drafts + prereleases filtered out, newest-first).
func (c *Client) ListBundleReleases(ctx context.Context) ([]BundleRelease, error) {
out := &bundleReleasesResponse{}
if err := c.doRequest(ctx, http.MethodGet, "/v1/bundle/releases", nil, out); err != nil {
return nil, err
}
return out.Releases, nil
}

// BundleSummary is the parsed bundle.yaml as JSON. The CLI doesn't
// need every field — Components is the most useful surface; the rest
// is opaque-passthrough so we don't have to keep this in sync with
// every bundle-schema bump.
type BundleSummary struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata BundleSummaryMetadata `json:"metadata"`
Shared map[string]any `json:"shared"`
Components map[string]BundleSummaryComponent `json:"components"`
}

type BundleSummaryMetadata struct {
Version string `json:"version"`
Description string `json:"description,omitempty"`
}

type BundleSummaryComponent struct {
Type string `json:"type"`
Image string `json:"image"`
Version string `json:"version"`
Optional bool `json:"optional,omitempty"`
Chart struct {
URL string `json:"url"`
Version string `json:"version"`
} `json:"chart"`
Devspace *struct {
Workflow string `json:"workflow,omitempty"`
} `json:"devspace,omitempty"`
}

// GetBundle returns the parsed bundle.yaml at the given ref. `ref`
// accepts the same shapes loadBundle does — semver, "v…", the full
// tag, or "main".
func (c *Client) GetBundle(ctx context.Context, ref string) (*BundleSummary, error) {
out := &BundleSummary{}
path := "/v1/bundle/" + url.PathEscape(ref)
if err := c.doRequest(ctx, http.MethodGet, path, nil, out); err != nil {
return nil, err
}
return out, nil
}
Loading