diff --git a/cmd/uncloud/machine/list.go b/cmd/uncloud/machine/list.go index 04aa474a..aaed211b 100644 --- a/cmd/uncloud/machine/list.go +++ b/cmd/uncloud/machine/list.go @@ -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) diff --git a/cmd/uncloud/main.go b/cmd/uncloud/main.go index 7affd4f1..95f16a9c 100644 --- a/cmd/uncloud/main.go +++ b/cmd/uncloud/main.go @@ -45,6 +45,7 @@ func main() { service.NewRootCommand(), service.NewInspectCommand(), service.NewListCommand(), + service.NewRmCommand(), service.NewRunCommand(), ) cobra.CheckErr(cmd.Execute()) diff --git a/cmd/uncloud/service/inspect.go b/cmd/uncloud/service/inspect.go index 989b7cc2..11973633 100644 --- a/cmd/uncloud/service/inspect.go +++ b/cmd/uncloud/service/inspect.go @@ -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) diff --git a/cmd/uncloud/service/list.go b/cmd/uncloud/service/list.go index d49e482e..feaa3a50 100644 --- a/cmd/uncloud/service/list.go +++ b/cmd/uncloud/service/list.go @@ -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) diff --git a/cmd/uncloud/service/rm.go b/cmd/uncloud/service/rm.go new file mode 100644 index 00000000..85aee389 --- /dev/null +++ b/cmd/uncloud/service/rm.go @@ -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 +} diff --git a/cmd/uncloud/service/root.go b/cmd/uncloud/service/root.go index df430d2b..df169f01 100644 --- a/cmd/uncloud/service/root.go +++ b/cmd/uncloud/service/root.go @@ -11,6 +11,7 @@ func NewRootCommand() *cobra.Command { } cmd.AddCommand( NewListCommand(), + NewRmCommand(), NewRunCommand(), ) return cmd diff --git a/cmd/uncloud/service/run.go b/cmd/uncloud/service/run.go index c82a3611..3bca49c0 100644 --- a/cmd/uncloud/service/run.go +++ b/cmd/uncloud/service/run.go @@ -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: