Add option to set config file path

This commit is contained in:
2026-03-03 21:54:50 +01:00
parent a53c57c039
commit e066321468
2 changed files with 17 additions and 9 deletions

View File

@@ -12,16 +12,17 @@ import (
)
type CLI struct {
NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."`
ConfigFile Config `embed:"" prefix:""`
NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."`
ConfigFilePath string `short:"c" default:"plm.toml" type:"path" help:"Config file path."`
ConfigFile Config `embed:"" prefix:""`
}
type Config struct {
Port string `toml:"port" short:"p" default:"8080" help:"Port to listen on for the server." comment:"Port to listen on for the server."`
DatabaseFilePath string `toml:"database-file-path" short:"d" default:"plm.db" help:"Path to sqlite database file." comment:"Path to sqlite database file."`
DemoDataPath string `toml:"demo-data-path" help:"Path to the sql file to load for demo data." comment:"Path to the sql file to load for demo data."`
DatabaseFilePath string `toml:"database-file-path" short:"d" default:"plm.db" type:"path" help:"Path to sqlite database file." comment:"Path to sqlite database file."`
DemoDataPath string `toml:"demo-data-path" type:"path" help:"Path to the sql file to load for demo data." comment:"Path to the sql file to load for demo data."`
JWTKey string `toml:"jwt-key" help:"Key used to encrypt JWT." comment:"Key used to encrypt the generated JWT. Encoded in base64. If empty a random one will be generated on every restart."`
ImageFolderPath string `toml:"image-folder-path" short:"i" default:"img" help:"Folder where uploaded files will be stored." comment:"Folder where uploaded files will be stored."`
ImageFolderPath string `toml:"image-folder-path" short:"i" default:"img" type:"path" help:"Folder where uploaded files will be stored." comment:"Folder where uploaded files will be stored."`
Limit int `toml:"limit" default:"100" help:"A single API call will return at most this number of records." comment:"A single API call will return at most this number of records."`
InventaireUrl string `toml:"inventaire-url" default:"https://inventaire.io" help:"An inventaire.io instance URL." comment:"An inventaire.io instance URL."`
DisableRegistration bool `toml:"disable-registration" short:"n" default:"false" help:"Disable new account creation." comment:"Disable new account creation."`
@@ -55,13 +56,19 @@ func defaultConfig() CLI {
DemoUsername: "demo",
AddUser: []string{},
}
return CLI{NoConfigFile: false, ConfigFile: c}
return CLI{NoConfigFile: false, ConfigFilePath: "plm.toml", ConfigFile: c}
}
func LoadConfig(configPath string) Config {
f, err := os.ReadFile(configPath)
func LoadConfig() Config {
var cfg CLI
//parse first to get config path
kong.Parse(&cfg)
configPath := cfg.ConfigFilePath
f, err := os.ReadFile(configPath)
configFileNotExist := false
if err != nil {
if errors.Is(err, os.ErrNotExist) {
@@ -76,6 +83,7 @@ func LoadConfig(configPath string) Config {
panic(err)
}
}
//parse in configs and cli
kong.Parse(&cfg, kong.Configuration(kongtoml.Loader, configPath))