Change valid-function for config

This commit is contained in:
Torjus Håkestad 2021-12-08 13:12:16 +01:00
parent 84fc2d6667
commit 6558c63096
2 changed files with 26 additions and 28 deletions

View File

@ -211,14 +211,14 @@ func (c *Config) UpdateFromEnv() error {
return nil return nil
} }
func (c *Config) ValidForServer() error { func (sc *ServerConfig) Valid() error {
// Verify that grpc-endpoint is set // Verify that grpc-endpoint is set
if c.Server.GRPCEndpoint == "" { if sc.GRPCEndpoint == "" {
return fmt.Errorf("missing require config-value Server.GRPCEndpoint") return fmt.Errorf("missing require config-value Server.GRPCEndpoint")
} }
// Verify loglevel // Verify loglevel
switch strings.ToUpper(c.Server.LogLevel) { switch strings.ToUpper(sc.LogLevel) {
case "DEBUG", "INFO", "WARN", "ERROR", "FATAL": case "DEBUG", "INFO", "WARN", "ERROR", "FATAL":
break break
default: default:
@ -226,11 +226,11 @@ func (c *Config) ValidForServer() error {
} }
// Verify datastore config // Verify datastore config
switch strings.ToLower(c.Server.DataStoreConfig.Type) { switch strings.ToLower(sc.DataStoreConfig.Type) {
case "memory": case "memory":
break break
case "bolt": case "bolt":
if c.Server.DataStoreConfig.BoltStoreConfig == nil || c.Server.DataStoreConfig.BoltStoreConfig.Path == "" { if sc.DataStoreConfig.BoltStoreConfig == nil || sc.DataStoreConfig.BoltStoreConfig.Path == "" {
return fmt.Errorf("server datastore is bolt, missing required config value Server.DataStore.Bolt.Path") return fmt.Errorf("server datastore is bolt, missing required config value Server.DataStore.Bolt.Path")
} }
default: default:
@ -238,54 +238,54 @@ func (c *Config) ValidForServer() error {
} }
// Verify filestore config // Verify filestore config
switch strings.ToLower(c.Server.FileStoreConfig.Type) { switch strings.ToLower(sc.FileStoreConfig.Type) {
case "memory": case "memory":
break break
case "filesystem": case "filesystem":
if c.Server.FileStoreConfig.FSStoreConfig == nil || c.Server.FileStoreConfig.FSStoreConfig.Dir == "" { if sc.FileStoreConfig.FSStoreConfig == nil || sc.FileStoreConfig.FSStoreConfig.Dir == "" {
return fmt.Errorf("server datastore is bolt, missing required config value Server.FileStore.FSStore.Path") return fmt.Errorf("server datastore is bolt, missing required config value Server.FileStore.FSStore.Path")
} }
case "bolt": case "bolt":
if c.Server.FileStoreConfig.BoltStoreConfig == nil || c.Server.FileStoreConfig.BoltStoreConfig.Path == "" { if sc.FileStoreConfig.BoltStoreConfig == nil || sc.FileStoreConfig.BoltStoreConfig.Path == "" {
return fmt.Errorf("server datastore is bolt, missing required config value Server.DataStore.Bolt.Path") return fmt.Errorf("server datastore is bolt, missing required config value Server.DataStore.Bolt.Path")
} }
} }
// Verify grpc-config // Verify grpc-config
if c.Server.GRPC.ListenAddr == "" { if sc.GRPC.ListenAddr == "" {
return fmt.Errorf("missing required config-value Server.GRPC.ListenAddr") return fmt.Errorf("missing required config-value Server.GRPC.ListenAddr")
} }
if c.Server.GRPC.CACerts.CertificateKeyPath == "" { if sc.GRPC.CACerts.CertificateKeyPath == "" {
// TODO: Maybe return custom error, so we can create certs if missing // TODO: Maybe return custom error, so we can create certs if missing
return fmt.Errorf("missing require value Server.GRPC.CACerts.CertificateKeyPath") return fmt.Errorf("missing require value Server.GRPC.CACerts.CertificateKeyPath")
} }
if c.Server.GRPC.CACerts.CertificatePath == "" { if sc.GRPC.CACerts.CertificatePath == "" {
// TODO: Maybe return custom error, so we can create certs if missing // TODO: Maybe return custom error, so we can create certs if missing
return fmt.Errorf("missing require value Server.GRPC.CACerts.CertificatePath") return fmt.Errorf("missing require value Server.GRPC.CACerts.CertificatePath")
} }
if c.Server.GRPC.Certs.CertificatePath == "" { if sc.GRPC.Certs.CertificatePath == "" {
// TODO: Maybe return custom error, so we can create certs if missing // TODO: Maybe return custom error, so we can create certs if missing
return fmt.Errorf("missing require value Server.GRPC.Certs.CertificatePath") return fmt.Errorf("missing require value Server.GRPC.Certs.CertificatePath")
} }
if c.Server.GRPC.Certs.CertificateKeyPath == "" { if sc.GRPC.Certs.CertificateKeyPath == "" {
// TODO: Maybe return custom error, so we can create certs if missing // TODO: Maybe return custom error, so we can create certs if missing
return fmt.Errorf("missing require value Server.GRPC.Certs.CertificateKeyPath") return fmt.Errorf("missing require value Server.GRPC.Certs.CertificateKeyPath")
} }
return nil return nil
} }
func (c *Config) ValidForClient() error { func (cc *ClientConfig) Valid() error {
if c.Client.Certs.CertificateKeyPath == "" { if cc.Certs.CertificateKeyPath == "" {
return fmt.Errorf("missing required value Client.Certs.CertificateKeyPath") return fmt.Errorf("missing required value Client.Certs.CertificateKeyPath")
} }
if c.Client.Certs.CertificatePath == "" { if cc.Certs.CertificatePath == "" {
return fmt.Errorf("missing required value Client.Certs.CertificatePath") return fmt.Errorf("missing required value Client.Certs.CertificatePath")
} }
if c.Client.DefaultServer == "" { if cc.DefaultServer == "" {
// TODO: Should probably have its own custom error // TODO: Should probably have its own custom error
return fmt.Errorf("missing required value Client.DefaultServer") return fmt.Errorf("missing required value Client.DefaultServer")
} }
if c.Client.ServerCertPath == "" { if cc.ServerCertPath == "" {
// TODO: Should probably have its own custom error // TODO: Should probably have its own custom error
return fmt.Errorf("missing required value Client.ServerCertPath") return fmt.Errorf("missing required value Client.ServerCertPath")
} }

View File

@ -189,20 +189,18 @@ func TestConfig(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error reading config: %s", err) t.Fatalf("Error reading config: %s", err)
} }
clientErr := cfg.ValidForClient()
serverErr := cfg.ValidForServer()
if c.ValidForClient && !(clientErr == nil) { if c.ValidForClient && !(cfg.Client.Valid() == nil) {
t.Errorf("Valid config ValidClientConfig returned wrong result: %s", clientErr) t.Errorf("Valid config ValidClientConfig returned wrong result: %s", cfg.Client.Valid())
} }
if !c.ValidForClient && (clientErr == nil) { if !c.ValidForClient && (cfg.Client.Valid() == nil) {
t.Errorf("Invalid config ValidClientConfig returned wrong result: %s", clientErr) t.Errorf("Invalid config ValidClientConfig returned wrong result: %s", cfg.Client.Valid())
} }
if c.ValidForServer && !(serverErr == nil) { if c.ValidForServer && !(cfg.Server.Valid() == nil) {
t.Errorf("Valid config ValidServerConfig returned wrong result: %s", clientErr) t.Errorf("Valid config ValidServerConfig returned wrong result: %s", cfg.Server.Valid())
} }
if !c.ValidForServer && (serverErr == nil) { if !c.ValidForServer && (cfg.Server.Valid() == nil) {
t.Errorf("Invalid config ValidServerConfig returned wrong result: %s", clientErr) t.Errorf("Invalid config ValidServerConfig returned wrong result: %s", cfg.Server.Valid())
} }
}) })
} }