refactor: use slog instead of log
This commit is contained in:
parent
fcffccc145
commit
d8743e54c1
8 changed files with 180 additions and 165 deletions
134
cmd.go
134
cmd.go
|
|
@ -2,7 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
|
|
@ -51,11 +51,11 @@ Use --background (-b) to keep tunnels running in the background without auto-con
|
|||
}
|
||||
|
||||
defer func() {
|
||||
log.Println("INFO: Closing SSH connection to jump host (defer)...")
|
||||
slog.Info("Closing SSH connection to jump host (defer)...")
|
||||
if err := sshCon.Close(); err != nil {
|
||||
log.Printf("WARN: Error closing SSH connection in defer: %v", err)
|
||||
slog.Warn(fmt.Sprintf("Error closing SSH connection in defer: %v", err))
|
||||
} else {
|
||||
log.Println("INFO: SSH connection closed via defer.")
|
||||
slog.Info("SSH connection closed via defer.")
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
@ -70,11 +70,11 @@ Use --background (-b) to keep tunnels running in the background without auto-con
|
|||
<-sigChan
|
||||
|
||||
fmt.Println("\nINFO: Received interrupt signal. Shutting down background process...")
|
||||
log.Println("INFO: Received signal, cleanup via defer sshCon.Close() will run.")
|
||||
slog.Info("Received signal, cleanup via defer sshCon.Close() will run.")
|
||||
if err := a.timeStore.StopTracking(); err != nil {
|
||||
log.Printf("WARN: Failed to stop time tracking: %v", err)
|
||||
slog.Warn(fmt.Sprintf("Failed to stop time tracking: %v", err))
|
||||
} else {
|
||||
log.Println("INFO: Time tracking stopped.")
|
||||
slog.Info("Time tracking stopped.")
|
||||
}
|
||||
fmt.Println("INFO: Background shutdown complete.")
|
||||
|
||||
|
|
@ -83,11 +83,11 @@ Use --background (-b) to keep tunnels running in the background without auto-con
|
|||
a.connectToWorkstation()
|
||||
|
||||
fmt.Println("INFO: Workstation SSH session finished.")
|
||||
log.Println("INFO: Foreground session ended, cleanup via defer sshCon.Close() will run.")
|
||||
slog.Info("Foreground session ended, cleanup via defer sshCon.Close() will run.")
|
||||
if err := a.timeStore.StopTracking(); err != nil {
|
||||
log.Printf("WARN: Failed to stop time tracking: %v", err)
|
||||
slog.Warn(fmt.Sprintf("Failed to stop time tracking: %v", err))
|
||||
} else {
|
||||
log.Println("INFO: Time tracking stopped.")
|
||||
slog.Info("Time tracking stopped.")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -108,13 +108,13 @@ func (a *App) stopCommand() *cobra.Command {
|
|||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Stopping workday procedures...")
|
||||
if err := a.timeStore.StopTracking(); err != nil {
|
||||
log.Printf("ERROR: Failed to stop time tracking: %v", err)
|
||||
slog.Error(fmt.Sprintf("Failed to stop time tracking: %v", err))
|
||||
} else {
|
||||
fmt.Println("Time tracking stopped.")
|
||||
}
|
||||
|
||||
if err := a.killForwardings(); err != nil {
|
||||
log.Printf("WARN: Could not kill all forwarding processes: %v", err)
|
||||
slog.Warn(fmt.Sprintf("Could not kill all forwarding processes: %v", err))
|
||||
} else {
|
||||
fmt.Println("Attempted to stop SSH tunnels.")
|
||||
}
|
||||
|
|
@ -160,7 +160,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 {
|
||||
log.Printf("ERROR: Failed to start tracking '%s': %v", tag, err)
|
||||
slog.Error(fmt.Sprintf("Failed to start tracking '%s': %v", tag, err))
|
||||
return fmt.Errorf("could not start tracking '%s': %w", tag, err)
|
||||
}
|
||||
return nil // Erfolg
|
||||
|
|
@ -174,7 +174,7 @@ This also stops any currently running timer.`,
|
|||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
fmt.Println("Starting break...")
|
||||
if err := a.timeStore.StartTracking(TagBreak); err != nil {
|
||||
log.Printf("ERROR: Failed to start break tracking: %v", err)
|
||||
slog.Error(fmt.Sprintf("Failed to start break tracking: %v", err))
|
||||
return fmt.Errorf("could not start break: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
|
@ -204,27 +204,27 @@ Export: Use the --export flag or the 'export' subcommand.`,
|
|||
filename := a.flags.ExportName
|
||||
if filename == "" || filename == "Arbeitszeiten.xlsx" {
|
||||
filename = "Arbeitszeiten_" + time.Now().Format("2006") + ".xlsx"
|
||||
log.Printf("INFO: No export name specified, using default: %s", filename)
|
||||
slog.Info(fmt.Sprintf("No export name specified, using default: %s", filename))
|
||||
}
|
||||
fmt.Printf("Exporting yearly timetable to '%s'...\n", filename)
|
||||
if err := a.timeStore.ExportSummary(filename); err != nil {
|
||||
log.Printf("ERROR: Failed to export summary to '%s': %v", filename, err)
|
||||
slog.Error(fmt.Sprintf("Failed to export summary to '%s': %v", filename, err))
|
||||
fmt.Printf("Error: Could not export to '%s'.\n", filename)
|
||||
}
|
||||
} else if a.flags.ShowWeek {
|
||||
fmt.Println("Showing weekly summary...")
|
||||
if err := a.timeStore.ShowSummary("week"); err != nil {
|
||||
log.Printf("ERROR: Failed to show week summary: %v", err)
|
||||
slog.Error(fmt.Sprintf("Failed to show week summary: %v", err))
|
||||
}
|
||||
} else if a.flags.ShowMonth {
|
||||
fmt.Println("Showing monthly summary...")
|
||||
if err := a.timeStore.ShowSummary("month"); err != nil {
|
||||
log.Printf("ERROR: Failed to show month summary: %v", err)
|
||||
slog.Error(fmt.Sprintf("Failed to show month summary: %v", err))
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Showing summary for period: %s...\n", period)
|
||||
if err := a.timeStore.ShowSummary(period); err != nil {
|
||||
log.Printf("ERROR: Failed to show summary for '%s': %v", period, err)
|
||||
slog.Error(fmt.Sprintf("Failed to show summary for '%s': %v", period, err))
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
@ -248,7 +248,7 @@ Export: Use the --export flag or the 'export' subcommand.`,
|
|||
}
|
||||
fmt.Printf("Exporting yearly timetable to '%s'...\n", filename)
|
||||
if err := a.timeStore.ExportSummary(filename); err != nil {
|
||||
log.Printf("ERROR: Failed to export summary to '%s': %v", filename, err)
|
||||
slog.Error(fmt.Sprintf("Failed to export summary to '%s': %v", filename, err))
|
||||
fmt.Printf("Error: Could not export to '%s'.\n", filename)
|
||||
}
|
||||
},
|
||||
|
|
@ -321,12 +321,12 @@ Example: workctl import-timew /path/to/timew-summary.txt`,
|
|||
|
||||
count, err := a.runImport(filepath)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Import failed: %v", err)
|
||||
slog.Error(fmt.Sprintf("Import failed: %v", err))
|
||||
return fmt.Errorf("import failed: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Successfully imported %d time entries.\n", count)
|
||||
log.Printf("INFO: Successfully imported %d time entries from %s", count, filepath)
|
||||
slog.Info(fmt.Sprintf("Successfully imported %d time entries from %s", count, filepath))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
|
@ -353,8 +353,8 @@ func (a *App) runImport(filepath string) (int, error) {
|
|||
}
|
||||
defer stmt.Close()
|
||||
|
||||
var current_date_str string
|
||||
imported_count := 0
|
||||
var currentDateStr string
|
||||
importedCount := 0
|
||||
location := time.Local
|
||||
|
||||
for i, line := range lines {
|
||||
|
|
@ -364,94 +364,94 @@ func (a *App) runImport(filepath string) (int, error) {
|
|||
}
|
||||
|
||||
fields := strings.Fields(line)
|
||||
var tag, start_str, end_str string
|
||||
has_date := false
|
||||
var tag, startStr, endStr string
|
||||
hasDate := false
|
||||
|
||||
if len(fields) >= 7 && strings.Contains(fields[1], "-") && len(fields[1]) == 10 {
|
||||
current_date_str = fields[1]
|
||||
currentDateStr = fields[1]
|
||||
tag = fields[3]
|
||||
start_str = fields[4]
|
||||
end_str = fields[5]
|
||||
has_date = true
|
||||
startStr = fields[4]
|
||||
endStr = fields[5]
|
||||
hasDate = true
|
||||
} else if len(fields) >= 4 && strings.Contains(fields[1], ":") && strings.Contains(fields[2], ":") {
|
||||
if current_date_str == "" {
|
||||
log.Printf("WARN: Skipping line without preceding date: %s", line)
|
||||
if currentDateStr == "" {
|
||||
slog.Warn(fmt.Sprintf("Skipping line without preceding date: %s", line))
|
||||
continue
|
||||
}
|
||||
tag = fields[0]
|
||||
start_str = fields[1]
|
||||
end_str = fields[2]
|
||||
has_date = false
|
||||
startStr = fields[1]
|
||||
endStr = fields[2]
|
||||
hasDate = false
|
||||
} else if len(fields) >= 6 && strings.Contains(fields[1], "-") && len(fields[1]) == 10 {
|
||||
current_date_str = fields[1]
|
||||
currentDateStr = fields[1]
|
||||
tag = fields[3]
|
||||
start_str = fields[4]
|
||||
end_str = fields[5]
|
||||
has_date = true
|
||||
if start_str == "0:00:00" && end_str == "0:00:00" {
|
||||
start_time, err := time.ParseInLocation("2006-01-02", current_date_str, location)
|
||||
startStr = fields[4]
|
||||
endStr = fields[5]
|
||||
hasDate = true
|
||||
if startStr == "0:00:00" && endStr == "0:00:00" {
|
||||
startTime, err := time.ParseInLocation("2006-01-02", currentDateStr, location)
|
||||
if err != nil {
|
||||
log.Printf("WARN: Skipping line with invalid date '%s': %v", current_date_str, err)
|
||||
slog.Warn(fmt.Sprintf("Skipping line with invalid date '%s': %v", currentDateStr, err))
|
||||
continue
|
||||
}
|
||||
end_time := start_time.Add(24 * time.Hour)
|
||||
endTime := startTime.Add(24 * time.Hour)
|
||||
|
||||
_, err = stmt.Exec(tag, start_time, end_time)
|
||||
_, err = stmt.Exec(tag, startTime, endTime)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Failed to insert full-day entry for %s (%s): %v", current_date_str, tag, err)
|
||||
slog.Error(fmt.Sprintf("Failed to insert full-day entry for %s (%s): %v", currentDateStr, tag, err))
|
||||
} else {
|
||||
imported_count++
|
||||
importedCount++
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
} else {
|
||||
log.Printf("WARN: Skipping unrecognized line format: %s", line)
|
||||
slog.Warn(fmt.Sprintf("Skipping unrecognized line format: %s", line))
|
||||
continue
|
||||
}
|
||||
|
||||
if end_str == "-" {
|
||||
log.Printf("INFO: Skipping currently running entry: %s", line)
|
||||
if endStr == "-" {
|
||||
slog.Info(fmt.Sprintf("Skipping currently running entry: %s", line))
|
||||
continue
|
||||
}
|
||||
|
||||
start_datetime_str := current_date_str + " " + start_str
|
||||
end_datetime_str := current_date_str + " " + end_str
|
||||
startDatetimeStr := currentDateStr + " " + startStr
|
||||
endDatetimeStr := currentDateStr + " " + endStr
|
||||
|
||||
start_time, err_start := time.ParseInLocation("2006-01-02 15:04:05", start_datetime_str, location)
|
||||
end_time, err_end := time.ParseInLocation("2006-01-02 15:04:05", end_datetime_str, location)
|
||||
startTime, errStart := time.ParseInLocation("2006-01-02 15:04:05", startDatetimeStr, location)
|
||||
endTime, errEnd := time.ParseInLocation("2006-01-02 15:04:05", endDatetimeStr, location)
|
||||
|
||||
if err_start != nil || err_end != nil {
|
||||
log.Printf("WARN: Skipping line with invalid date/time format ('%s' / '%s'): %v / %v", start_datetime_str, end_datetime_str, err_start, err_end)
|
||||
if errStart != nil || errEnd != nil {
|
||||
slog.Warn(fmt.Sprintf("Skipping line with invalid date/time format ('%s' / '%s'): %v / %v", startDatetimeStr, endDatetimeStr, errStart, errEnd))
|
||||
continue
|
||||
}
|
||||
|
||||
if end_time.Before(start_time) {
|
||||
if has_date {
|
||||
log.Printf("WARN: End time is before start time on the same date line, skipping: %s", line)
|
||||
if endTime.Before(startTime) {
|
||||
if hasDate {
|
||||
slog.Warn(fmt.Sprintf("End time is before start time on the same date line, skipping: %s", line))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
db_tag := strings.ToLower(tag)
|
||||
switch db_tag {
|
||||
dbTag := strings.ToLower(tag)
|
||||
switch dbTag {
|
||||
case "work":
|
||||
db_tag = TagWork
|
||||
dbTag = TagWork
|
||||
case "break":
|
||||
db_tag = TagBreak
|
||||
dbTag = TagBreak
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(db_tag, start_time, end_time)
|
||||
_, err = stmt.Exec(dbTag, startTime, endTime)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Failed to insert entry for %s (%s, %s -> %s): %v", current_date_str, db_tag, start_time.Format(time.RFC3339), end_time.Format(time.RFC3339), err)
|
||||
slog.Error(fmt.Sprintf("Failed to insert entry for %s (%s, %s -> %s): %v", currentDateStr, dbTag, startTime.Format(time.RFC3339), endTime.Format(time.RFC3339), err))
|
||||
} else {
|
||||
imported_count++
|
||||
importedCount++
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return imported_count, fmt.Errorf("failed to commit transaction: %w", err)
|
||||
return importedCount, fmt.Errorf("failed to commit transaction: %w", err)
|
||||
}
|
||||
|
||||
return imported_count, nil
|
||||
return importedCount, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue