Add option to set config file path
This commit is contained in:
@@ -12,16 +12,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."`
|
NoConfigFile bool `short:"C" default:"false" help:"Disable config file creation."`
|
||||||
ConfigFile Config `embed:"" prefix:""`
|
ConfigFilePath string `short:"c" default:"plm.toml" type:"path" help:"Config file path."`
|
||||||
|
ConfigFile Config `embed:"" prefix:""`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
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."`
|
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."`
|
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" help:"Path to the sql file to load for demo data." comment:"Path to the sql file to load for demo data."`
|
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."`
|
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."`
|
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."`
|
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."`
|
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",
|
DemoUsername: "demo",
|
||||||
AddUser: []string{},
|
AddUser: []string{},
|
||||||
}
|
}
|
||||||
return CLI{NoConfigFile: false, ConfigFile: c}
|
return CLI{NoConfigFile: false, ConfigFilePath: "plm.toml", ConfigFile: c}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig(configPath string) Config {
|
func LoadConfig() Config {
|
||||||
f, err := os.ReadFile(configPath)
|
|
||||||
|
|
||||||
var cfg CLI
|
var cfg CLI
|
||||||
|
|
||||||
|
//parse first to get config path
|
||||||
|
kong.Parse(&cfg)
|
||||||
|
|
||||||
|
configPath := cfg.ConfigFilePath
|
||||||
|
|
||||||
|
f, err := os.ReadFile(configPath)
|
||||||
configFileNotExist := false
|
configFileNotExist := false
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@@ -76,6 +83,7 @@ func LoadConfig(configPath string) Config {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//parse in configs and cli
|
//parse in configs and cli
|
||||||
kong.Parse(&cfg, kong.Configuration(kongtoml.Loader, configPath))
|
kong.Parse(&cfg, kong.Configuration(kongtoml.Loader, configPath))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user