refactor: perform clean up and add option for direct connection

This commit is contained in:
Patryk Hegenberg 2025-04-02 22:19:32 +02:00
parent 29bdd3a2a4
commit a739a16a5e
3 changed files with 71 additions and 15 deletions

68
cmd.go
View file

@ -4,7 +4,9 @@ import (
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/spf13/cobra"
@ -33,17 +35,69 @@ and other utilities.`,
}
func (a *App) startCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "start",
Short: "Start work: Track time, wake PC, connect",
Long: "Starts time tracking for 'work', attempts to wake the workstation, and sets up SSH tunnels.",
Run: func(cmd *cobra.Command, args []string) {
Short: "Start work: Track time, WOL, setup tunnels, optionally connect or run in background",
Long: `Starts time tracking, attempts WOL, sets up SSH tunnels.
Default behavior: Immediately starts an interactive SSH session to the workstation via the tunnel. The command blocks until this session ends.
Use --background (-b) to keep tunnels running in the background without auto-connecting. Press Ctrl+C to stop background tunnels.`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Starting workday procedures...")
a.connect()
fmt.Println("Workday start initiated. Tunnels are running in the background.")
fmt.Println("Use 'workctl connect rdp' or connect manually.")
sshCon, err := a.connect()
if err != nil {
fmt.Printf("ERROR: Failed to start connections: %v\n", err)
return fmt.Errorf("connection setup failed: %w", err)
}
defer func() {
log.Println("INFO: Closing SSH connection to jump host (defer)...")
if err := sshCon.Close(); err != nil {
log.Printf("WARN: Error closing SSH connection in defer: %v", err)
} else {
log.Println("INFO: SSH connection closed via defer.")
}
}()
if a.flags.StartInBackground {
fmt.Println("\nINFO: Tunnels are active in background.")
fmt.Println(" Connect manually via SSH: ssh -p 2048 <user>@127.0.0.1")
fmt.Println(" Connect manually via RDP: xfreerdp /v:127.0.0.1:6000 ...")
fmt.Println("INFO: Press Ctrl+C to stop tunnels.")
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
fmt.Println("\nINFO: Received interrupt signal. Shutting down background process...")
log.Println("INFO: Received signal, cleanup via defer sshCon.Close() will run.")
if err := a.timeStore.StopTracking(); err != nil {
log.Printf("WARN: Failed to stop time tracking: %v", err)
} else {
log.Println("INFO: Time tracking stopped.")
}
fmt.Println("INFO: Background shutdown complete.")
} else {
fmt.Println("INFO: Automatically connecting to workstation via SSH tunnel...")
a.connectToWorkstation()
fmt.Println("INFO: Workstation SSH session finished.")
log.Println("INFO: Foreground session ended, cleanup via defer sshCon.Close() will run.")
if err := a.timeStore.StopTracking(); err != nil {
log.Printf("WARN: Failed to stop time tracking: %v", err)
} else {
log.Println("INFO: Time tracking stopped.")
}
}
return nil
},
}
cmd.Flags().BoolVarP(&a.flags.StartInBackground, "background", "b", false, "Run tunnels in the background instead of connecting immediately")
return cmd
}
func (a *App) stopCommand() *cobra.Command {