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() {
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()
}