refactor(logging): change logging to slog

This commit is contained in:
Patryk Hegenberg 2025-01-03 07:41:59 +01:00
parent 5a154429d8
commit f00a02632f

68
main.go
View file

@ -3,10 +3,11 @@ package main
import (
"fmt"
"io"
"log"
"log/slog"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
@ -32,30 +33,43 @@ type Config struct {
var cfg Config
func init() {
log.Println("Loading Config File")
viper.SetConfigFile("work-config.toml")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
viper.AutomaticEnv()
err := viper.ReadInConfig()
configPath, err := os.UserConfigDir()
if err != nil {
log.Printf("error loading config file: %v", err)
slog.Error("Unable to find config path", "error", err)
return
}
workConfigPath := filepath.Join(configPath, "work")
configFile := filepath.Join(workConfigPath, "config.toml")
slog.Info("Looking for config file", "path", configFile)
if _, err := os.Stat(configFile); os.IsNotExist(err) {
slog.Error("Config file does not exist", "path", configFile)
return
}
viper.SetConfigFile(configFile)
viper.SetConfigType("toml")
err = viper.ReadInConfig()
if err != nil {
slog.Error("Error loading config file", "error", err)
return
}
slog.Info("Config file successfully loaded")
allSettings := viper.AllSettings()
log.Printf("All loaded settings: %+v", allSettings)
slog.Debug("Loaded settings", "config", allSettings)
err = viper.UnmarshalKey("default", &cfg)
if err != nil {
log.Printf("unable to decode into struct: %v", err)
slog.Error("Unable to decode into struct", "error", err)
}
}
func main() {
// err := LoadConfig()
// if err != nil {
// log.Printf("error: %v", err)
// }
var choice string
form := huh.NewForm(
huh.NewGroup(
@ -80,14 +94,13 @@ func main() {
err := form.Run()
if err != nil {
fmt.Println("Error:", err)
slog.Error("Cannot run form", "error", err)
return
}
switch choice {
case "Start":
connect()
// startSequence()
case "stop Work":
runCommand("timew", "stop", "work")
case "start break":
@ -118,13 +131,14 @@ func connect() {
wakeLou()
sshClient, err := ssh.Dial("tcp", cfg.SSHHost+":"+fmt.Sprintf("%v", cfg.SSHPort), makeSSHClient())
if err != nil {
log.Fatal("Failed to dial: ", err)
slog.Error("Failed to dial", "error", err)
return
}
defer sshClient.Close()
session, err := sshClient.NewSession()
if err != nil {
log.Fatal("Failed to create session: ", err)
slog.Error("Failed to create session", "error", err)
}
defer session.Close()
@ -138,7 +152,7 @@ func connect() {
func forwardPort(sshConn *ssh.Client, localPort, remoteHost, remotePort string) {
listener, err := net.Listen("tcp", "127.0.0.1:"+localPort)
if err != nil {
log.Printf("Fehler beim Öffnen des lokalen Ports %s: %v", localPort, err)
slog.Error("Error opening local port", "port", localPort, "error", err)
return
}
defer listener.Close()
@ -146,13 +160,13 @@ func forwardPort(sshConn *ssh.Client, localPort, remoteHost, remotePort string)
for {
localConn, err := listener.Accept()
if err != nil {
log.Printf("Fehler beim Akzeptieren der Verbindung: %v", err)
slog.Error("Error accepting connection", "error", err)
continue
}
remoteConn, err := sshConn.Dial("tcp", remoteHost+":"+remotePort)
if err != nil {
log.Printf("Fehler beim Verbinden zum Remote-Host %s:%s: %v", remoteHost, remotePort, err)
slog.Error("Error connecting to remote host", "host", remoteHost, "port", remotePort, "error", err)
localConn.Close()
continue
}
@ -172,12 +186,14 @@ func makeSSHClient() *ssh.ClientConfig {
keypath := os.ExpandEnv("$HOME/.ssh/hegenberg")
keyBytes, err := os.ReadFile(keypath)
if err != nil {
log.Fatalf("Failed to read private key: %s", err)
slog.Error("Failed to read private key", "error", err)
os.Exit(1)
}
key, err := ssh.ParsePrivateKeyWithPassphrase(keyBytes, []byte(cfg.RDPPassword))
if err != nil {
log.Fatalf("Failed to parse private key: %s", err)
slog.Error("Failed to parse private key", "error", err)
os.Exit(1)
}
return &ssh.ClientConfig{
@ -197,7 +213,7 @@ func runCommand(name string, args ...string) {
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
fmt.Println("Error:", err)
slog.Error("Command execution error", "command", name, "args", args, "error", err)
}
}
@ -216,7 +232,7 @@ func wakeLou() {
cfg.VardaHost,
cfg.LouMac)
args := strings.Split(sshCommand, " ")
log.Println(args)
slog.Info("Executing wake command", "args", args)
runCommand("ssh", args[1:]...)
}