refactor: introduce TimeWarrior struct

This commit is contained in:
Patryk Hegenberg 2025-01-03 11:40:22 +01:00
parent aa3000cce6
commit fec43ea77d

58
main.go
View file

@ -79,7 +79,9 @@ func loadConfig() (Config, error) {
} }
func (a *App) connect() { func (a *App) connect() {
a.runCommand("timew", "start", "work") tw := NewTimeWarrior()
tw.StartWork()
// a.runCommand("timew", "start", "work")
a.wakeWorkstation() a.wakeWorkstation()
sshClient, err := ssh.Dial("tcp", a.cfg.SSHHost+":"+fmt.Sprintf("%v", a.cfg.SSHPort), a.makeSSHClient()) sshClient, err := ssh.Dial("tcp", a.cfg.SSHHost+":"+fmt.Sprintf("%v", a.cfg.SSHPort), a.makeSSHClient())
if err != nil { if err != nil {
@ -205,6 +207,7 @@ func (a *App) startRDPConnection() {
func (a *App) makeChoice() { func (a *App) makeChoice() {
var choice string var choice string
tw := NewTimeWarrior()
form := huh.NewForm( form := huh.NewForm(
huh.NewGroup( huh.NewGroup(
huh.NewSelect[string](). huh.NewSelect[string]().
@ -235,17 +238,16 @@ func (a *App) makeChoice() {
switch choice { switch choice {
case "Start": case "Start":
a.connect() a.connect()
// startSequence()
case "stop Work": case "stop Work":
a.runCommand("timew", "stop", "work") tw.StopWork()
case "start break": case "start break":
a.runCommand("timew", "track", "break") tw.StartBreak()
case "stop break": case "stop break":
a.runCommand("timew", "track", "work") tw.StopBreak()
case "show week summary": case "show week summary":
a.runCommand("timew", "summary", ":week", "work") tw.ShowSummary(":week")
case "show month summary": case "show month summary":
a.runCommand("timew", "summary", ":month", "work") tw.ShowSummary(":month")
case "wake workstation": case "wake workstation":
a.wakeWorkstation() a.wakeWorkstation()
case "connect to jump": case "connect to jump":
@ -255,10 +257,49 @@ func (a *App) makeChoice() {
case "start rdp connection": case "start rdp connection":
a.startRDPConnection() a.startRDPConnection()
case "export": 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 { func (a *App) setupCommands() *cobra.Command {
rootCmd := &cobra.Command{ rootCmd := &cobra.Command{
Use: "work", Use: "work",
@ -330,7 +371,6 @@ func main() {
if err := app.setupCommands().Execute(); err != nil { if err := app.setupCommands().Execute(); err != nil {
log.Fatalf("error executing command: %v", err) log.Fatalf("error executing command: %v", err)
} }
// return
} else { } else {
app.makeChoice() app.makeChoice()
} }