refactor: perform clean up and add option for direct connection
This commit is contained in:
parent
29bdd3a2a4
commit
a739a16a5e
3 changed files with 71 additions and 15 deletions
9
app.go
9
app.go
|
|
@ -42,7 +42,7 @@ func (a *App) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) connect() {
|
func (a *App) connect() (*SSHConnection, error) { // Rückgabetyp geändert
|
||||||
if err := a.timeStore.StartTracking(TagWork); err != nil {
|
if err := a.timeStore.StartTracking(TagWork); err != nil {
|
||||||
log.Printf("WARN: Failed to start time tracking for '%s': %v", TagWork, err)
|
log.Printf("WARN: Failed to start time tracking for '%s': %v", TagWork, err)
|
||||||
}
|
}
|
||||||
|
|
@ -51,7 +51,7 @@ func (a *App) connect() {
|
||||||
|
|
||||||
sshCon, err := a.newSSHConnection()
|
sshCon, err := a.newSSHConnection()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("FATAL: Failed to establish primary SSH connection: %v", err)
|
return nil, fmt.Errorf("failed to establish primary SSH connection: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("INFO: SSH connection established. Setting up tunnels...")
|
log.Println("INFO: SSH connection established. Setting up tunnels...")
|
||||||
|
|
@ -74,8 +74,9 @@ func (a *App) connect() {
|
||||||
log.Println("INFO: RDP forwarder stopped.")
|
log.Println("INFO: RDP forwarder stopped.")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(500 * time.Millisecond)
|
||||||
log.Println("INFO: Tunnels should be active. You can now connect to localhost:2048 (SSH) or localhost:6000 (RDP).")
|
|
||||||
|
return sshCon, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) runCommand(name string, args ...string) error {
|
func (a *App) runCommand(name string, args ...string) error {
|
||||||
|
|
|
||||||
68
cmd.go
68
cmd.go
|
|
@ -4,7 +4,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -33,17 +35,69 @@ and other utilities.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) startCommand() *cobra.Command {
|
func (a *App) startCommand() *cobra.Command {
|
||||||
return &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Start work: Track time, wake PC, connect",
|
Short: "Start work: Track time, WOL, setup tunnels, optionally connect or run in background",
|
||||||
Long: "Starts time tracking for 'work', attempts to wake the workstation, and sets up SSH tunnels.",
|
Long: `Starts time tracking, attempts WOL, sets up SSH tunnels.
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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...")
|
fmt.Println("Starting workday procedures...")
|
||||||
a.connect()
|
|
||||||
fmt.Println("Workday start initiated. Tunnels are running in the background.")
|
sshCon, err := a.connect()
|
||||||
fmt.Println("Use 'workctl connect rdp' or connect manually.")
|
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 {
|
func (a *App) stopCommand() *cobra.Command {
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,11 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
ShowWeek bool
|
ShowWeek bool
|
||||||
ShowMonth bool
|
ShowMonth bool
|
||||||
ShowExport bool
|
ShowExport bool
|
||||||
ExportName string
|
ExportName string
|
||||||
|
StartInBackground bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig() (Config, error) {
|
func loadConfig() (Config, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue