Add option to not create a config file

This commit is contained in:
2026-03-03 15:40:50 +01:00
parent 12c90ad8ff
commit 480118e481

View File

@@ -11,6 +11,11 @@ import (
"github.com/pelletier/go-toml" "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 { type Config struct {
Port string `toml:"port" short:"p" default:"8080" comment:"The port to listen on for the server."` 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."` 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 return nil
} }
func defaultConfig() Config { func defaultConfig() CLI {
return Config{ c := Config{
Port: "8080", Port: "8080",
DatabaseFilePath: "plm.db", DatabaseFilePath: "plm.db",
DemoDataPath: "", DemoDataPath: "",
@@ -50,12 +55,13 @@ func defaultConfig() Config {
DemoUsername: "demo", DemoUsername: "demo",
AddUser: []string{}, AddUser: []string{},
} }
return CLI{NoConfigFile: false, ConfigFile: c}
} }
func LoadConfig(configPath string) Config { func LoadConfig(configPath string) Config {
f, err := os.ReadFile(configPath) f, err := os.ReadFile(configPath)
var cfg Config var cfg CLI
configFileNotExist := false configFileNotExist := false
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
@@ -70,10 +76,11 @@ func LoadConfig(configPath string) Config {
panic(err) panic(err)
} }
} }
//parse in configs and cli
kong.Parse(&cfg, kong.Configuration(kongtoml.Loader, configPath)) kong.Parse(&cfg, kong.Configuration(kongtoml.Loader, configPath))
if configFileNotExist { if configFileNotExist && !cfg.NoConfigFile {
b, err := toml.Marshal(cfg) b, err := toml.Marshal(cfg.ConfigFile)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -83,5 +90,5 @@ func LoadConfig(configPath string) Config {
} }
} }
return cfg return cfg.ConfigFile
} }