Skip to content

Commit f9e5046

Browse files
committed
feat(internal): add command to stop and remove a container
1 parent e545e54 commit f9e5046

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

cli/tools/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func NewToolsCommand(cmdCli cli.Cli) *cobra.Command {
1616
NewContainerCloneCommand(cmdCli),
1717
NewContainerLogsCommand(cmdCli),
1818
NewContainerRunInContextCommand(cmdCli),
19+
NewContainerRemoveCommand(cmdCli),
1920
)
2021
return cmd
2122
}

cli/tools/container_remove.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright © 2024 thin-edge.io <[email protected]>
3+
*/
4+
package tools
5+
6+
import (
7+
"context"
8+
"errors"
9+
"log/slog"
10+
11+
"github.com/spf13/cobra"
12+
"github.com/thin-edge/tedge-container-plugin/pkg/cli"
13+
"github.com/thin-edge/tedge-container-plugin/pkg/container"
14+
)
15+
16+
type ContainerRemoveCommand struct {
17+
*cobra.Command
18+
19+
CommandContext cli.Cli
20+
21+
// Options
22+
Tail string
23+
Since string
24+
Until string
25+
Timestamps bool
26+
Follow bool
27+
Details bool
28+
}
29+
30+
// NewContainerLogsCommand creates a new container remove command
31+
func NewContainerRemoveCommand(ctx cli.Cli) *cobra.Command {
32+
command := &ContainerLogsCommand{
33+
CommandContext: ctx,
34+
}
35+
cmd := &cobra.Command{
36+
Use: "container-remove [OPTIONS] CONTAINER...",
37+
Short: "Remove a container (stopping if necessary)",
38+
RunE: command.RunE,
39+
Args: cobra.ArbitraryArgs,
40+
SilenceUsage: true,
41+
}
42+
command.Command = cmd
43+
return cmd
44+
}
45+
46+
func (c *ContainerRemoveCommand) RunE(cmd *cobra.Command, args []string) error {
47+
slog.Debug("Executing", "cmd", cmd.CalledAs(), "args", args)
48+
49+
containerCli, err := container.NewContainerClient()
50+
if err != nil {
51+
return err
52+
}
53+
54+
ctx := context.Background()
55+
56+
errs := make([]error, 0)
57+
for _, name := range args {
58+
errs = append(errs, containerCli.StopRemoveContainer(ctx, name))
59+
}
60+
61+
return errors.Join(errs...)
62+
}

0 commit comments

Comments
 (0)