Skip to content

Commit 68d2d7c

Browse files
committed
feat: Add delete existing session cmd
1 parent f0afc3e commit 68d2d7c

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

cmd/sessionizer.go

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func runWithHandler(h handler.ISessionHandler, ctx context.Context, cmd *cli.Com
4949
return h.GrabExistingSession(ctx)
5050
} else if len(args) > 0 && args[0] == "create" {
5151
return h.CreateNewProjectSession(ctx)
52+
} else if len(args) > 0 && args[0] == "delete" {
53+
return h.DeleteProjectSession(ctx)
5254
} else if len(args) > 0 {
5355
return ErrNoSuchCmd
5456
} else {

cmd/sessionizer_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func (mh *MockSessionHandler) CreateNewProjectSession(ctx context.Context) error
4040
return nil
4141
}
4242

43+
func (mh *MockSessionHandler) DeleteProjectSession(ctx context.Context) error {
44+
return nil
45+
}
46+
4347
type args struct {
4448
cmd string
4549
}

handler/handler.go

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ISessionHandler interface {
1919
NewSession(ctx context.Context) error
2020
GrabExistingSession(ctx context.Context) error
2121
CreateNewProjectSession(ctx context.Context) error
22+
DeleteProjectSession(ctx context.Context) error
2223
}
2324

2425
type SessionHandler struct{}
@@ -329,6 +330,34 @@ func (sh *SessionHandler) CreateNewProjectSession(ctx context.Context) error {
329330
return nil
330331
}
331332

333+
func (sh *SessionHandler) getSessionInfoToDelete(existingProjects []string) (string, error) {
334+
deleteSessionPrompt := promptui.Select{
335+
Label: "Which tmux session do you want to delete?",
336+
Items: existingProjects,
337+
}
338+
_, deleteSessionName, err := deleteSessionPrompt.Run()
339+
if err != nil {
340+
return "", err
341+
}
342+
return deleteSessionName, nil
343+
}
344+
345+
func (sh *SessionHandler) DeleteProjectSession(ctx context.Context) error {
346+
out, err := exec.Command("tmux", "list-sessions", "-F", "#{session_name}").Output()
347+
if err != nil {
348+
return fmt.Errorf("failed to execute tmux list-sessions cmd: %w", err)
349+
}
350+
sessions := strings.Split(strings.TrimSpace(string(out)), "\n")
351+
deleteSession, err := sh.getSessionInfoToDelete(sessions)
352+
if err != nil {
353+
return fmt.Errorf("failed to get a session name to delete: %w", err)
354+
}
355+
if err = sh.newTmuxCmd("tmux", "kill-session", "-t", deleteSession).Run(); err != nil {
356+
return fmt.Errorf("failed to kill session: %w", err)
357+
}
358+
return nil
359+
}
360+
332361
func (sh *SessionHandler) isInSession() bool {
333362
return len(os.Getenv("TMUX")) > 0
334363
}

0 commit comments

Comments
 (0)