68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
SSHUser string `mapstructure:"SSH_USER"`
|
|
SSHHost string `mapstructure:"SSH_HOST"`
|
|
JumpUser string `mapstructure:"JUMP_USER"`
|
|
JumpHost string `mapstructure:"JUMP_HOST"`
|
|
WorkstationHost string `mapstructure:"WORKSTATION_HOST"`
|
|
WorkstationUser string `mapstructure:"WORKSTATION_USER"`
|
|
WorkstationMac string `mapstructure:"WORKSTATION_MAC"`
|
|
RDPUser string `mapstructure:"RDP_USER"`
|
|
RDPPassword string `mapstructure:"RDP_PASSWORD"`
|
|
WorkstationIP string `mapstructure:"WORKSTATION_IP"`
|
|
SSHPort int `mapstructure:"SSH_PORT"`
|
|
}
|
|
|
|
type Flags struct {
|
|
ShowWeek bool
|
|
ShowMonth bool
|
|
ShowExport bool
|
|
ExportName string
|
|
}
|
|
|
|
func loadConfig() (Config, error) {
|
|
var cfg Config
|
|
configPath, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
|
|
workConfigPath := filepath.Join(configPath, "work")
|
|
configFile := filepath.Join(workConfigPath, "config.toml")
|
|
|
|
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 err := viper.UnmarshalKey("default", &cfg); err != nil {
|
|
return cfg, fmt.Errorf("error decoding config: %w", err)
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func getDBPath() (string, error) {
|
|
configPath, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return "", fmt.Errorf("cannot get user config dir: %w", err)
|
|
}
|
|
workConfigPath := filepath.Join(configPath, "work")
|
|
// Sicherstellen, dass das Verzeichnis existiert
|
|
if err := os.MkdirAll(workConfigPath, 0750); err != nil {
|
|
return "", fmt.Errorf("cannot create config directory %s: %w", workConfigPath, err)
|
|
}
|
|
return filepath.Join(workConfigPath, "work_time.db"), nil
|
|
}
|