add ListServices method and ls CLI command

This commit is contained in:
Pavel Sviderski
2024-12-05 17:21:54 +10:00
parent 3ce9c8be04
commit 1fb8a3d7a0
8 changed files with 150 additions and 13 deletions
+7 -7
View File
@@ -10,7 +10,7 @@ import (
"time"
"github.com/spf13/cobra"
client "uncloud/internal/cli"
"uncloud/internal/cli"
)
type inspectOptions struct {
@@ -25,7 +25,7 @@ func NewInspectCommand() *cobra.Command {
Short: "Display detailed information on a service.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*client.CLI)
uncli := cmd.Context().Value("cli").(*cli.CLI)
opts.service = args[0]
return inspect(cmd.Context(), uncli, &opts)
},
@@ -37,19 +37,19 @@ func NewInspectCommand() *cobra.Command {
return cmd
}
func inspect(ctx context.Context, uncli *client.CLI, opts *inspectOptions) error {
cli, err := uncli.ConnectCluster(ctx, opts.cluster)
func inspect(ctx context.Context, uncli *cli.CLI, opts *inspectOptions) error {
client, err := uncli.ConnectCluster(ctx, opts.cluster)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer cli.Close()
defer client.Close()
svc, err := cli.InspectService(ctx, opts.service)
svc, err := client.InspectService(ctx, opts.service)
if err != nil {
return fmt.Errorf("inspect service: %w", err)
}
machines, err := cli.ListMachines(ctx)
machines, err := client.ListMachines(ctx)
if err != nil {
return fmt.Errorf("list machines: %w", err)
}
+53
View File
@@ -0,0 +1,53 @@
package service
import (
"context"
"fmt"
"github.com/spf13/cobra"
"os"
"text/tabwriter"
"uncloud/internal/cli"
)
func NewListCommand() *cobra.Command {
var cluster string
cmd := &cobra.Command{
Use: "ls",
Aliases: []string{"list"},
Short: "List services.",
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
return runList(cmd.Context(), uncli, cluster)
},
}
cmd.Flags().StringVarP(
&cluster, "cluster", "c", "",
"Name of the cluster. (default is the current cluster)",
)
return cmd
}
func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
client, err := uncli.ConnectCluster(ctx, clusterName)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer client.Close()
services, err := client.ListServices(ctx)
if err != nil {
return fmt.Errorf("list services: %w", err)
}
// Print the list of services in a table format.
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
if _, err = fmt.Fprintln(tw, "SERVICE ID\tNAME\tMODE\tREPLICAS"); err != nil {
return fmt.Errorf("write header: %w", err)
}
for _, s := range services {
if _, err = fmt.Fprintf(tw, "%s\t%s\t%s\t%d\n", s.ID, s.Name, s.Mode, len(s.Containers)); err != nil {
return fmt.Errorf("write row: %w", err)
}
}
return tw.Flush()
}
+1
View File
@@ -10,6 +10,7 @@ func NewRootCommand() *cobra.Command {
Short: "Manage services in an Uncloud cluster.",
}
cmd.AddCommand(
NewListCommand(),
NewRunCommand(),
)
return cmd
+3 -3
View File
@@ -68,11 +68,11 @@ func runRun(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
return fmt.Errorf("invalid replication mode: %q", opts.mode)
}
c, err := uncli.ConnectCluster(ctx, opts.cluster)
client, err := uncli.ConnectCluster(ctx, opts.cluster)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer c.Close()
defer client.Close()
spec := api.ServiceSpec{
Container: api.ContainerSpec{
@@ -82,7 +82,7 @@ func runRun(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
Mode: opts.mode,
Name: opts.name,
}
resp, err := c.RunService(ctx, spec)
resp, err := client.RunService(ctx, spec)
if err != nil {
return fmt.Errorf("run service: %w", err)
}