first commit

This commit is contained in:
Patryk Hegenberg 2025-06-20 12:50:47 +02:00
commit 2c7cef5e5a
2 changed files with 83 additions and 0 deletions

80
main.go Normal file
View file

@ -0,0 +1,80 @@
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
const sessionName = "dev-session"
func main() {
if sessionExists(sessionName) {
handleExistingSession()
} else {
createNewSession()
}
}
func sessionExists(name string) bool {
cmd := exec.Command("tmux", "has-session", "-t", name)
return cmd.Run() == nil
}
func handleExistingSession() {
fmt.Printf("A session named '%s' already exists.\n", sessionName)
fmt.Print("Do you want to attach to it (a), kill it and create a new one (n), or cancel (c)? ")
reader := bufio.NewReader(os.Stdin)
choice, _ := reader.ReadString('\n')
choice = strings.TrimSpace(strings.ToLower(choice))
switch choice {
case "a":
attachToSession()
case "n":
killSession()
createNewSession()
default:
fmt.Println("Operation cancelled.")
}
}
func attachToSession() {
cmd := exec.Command("tmux", "attach-session", "-t", sessionName)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Printf("Error attaching to session: %v\n", err)
}
}
func killSession() {
cmd := exec.Command("tmux", "kill-session", "-t", sessionName)
if err := cmd.Run(); err != nil {
fmt.Printf("Error killing session: %v\n", err)
}
}
func createNewSession() {
cmd := exec.Command("tmux", "new-session", "-d", "-s", sessionName)
if err := cmd.Run(); err != nil {
fmt.Printf("Error starting tmux session: %v\n", err)
return
}
cmd = exec.Command("tmux", "send-keys", "-t", sessionName, "$EDITOR .", "C-m")
if err := cmd.Run(); err != nil {
fmt.Printf("Error opening editor: %v\n", err)
// cmd = exec.Command("tmux", "send-keys", "-t", sessionName, "nvim-lazy .", "C-m")
// if err := cmd.Run(); err != nil {
// fmt.Printf("Error opening LazyVim editor: %v\n", err)
// }
return
}
attachToSession()
}