Add option to not create a config file
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user