work/cmd.go

126 lines
3.5 KiB
Go

package main
import (
"fmt"
"log"
"github.com/spf13/cobra"
)
func (a *App) setupCommands() *cobra.Command {
rootCmd := &cobra.Command{
Use: "work",
Short: "Fast work interactions",
Long: `A CLI tool to perform basic work cli tasks.`,
}
rootCmd.AddCommand(a.startCommand())
rootCmd.AddCommand(a.stopCommand())
rootCmd.AddCommand(a.showCommand())
return rootCmd
}
func (a *App) startCommand() *cobra.Command {
return &cobra.Command{
Use: "start",
Short: "start work",
Long: "command to start the work day",
Run: func(cmd *cobra.Command, args []string) {
a.connect()
},
}
}
// cmd.go (Auszüge)
func (a *App) stopCommand() *cobra.Command {
return &cobra.Command{
// ... Use, Short, Long ...
RunE: func(cmd *cobra.Command, args []string) error { // RunE verwenden für Fehlerbehandlung
// tw := NewTimeWarrior() // Entfernen
if err := a.tracker.StopWork(); err != nil {
log.Printf("Error stopping work: %v", err)
// return err // Fehler zurückgeben, nicht os.Exit
}
// Das Beenden der Forwardings ist hier richtig platziert
if err := a.killForwardings(); err != nil {
log.Printf("error stopping port forwarding: %v", err)
// Ggf. auch hier Fehler zurückgeben oder nur loggen
}
// Kein os.Exit(0) hier!
return nil // Erfolg signalisieren
},
}
}
func (a *App) showCommand() *cobra.Command {
cmd := &cobra.Command{
// ... Use, Short, Long ...
RunE: func(cmd *cobra.Command, args []string) error { // RunE verwenden
// tw := NewTimeWarrior() // Entfernen
var err error
if a.flags.ShowExport {
err = a.tracker.ExportSummary(a.flags.ExportName)
} else if a.flags.ShowWeek { // else if, falls nur eine Aktion gewünscht
err = a.tracker.ShowSummary(":week")
} else if a.flags.ShowMonth {
err = a.tracker.ShowSummary(":month")
} else {
// Standardaktion, wenn kein Flag gesetzt ist? Oder Fehler?
fmt.Println("Please specify a flag: --week, --month, or --export")
}
if err != nil {
log.Printf("Error in show command: %v", err)
return err // Fehler zurückgeben
}
// Kein os.Exit(0)!
return nil
},
}
// Flags bleiben gleich...
// return cmd
// }
// func (a *App) stopCommand() *cobra.Command {
// return &cobra.Command{
// Use: "stop",
// Short: "stop work",
// Long: "command to stop the work day",
// Run: func(cmd *cobra.Command, args []string) {
// tw := NewTimeWarrior()
// tw.StopWork()
// if err := a.killForwardings(); err != nil {
// log.Printf("error stoping port forwarding: %v", err)
// }
// os.Exit(0)
// },
// }
// }
//
// func (a *App) showCommand() *cobra.Command {
// cmd := &cobra.Command{
// Use: "show",
// Short: "show timetracking",
// Long: "show different timetracking",
// Run: func(cmd *cobra.Command, args []string) {
// tw := NewTimeWarrior()
// if a.flags.ShowExport {
// tw.ExportSummary(a.flags.ExportName)
// }
// if a.flags.ShowWeek {
// tw.ShowSummary(":week")
// }
// if a.flags.ShowMonth {
// tw.ShowSummary(":month")
// }
// os.Exit(0)
// },
// }
cmd.Flags().BoolVarP(&a.flags.ShowWeek, "week", "w", false, "show timewarrior week summary")
cmd.Flags().BoolVarP(&a.flags.ShowMonth, "month", "m", false, "show timewarrior month summary")
cmd.Flags().BoolVarP(&a.flags.ShowExport, "export", "e", false, "export timewarrior timetable")
cmd.Flags().StringVarP(&a.flags.ExportName, "name", "n", "Arbeitszeiten.xlsx", "name of exported excel table")
return cmd
}