From fec43ea77d538cd0fd9b6ac5100dedaef2bab469 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Fri, 3 Jan 2025 11:40:22 +0100 Subject: [PATCH] refactor: introduce TimeWarrior struct --- main.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index fc40ac7..d1ac7b9 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,9 @@ func loadConfig() (Config, error) { } func (a *App) connect() { - a.runCommand("timew", "start", "work") + tw := NewTimeWarrior() + tw.StartWork() + // a.runCommand("timew", "start", "work") a.wakeWorkstation() sshClient, err := ssh.Dial("tcp", a.cfg.SSHHost+":"+fmt.Sprintf("%v", a.cfg.SSHPort), a.makeSSHClient()) if err != nil { @@ -205,6 +207,7 @@ func (a *App) startRDPConnection() { func (a *App) makeChoice() { var choice string + tw := NewTimeWarrior() form := huh.NewForm( huh.NewGroup( huh.NewSelect[string](). @@ -235,17 +238,16 @@ func (a *App) makeChoice() { switch choice { case "Start": a.connect() - // startSequence() case "stop Work": - a.runCommand("timew", "stop", "work") + tw.StopWork() case "start break": - a.runCommand("timew", "track", "break") + tw.StartBreak() case "stop break": - a.runCommand("timew", "track", "work") + tw.StopBreak() case "show week summary": - a.runCommand("timew", "summary", ":week", "work") + tw.ShowSummary(":week") case "show month summary": - a.runCommand("timew", "summary", ":month", "work") + tw.ShowSummary(":month") case "wake workstation": a.wakeWorkstation() case "connect to jump": @@ -255,10 +257,49 @@ func (a *App) makeChoice() { case "start rdp connection": a.startRDPConnection() case "export": - fmt.Println("Exporting Work times") + tw.ExportSummary() } } +type TimeWarrior struct{} + +func NewTimeWarrior() *TimeWarrior { + return &TimeWarrior{} +} + +func (t *TimeWarrior) runCommand(args ...string) error { + cmd := exec.Command("timew", args...) + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + cmd.Stderr = os.Stderr + return cmd.Run() +} + +func (t *TimeWarrior) StartWork() error { + return t.runCommand("start", "work") +} + +func (t *TimeWarrior) StopWork() error { + return t.runCommand("stop", "work") +} + +func (t *TimeWarrior) StartBreak() error { + return t.runCommand("track", "break") +} + +func (t *TimeWarrior) StopBreak() error { + return t.runCommand("track", "work") +} + +func (t *TimeWarrior) ShowSummary(period string) error { + return t.runCommand("summary", period, "work") +} + +func (t *TimeWarrior) ExportSummary() error { + fmt.Println("Export Timetable") + return nil +} + func (a *App) setupCommands() *cobra.Command { rootCmd := &cobra.Command{ Use: "work", @@ -330,7 +371,6 @@ func main() { if err := app.setupCommands().Execute(); err != nil { log.Fatalf("error executing command: %v", err) } - // return } else { app.makeChoice() }