Skip to content

Commit

Permalink
implement rm command for subsystems #242
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Jun 23, 2023
1 parent 1601a61 commit 0914133
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
23 changes: 10 additions & 13 deletions cmd/subsyStems.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,20 @@ func NewSubSystemsCommand() *cmdr.Command {
)

// Rm subcommand
/*
rmCmd := cmdr.NewCommand(
"rm",
apx.Trans("rmSubSystem.long"),
apx.Trans("rmSubSystem.short"),
rmSubSystem,
)
rmCmd.Example = "apx subsystems rm --name my-subsystem"
rmCmd.Args = cobra.ExactArgs(1)
*/

rmCmd := cmdr.NewCommand(
"rm",
apx.Trans("rmSubSystem.long"),
apx.Trans("rmSubSystem.short"),
rmSubSystem,
)
rmCmd.Example = "apx subsystems rm --name my-subsystem"
rmCmd.Args = cobra.ExactArgs(1)

// Add subcommands to subsystems
cmd.AddCommand(listCmd)
cmd.AddCommand(newCmd)
// cmd.AddCommand(rmCmd, rmCmd)
cmd.AddCommand(rmCmd, rmCmd)

return cmd
}
Expand Down Expand Up @@ -156,7 +155,6 @@ func newSubSystem(cmd *cobra.Command, args []string) error {
return nil
}

/*
func rmSubSystem(cmd *cobra.Command, args []string) error {
subSystemName := args[0]

Expand All @@ -174,4 +172,3 @@ func rmSubSystem(cmd *cobra.Command, args []string) error {

return nil
}
*/
23 changes: 23 additions & 0 deletions core/dbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,29 @@ func (d *dbox) ListContainers() ([]dboxContainer, error) {
return containers, nil
}

func (d *dbox) GetContainer(name string) (*dboxContainer, error) {
containers, err := d.ListContainers()
if err != nil {
return nil, err
}

for _, container := range containers {
if container.Name == name {
return &container, nil
}
}

return nil, errors.New("container not found")
}

func (d *dbox) ContainerDelete(name string) error {
_, err := d.RunCommand("rm", []string{
"--name", name,
"--force",
}, []string{}, false, false)
return err
}

func (d *dbox) CreateContainer(name string, image string, additionalPackages []string, labels map[string]string) error {
args := []string{
"--image", image,
Expand Down
33 changes: 33 additions & 0 deletions core/subSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ func (s *SubSystem) Create() error {
return nil
}

func LoadSubSystem(name string) (*SubSystem, error) {
dbox, err := NewDbox()
if err != nil {
return nil, err
}

container, err := dbox.GetContainer(fmt.Sprintf("apx-%s", name))
if err != nil {
return nil, err
}

stack, err := LoadStack(container.Labels["stack"])
if err != nil {
return nil, err
}

return &SubSystem{
InternalName: container.Name,
Name: container.Labels["name"],
Stack: stack,
Status: container.Status,
}, nil
}

func ListSubSystems() ([]*SubSystem, error) {
dbox, err := NewDbox()
if err != nil {
Expand Down Expand Up @@ -105,3 +129,12 @@ func (s *SubSystem) Enter() error {

return dbox.ContainerEnter(s.InternalName)
}

func (s *SubSystem) Remove() error {
dbox, err := NewDbox()
if err != nil {
return err
}

return dbox.ContainerDelete(s.InternalName)
}

0 comments on commit 0914133

Please sign in to comment.