refactor: rebuild work without timewarrior dependency

This commit is contained in:
Patryk Hegenberg 2025-04-02 13:44:30 +02:00
parent b083a8255c
commit 4ceed6f301
12 changed files with 1879 additions and 626 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"log"
"os"
"path/filepath"
@ -20,6 +21,7 @@ type Config struct {
RDPPassword string `mapstructure:"RDP_PASSWORD"`
WorkstationIP string `mapstructure:"WORKSTATION_IP"`
SSHPort int `mapstructure:"SSH_PORT"`
// DatabasePath string `mapstructure:"DATABASE_PATH"`
}
type Flags struct {
@ -33,22 +35,35 @@ func loadConfig() (Config, error) {
var cfg Config
configPath, err := os.UserConfigDir()
if err != nil {
return cfg, err
return cfg, fmt.Errorf("could not get user config dir: %w", err)
}
workConfigPath := filepath.Join(configPath, "work")
configFile := filepath.Join(workConfigPath, "config.toml")
if err := os.MkdirAll(workConfigPath, 0750); err != nil {
return cfg, fmt.Errorf("could not create config directory '%s': %w", workConfigPath, err)
}
viper.SetConfigFile(configFile)
viper.SetConfigType("toml")
viper.AddConfigPath(".")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
return cfg, fmt.Errorf("error reading config file: %w", err)
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return cfg, fmt.Errorf("error reading config file '%s': %w", configFile, err)
}
log.Printf("INFO: Config file '%s' not found, using defaults/env vars.", configFile)
}
if err := viper.UnmarshalKey("default", &cfg); err != nil {
return cfg, fmt.Errorf("error decoding config: %w", err)
if err := viper.Unmarshal(&cfg); err != nil {
return cfg, fmt.Errorf("error decoding config from '%s': %w", configFile, err)
}
}
if cfg.SSHPort == 0 {
cfg.SSHPort = 22
}
return cfg, nil