Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -180,6 +181,8 @@ func runValidation(recipe playground.Recipe) error {
return nil
}

var sessionNameRegex = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`)

func shutDownCmdFunc(cmdName string) func(cmd *cobra.Command, args []string) error {
var keepResources bool
switch cmdName {
Expand All @@ -192,11 +195,23 @@ func shutDownCmdFunc(cmdName string) func(cmd *cobra.Command, args []string) err
panic("setting up shut down func for unknown cmd: " + cmdName)
}
return func(cmd *cobra.Command, args []string) error {
sessions := args
if len(sessions) == 0 {
if len(args) == 0 {
return fmt.Errorf("please specify at least one session name or 'all' to %s all sessions", cmdName)
}
if len(sessions) == 1 && sessions[0] == "all" {
for _, arg := range args {
if arg == "all" {
if len(args) != 1 {
return fmt.Errorf("'all' cannot be combined with session names")
}
continue
}
if !sessionNameRegex.MatchString(arg) {
return fmt.Errorf("invalid session name %q: must be lowercase letters/digits separated by hyphens (e.g., happy-dolphin)", arg)
}
}
isAll := len(args) == 1 && args[0] == "all"
sessions := args
if isAll {
var err error
sessions, err = playground.GetLocalSessions()
if err != nil {
Expand All @@ -209,6 +224,27 @@ func shutDownCmdFunc(cmdName string) func(cmd *cobra.Command, args []string) err
return err
}
}
if cmdName != "clean" {
Comment thread
canercidam marked this conversation as resolved.
Comment thread
canercidam marked this conversation as resolved.
return nil
}
sessionsDir, err := utils.GetSessionsDir()
if err != nil {
return err
}
if isAll {
err = os.RemoveAll(sessionsDir)
if err != nil {
slog.Warn("failed to remove sessions directory", "error", err)
}
return nil
}
for _, session := range sessions {
fullSessionDir := filepath.Join(sessionsDir, session)
Comment thread
canercidam marked this conversation as resolved.
err = os.RemoveAll(fullSessionDir)
if err != nil {
slog.Warn("failed to remove session directory", "session", fullSessionDir, "error", err)
}
}
return nil
}
}
Expand Down