Files
gertdns/auth/toml.go
2021-10-20 23:23:36 +02:00

46 lines
1.1 KiB
Go

package auth
import (
"io/ioutil"
"os"
"github.com/gookit/color"
"github.com/pelletier/go-toml/v2"
)
func loadAuthFile(authFilePath string) (map[string]*userRaw, error) {
bytes, err := ioutil.ReadFile(authFilePath)
if err != nil {
color.Errorln(err.Error())
color.Warnln("Creating new authentication file")
return writeAuthFile(authFilePath, map[string]*userRaw{})
}
var users map[string]*userRaw
err = toml.Unmarshal(bytes, &users)
if err != nil {
color.Errorln(err.Error())
color.Errorln("Authentication file " + authFilePath + " could not be read as TOML file")
return users, err
}
color.Infoln("Loaded authentication file " + authFilePath)
return users, err
}
func writeAuthFile(authFilePath string, users map[string]*userRaw) (map[string]*userRaw, error) {
authBytes, err := toml.Marshal(users)
if err != nil {
color.Errorln(err.Error())
color.Errorln("Default authentication struct is not TOML conform")
return users, err
}
err = os.WriteFile(authFilePath, authBytes, 0644)
if err != nil {
color.Errorln(err.Error())
color.Warnln("Using default authentication without file")
}
return users, err
}