@@ -10,6 +10,7 @@ import (
1010 "net/url"
1111 "os"
1212 "path/filepath"
13+ "regexp"
1314 "sort"
1415 "strings"
1516 "time"
@@ -181,6 +182,8 @@ func runValidation(recipe playground.Recipe) error {
181182 return nil
182183}
183184
185+ var sessionNameRegex = regexp .MustCompile (`^[a-z0-9]+(-[a-z0-9]+)*$` )
186+
184187func shutDownCmdFunc (cmdName string ) func (cmd * cobra.Command , args []string ) error {
185188 var keepResources bool
186189 switch cmdName {
@@ -193,11 +196,23 @@ func shutDownCmdFunc(cmdName string) func(cmd *cobra.Command, args []string) err
193196 panic ("setting up shut down func for unknown cmd: " + cmdName )
194197 }
195198 return func (cmd * cobra.Command , args []string ) error {
196- sessions := args
197- if len (sessions ) == 0 {
199+ if len (args ) == 0 {
198200 return fmt .Errorf ("please specify at least one session name or 'all' to %s all sessions" , cmdName )
199201 }
200- if len (sessions ) == 1 && sessions [0 ] == "all" {
202+ for _ , arg := range args {
203+ if arg == "all" {
204+ if len (args ) != 1 {
205+ return fmt .Errorf ("'all' cannot be combined with session names" )
206+ }
207+ continue
208+ }
209+ if ! sessionNameRegex .MatchString (arg ) {
210+ return fmt .Errorf ("invalid session name %q: must be lowercase letters/digits separated by hyphens (e.g., happy-dolphin)" , arg )
211+ }
212+ }
213+ isAll := len (args ) == 1 && args [0 ] == "all"
214+ sessions := args
215+ if isAll {
201216 var err error
202217 sessions , err = playground .GetLocalSessions ()
203218 if err != nil {
@@ -210,6 +225,27 @@ func shutDownCmdFunc(cmdName string) func(cmd *cobra.Command, args []string) err
210225 return err
211226 }
212227 }
228+ if cmdName != "clean" {
229+ return nil
230+ }
231+ sessionsDir , err := utils .GetSessionsDir ()
232+ if err != nil {
233+ return err
234+ }
235+ if isAll {
236+ err = os .RemoveAll (sessionsDir )
237+ if err != nil {
238+ slog .Warn ("failed to remove sessions directory" , "error" , err )
239+ }
240+ return nil
241+ }
242+ for _ , session := range sessions {
243+ fullSessionDir := filepath .Join (sessionsDir , session )
244+ err = os .RemoveAll (fullSessionDir )
245+ if err != nil {
246+ slog .Warn ("failed to remove session directory" , "session" , fullSessionDir , "error" , err )
247+ }
248+ }
213249 return nil
214250 }
215251}
0 commit comments