Skip to content

Commit

Permalink
fix:[#427] refuse to remove stack if in use
Browse files Browse the repository at this point in the history
- add check for stack in use before removing subsystem
- add error message for in use stack in locales
  • Loading branch information
jardon committed Nov 14, 2024
1 parent 5c16ee5 commit 8583d99
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,23 @@ func removeStack(cmd *cobra.Command, args []string) error {
return nil
}

subSystems, _ := core.ListSubsystemForStack(stackName)
if len(subSystems) > 0 {
cmdr.Error.Printfln(apx.Trans("stacks.rm.error.inUse"), len(subSystems))
table := core.CreateApxTable(os.Stdout)
table.SetHeader([]string{apx.Trans("subsystems.labels.name"), "Stack", apx.Trans("subsystems.labels.status"), "Pkgs"})
for _, subSystem := range subSystems {
table.Append([]string{
subSystem.Name,
subSystem.Stack.Name,
subSystem.Status,
fmt.Sprintf("%d", len(subSystem.Stack.Packages)),
})
}
table.Render()
return nil
}

force, _ := cmd.Flags().GetBool("force")

if !force {
Expand Down
41 changes: 41 additions & 0 deletions core/subSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,47 @@ func ListSubSystems(includeManaged bool, includeRootFull bool) ([]*SubSystem, er
return subsystems, nil
}

// ListSubsystemForStack returns a list of subsystems for the specified stack.
func ListSubsystemForStack(stackName string) ([]*SubSystem, error) {
dbox, err := NewDbox()
if err != nil {
return nil, err
}

containers, err := dbox.ListContainers(true)
if err != nil {
return nil, err
}

subsystems := []*SubSystem{}
for _, container := range containers {
if _, ok := container.Labels["name"]; !ok {
continue
}

stack, err := LoadStack(stackName)
if err != nil {
log.Printf("Error loading stack %s: %s", stackName, err)
continue
}

internalName := genInternalName(container.Labels["name"])
subsystem := &SubSystem{
InternalName: internalName,
Name: container.Labels["name"],
Stack: stack,
Status: container.Status,
ExportedPrograms: findExported(internalName, container.Labels["name"]),
}

if subsystem.Stack.Name == stack.Name {
subsystems = append(subsystems, subsystem)
}
}

return subsystems, nil
}

func (s *SubSystem) Exec(captureOutput, detachedMode bool, args ...string) (string, error) {
dbox, err := NewDbox()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ stacks:
description: "Remove the specified stack."
error:
noName: "No name specified."
inUse: "The stack is used in %d subsystems:"
info:
askConfirmation: "Are you sure you want to remove '%s'?"
success: "Removed stack '%s'."
Expand Down

0 comments on commit 8583d99

Please sign in to comment.