add service rm CLI command

This commit is contained in:
Pavel Sviderski
2024-12-05 17:32:30 +10:00
parent 1fb8a3d7a0
commit f85b5dd01d
7 changed files with 60 additions and 8 deletions
+2 -2
View File
@@ -21,7 +21,7 @@ func NewListCommand() *cobra.Command {
Short: "List machines in a cluster.",
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
return runList(cmd.Context(), uncli, cluster)
return list(cmd.Context(), uncli, cluster)
},
}
cmd.Flags().StringVarP(
@@ -31,7 +31,7 @@ func NewListCommand() *cobra.Command {
return cmd
}
func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
func list(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)
+1
View File
@@ -45,6 +45,7 @@ func main() {
service.NewRootCommand(),
service.NewInspectCommand(),
service.NewListCommand(),
service.NewRmCommand(),
service.NewRunCommand(),
)
cobra.CheckErr(cmd.Execute())
+2 -2
View File
@@ -27,7 +27,7 @@ func NewInspectCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
opts.service = args[0]
return inspect(cmd.Context(), uncli, &opts)
return inspect(cmd.Context(), uncli, opts)
},
}
cmd.Flags().StringVarP(
@@ -37,7 +37,7 @@ func NewInspectCommand() *cobra.Command {
return cmd
}
func inspect(ctx context.Context, uncli *cli.CLI, opts *inspectOptions) error {
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)
+2 -2
View File
@@ -17,7 +17,7 @@ func NewListCommand() *cobra.Command {
Short: "List services.",
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
return runList(cmd.Context(), uncli, cluster)
return list(cmd.Context(), uncli, cluster)
},
}
cmd.Flags().StringVarP(
@@ -27,7 +27,7 @@ func NewListCommand() *cobra.Command {
return cmd
}
func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
func list(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)
+50
View File
@@ -0,0 +1,50 @@
package service
import (
"context"
"fmt"
"github.com/spf13/cobra"
"uncloud/internal/cli"
)
type rmOptions struct {
services []string
cluster string
}
func NewRmCommand() *cobra.Command {
opts := rmOptions{}
cmd := &cobra.Command{
Use: "rm SERVICE [SERVICE...]",
Aliases: []string{"remove", "delete"},
Short: "Remove one or more services.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
uncli := cmd.Context().Value("cli").(*cli.CLI)
opts.services = args
return rm(cmd.Context(), uncli, opts)
},
}
cmd.Flags().StringVarP(
&opts.cluster, "cluster", "c", "",
"Name of the cluster. (default is the current cluster)",
)
return cmd
}
func rm(ctx context.Context, uncli *cli.CLI, opts rmOptions) error {
client, err := uncli.ConnectCluster(ctx, opts.cluster)
if err != nil {
return fmt.Errorf("connect to cluster: %w", err)
}
defer client.Close()
for _, s := range opts.services {
if err = client.RemoveService(ctx, s); err != nil {
return fmt.Errorf("remove service %q: %w", s, err)
}
fmt.Printf("Service %q removed.\n", s)
}
return nil
}
+1
View File
@@ -11,6 +11,7 @@ func NewRootCommand() *cobra.Command {
}
cmd.AddCommand(
NewListCommand(),
NewRmCommand(),
NewRunCommand(),
)
return cmd
+2 -2
View File
@@ -34,7 +34,7 @@ func NewRunCommand() *cobra.Command {
opts.command = args[1:]
}
return runRun(cmd.Context(), uncli, opts)
return run(cmd.Context(), uncli, opts)
},
}
@@ -61,7 +61,7 @@ func NewRunCommand() *cobra.Command {
return cmd
}
func runRun(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
func run(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
switch opts.mode {
case "", api.ServiceModeReplicated, api.ServiceModeGlobal:
default: