Skip to content

Commit

Permalink
feat: clear command
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetozer committed Jun 28, 2024
1 parent 8b84d4e commit 3501209
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
19 changes: 19 additions & 0 deletions pkg/cmd/clear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/ahmetozer/sandal/pkg/config"
"github.com/ahmetozer/sandal/pkg/container"
)

func clear(args []string) error {
conts, _ := config.AllContainers()
for _, c := range conts {
if !c.Remove {
continue
}
if !container.IsRunning(&c) {
deRunContainer(&c)
}
}
return nil
}
3 changes: 3 additions & 0 deletions pkg/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func Main() {
executeSubCommand(deamon)
case "cmd":
executeSubCommand(cmd)
case "clear":
executeSubCommand(clear)
case "help":
subCommandsHelp()
default:
Expand Down Expand Up @@ -71,6 +73,7 @@ func subCommandsHelp() {
inspect - Get configuration file
cmd - Get execution command
daemon - Start sandal daemon
clear - Clear unused containers
help - Show this help`)

fmt.Printf("\n\nVersion: %s\n", BuildVersion)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func rm(args []string) error {
return fmt.Errorf("container %s is running, please stop it first", c.Name)
}

c.Keep = false
c.Remove = true
deRunContainer(&c)

}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func run(args []string) error {
// f.StringVar(&c.RootfsDir, "rootfs", "", "rootfs directory")
f.BoolVar(&c.ReadOnly, "ro", false, "read only rootfs")

f.BoolVar(&c.Keep, "keep", false, "do not remove container files on exit")
f.BoolVar(&c.Remove, "rm", false, "remove container files on exit")

f.BoolVar(&c.Startup, "startup", false, "run container at startup by sandal daemon")

Expand Down Expand Up @@ -139,7 +139,7 @@ func Start(c *config.Config, HostIface, PodIface config.NetIface) error {
// Starting proccess
exitCode, err = container.Start(c, c.PodArgs)

if c.Keep {
if !c.Remove {
c.Status = fmt.Sprintf("exit %d", exitCode)
if err != nil {
c.Status = fmt.Sprintf("err %v", err)
Expand All @@ -163,7 +163,7 @@ func deRunContainer(c *config.Config) {
net.Clear(c)
}

if !c.Keep {
if c.Remove {
if err := os.RemoveAll(c.ContDir()); err != nil {
slog.Debug("removeall", slog.String("err", err.Error()))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Config struct {
SquashfsFile string
RootfsDir string
ReadOnly bool
Keep bool
Remove bool
EnvAll bool
Background bool
Startup bool
Expand Down

0 comments on commit 3501209

Please sign in to comment.