load config.json in current directory by default (issue #17)

This commit is contained in:
Lou Knauer 2022-07-05 10:22:46 +02:00
parent 8bdab3298d
commit 65f7e5e6c8
2 changed files with 9 additions and 8 deletions

View File

@ -137,7 +137,7 @@ func main() {
flag.BoolVar(&flagSyncLDAP, "sync-ldap", false, "Sync the 'user' table with ldap") flag.BoolVar(&flagSyncLDAP, "sync-ldap", false, "Sync the 'user' table with ldap")
flag.BoolVar(&flagStopImmediately, "no-server", false, "Do not start a server, stop right after initialization and argument handling") flag.BoolVar(&flagStopImmediately, "no-server", false, "Do not start a server, stop right after initialization and argument handling")
flag.BoolVar(&flagGops, "gops", false, "Listen via github.com/google/gops/agent (for debugging)") flag.BoolVar(&flagGops, "gops", false, "Listen via github.com/google/gops/agent (for debugging)")
flag.StringVar(&flagConfigFile, "config", "", "Overwrite the global config options by those specified in `config.json`") flag.StringVar(&flagConfigFile, "config", "./config.json", "Overwrite the global config options by those specified in `config.json`")
flag.StringVar(&flagNewUser, "add-user", "", "Add a new user. Argument format: `<username>:[admin,api,user]:<password>`") flag.StringVar(&flagNewUser, "add-user", "", "Add a new user. Argument format: `<username>:[admin,api,user]:<password>`")
flag.StringVar(&flagDelUser, "del-user", "", "Remove user by `username`") flag.StringVar(&flagDelUser, "del-user", "", "Remove user by `username`")
flag.StringVar(&flagGenJWT, "jwt", "", "Generate and print a JWT for the user specified by its `username`") flag.StringVar(&flagGenJWT, "jwt", "", "Generate and print a JWT for the user specified by its `username`")
@ -155,17 +155,19 @@ func main() {
log.Fatalf("parsing './.env' file failed: %s", err.Error()) log.Fatalf("parsing './.env' file failed: %s", err.Error())
} }
if flagConfigFile != "" { // Load JSON config:
f, err := os.Open(flagConfigFile) f, err := os.Open(flagConfigFile)
if err != nil { if err != nil {
if !os.IsNotExist(err) || flagConfigFile != "./config.json" {
log.Fatal(err) log.Fatal(err)
} }
defer f.Close() } else {
dec := json.NewDecoder(f) dec := json.NewDecoder(f)
dec.DisallowUnknownFields() dec.DisallowUnknownFields()
if err := dec.Decode(&programConfig); err != nil { if err := dec.Decode(&programConfig); err != nil {
log.Fatal(err) log.Fatal(err)
} }
f.Close()
} }
// As a special case for `db`, allow using an environment variable instead of the value // As a special case for `db`, allow using an environment variable instead of the value
@ -176,7 +178,6 @@ func main() {
programConfig.DB = os.Getenv(envvar) programConfig.DB = os.Getenv(envvar)
} }
var err error
repository.Connect(programConfig.DBDriver, programConfig.DB) repository.Connect(programConfig.DBDriver, programConfig.DB)
db := repository.GetConnection() db := repository.GetConnection()

View File

@ -142,13 +142,13 @@ func (auth *Authentication) SyncWithLDAP(deleteOldUsers bool) error {
for username, where := range users { for username, where := range users {
if where == IN_DB && deleteOldUsers { if where == IN_DB && deleteOldUsers {
log.Infof("ldap-sync: remove %#v (does not show up in LDAP anymore)", username) log.Debugf("ldap-sync: remove %#v (does not show up in LDAP anymore)", username)
if _, err := auth.db.Exec(`DELETE FROM user WHERE user.username = ?`, username); err != nil { if _, err := auth.db.Exec(`DELETE FROM user WHERE user.username = ?`, username); err != nil {
return err return err
} }
} else if where == IN_LDAP { } else if where == IN_LDAP {
name := newnames[username] name := newnames[username]
log.Infof("ldap-sync: add %#v (name: %#v, roles: [user], ldap: true)", username, name) log.Debugf("ldap-sync: add %#v (name: %#v, roles: [user], ldap: true)", username, name)
if _, err := auth.db.Exec(`INSERT INTO user (username, ldap, name, roles) VALUES (?, ?, ?, ?)`, if _, err := auth.db.Exec(`INSERT INTO user (username, ldap, name, roles) VALUES (?, ?, ?, ?)`,
username, 1, name, "[\""+RoleUser+"\"]"); err != nil { username, 1, name, "[\""+RoleUser+"\"]"); err != nil {
return err return err