From 480118e481efaba224ea19ee5f0f0b761062256c Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Tue, 3 Mar 2026 15:40:50 +0100 Subject: [PATCH] Add option to not create a config file --- internal/config/config.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 5937f5d..cb36129 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,6 +11,11 @@ import ( "github.com/pelletier/go-toml" ) +type CLI struct { + NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."` + ConfigFile Config `embed:"" prefix:""` +} + type Config struct { Port string `toml:"port" short:"p" default:"8080" comment:"The port to listen on for the server."` DatabaseFilePath string `toml:"database-file-path" short:"d" default:"plm.db" comment:"Path to sqlite database file."` @@ -36,8 +41,8 @@ func (u UserListAsStrings) Validate() error { return nil } -func defaultConfig() Config { - return Config{ +func defaultConfig() CLI { + c := Config{ Port: "8080", DatabaseFilePath: "plm.db", DemoDataPath: "", @@ -50,12 +55,13 @@ func defaultConfig() Config { DemoUsername: "demo", AddUser: []string{}, } + return CLI{NoConfigFile: false, ConfigFile: c} } func LoadConfig(configPath string) Config { f, err := os.ReadFile(configPath) - var cfg Config + var cfg CLI configFileNotExist := false if err != nil { if errors.Is(err, os.ErrNotExist) { @@ -70,10 +76,11 @@ func LoadConfig(configPath string) Config { panic(err) } } + //parse in configs and cli kong.Parse(&cfg, kong.Configuration(kongtoml.Loader, configPath)) - if configFileNotExist { - b, err := toml.Marshal(cfg) + if configFileNotExist && !cfg.NoConfigFile { + b, err := toml.Marshal(cfg.ConfigFile) if err != nil { panic(err) } @@ -83,5 +90,5 @@ func LoadConfig(configPath string) Config { } } - return cfg + return cfg.ConfigFile }