diff --git a/app.go b/app.go index 1c4d18b..afde762 100644 --- a/app.go +++ b/app.go @@ -42,8 +42,8 @@ func (a *App) Close() error { return nil } -func (a *App) connect() (*SSHConnection, error) { // Rückgabetyp geändert - if err := a.timeStore.StartTracking(TagWork); err != nil { +func (a *App) connect(withoutTimew bool) (*SSHConnection, error) { // Rückgabetyp geändert + if err := a.timeStore.StartTracking(TagWork, withoutTimew); err != nil { slog.Warn(fmt.Sprintf("Failed to start time tracking for '%s': %v", TagWork, err)) } @@ -189,20 +189,20 @@ func (a *App) makeChoice() { switch choice { case "start work": - a.connect() + a.connect(withoutTimew) case "stop work": - if err := a.timeStore.StopTracking(); err != nil { + if err := a.timeStore.StopTracking(withoutTimew); err != nil { slog.Error(fmt.Sprintf("Failed to stop time tracking: %v", err)) } if err := a.killForwardings(); err != nil { slog.Warn(fmt.Sprintf("Could not kill all forwardings: %v", err)) } case "start break": - if err := a.timeStore.StartTracking(TagBreak); err != nil { + if err := a.timeStore.StartTracking(TagBreak, withoutTimew); err != nil { slog.Error(fmt.Sprintf("Failed to start break tracking: %v", err)) } case "stop break": - if err := a.timeStore.StartTracking(TagWork); err != nil { + if err := a.timeStore.StartTracking(TagWork, withoutTimew); err != nil { slog.Error(fmt.Sprintf("Failed to stop break (start work): %v", err)) } case "show day summary": diff --git a/cmd.go b/cmd.go index afdae17..7180482 100644 --- a/cmd.go +++ b/cmd.go @@ -12,6 +12,8 @@ import ( "github.com/spf13/cobra" ) +var withoutTimew bool + func (a *App) setupCommands() *cobra.Command { rootCmd := &cobra.Command{ Use: "workctl", @@ -44,7 +46,7 @@ Use --background (-b) to keep tunnels running in the background without auto-con RunE: func(cmd *cobra.Command, args []string) error { fmt.Println("Starting workday procedures...") - sshCon, err := a.connect() + sshCon, err := a.connect(withoutTimew) if err != nil { fmt.Printf("ERROR: Failed to start connections: %v\n", err) return fmt.Errorf("connection setup failed: %w", err) @@ -71,7 +73,7 @@ Use --background (-b) to keep tunnels running in the background without auto-con fmt.Println("\nINFO: Received interrupt signal. Shutting down background process...") slog.Info("Received signal, cleanup via defer sshCon.Close() will run.") - if err := a.timeStore.StopTracking(); err != nil { + if err := a.timeStore.StopTracking(withoutTimew); err != nil { slog.Warn(fmt.Sprintf("Failed to stop time tracking: %v", err)) } else { slog.Info("Time tracking stopped.") @@ -84,7 +86,7 @@ Use --background (-b) to keep tunnels running in the background without auto-con fmt.Println("Workstation SSH session finished.") slog.Info("Foreground session ended, cleanup via defer sshCon.Close() will run.") - if err := a.timeStore.StopTracking(); err != nil { + if err := a.timeStore.StopTracking(withoutTimew); err != nil { slog.Warn(fmt.Sprintf("Failed to stop time tracking: %v", err)) } else { slog.Info("Time tracking stopped.") @@ -96,18 +98,19 @@ Use --background (-b) to keep tunnels running in the background without auto-con } cmd.Flags().BoolVarP(&a.flags.StartInBackground, "background", "b", false, "Run tunnels in the background instead of connecting immediately") + cmd.Flags().BoolVarP(&withoutTimew, "timew", "t", false, "Set this flag if you dont want to use Timewarrior Timestorage as well") return cmd } func (a *App) stopCommand() *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "stop", Short: "Stop work: Stop time tracking, kill tunnels", Long: "Stops the current time tracking entry and attempts to kill active SSH tunnels.", Run: func(cmd *cobra.Command, args []string) { fmt.Println("Stopping workday procedures...") - if err := a.timeStore.StopTracking(); err != nil { + if err := a.timeStore.StopTracking(withoutTimew); err != nil { slog.Error(fmt.Sprintf("Failed to stop time tracking: %v", err)) } else { fmt.Println("Time tracking stopped.") @@ -122,6 +125,8 @@ func (a *App) stopCommand() *cobra.Command { fmt.Println("Workday stop procedures finished.") }, } + cmd.Flags().BoolVarP(&withoutTimew, "timew", "t", false, "Set this flag if you dont want to use Timewarrior Timestorage as well") + return cmd } func (a *App) trackCommand() *cobra.Command { @@ -159,7 +164,7 @@ This also stops any currently running timer.`, default: fmt.Printf("Attempting to start tracking interval '%s'...\n", tag) - if err := a.timeStore.StartTracking(tag); err != nil { + if err := a.timeStore.StartTracking(tag, withoutTimew); err != nil { slog.Error(fmt.Sprintf("Failed to start tracking '%s': %v", tag, err)) return fmt.Errorf("could not start tracking '%s': %w", tag, err) } @@ -173,7 +178,7 @@ This also stops any currently running timer.`, Short: "Start tracking 'break'", RunE: func(cmd *cobra.Command, args []string) error { fmt.Println("Starting break...") - if err := a.timeStore.StartTracking(TagBreak); err != nil { + if err := a.timeStore.StartTracking(TagBreak, withoutTimew); err != nil { slog.Error(fmt.Sprintf("Failed to start break tracking: %v", err)) return fmt.Errorf("could not start break: %w", err) } diff --git a/helpers.go b/helpers.go new file mode 100644 index 0000000..c12c05c --- /dev/null +++ b/helpers.go @@ -0,0 +1,18 @@ +package main + +import ( + "log/slog" + "os" + "os/exec" +) + +func runCommand(name string, args ...string) { + cmd := exec.Command(name, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + err := cmd.Run() + if err != nil { + slog.Error("Command execution error", "command", name, "args", args, "error", err) + } +} diff --git a/store.go b/store.go index 12e0b94..00565c4 100644 --- a/store.go +++ b/store.go @@ -113,7 +113,7 @@ func (ts *TimeStore) stopCurrentEntry(now time.Time) (bool, error) { return rowsAffected > 0, nil } -func (ts *TimeStore) StartTracking(tag string) error { +func (ts *TimeStore) StartTracking(tag string, withoutTimew bool) error { if tag == "" { return fmt.Errorf("cannot start tracking with an empty tag") } @@ -126,6 +126,9 @@ func (ts *TimeStore) StartTracking(tag string) error { if stopped { slog.Info("Stopped previous time entry.") } + if !withoutTimew { + runCommand("timew", "start", "work") + } query := `INSERT INTO time_entries (tag, start_time, end_time) VALUES (?, ?, NULL);` _, err = ts.db.Exec(query, tag, now) @@ -136,12 +139,15 @@ func (ts *TimeStore) StartTracking(tag string) error { return nil } -func (ts *TimeStore) StopTracking() error { +func (ts *TimeStore) StopTracking(withoutTimew bool) error { now := time.Now() stopped, err := ts.stopCurrentEntry(now) if err != nil { return err } + if !withoutTimew { + runCommand("timew", "stop", "work") + } if stopped { slog.Info(fmt.Sprintf("Stopped tracking at %s", now.Format(time.RFC3339))) } else {