From 94b5b45396060dc6ebccf147554898235c946fc6 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 8 Dec 2021 11:43:41 +0100 Subject: [PATCH] Allow updating config from env --- config/config.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ ezshare.example.toml | 18 +++++++++++++ 2 files changed, 80 insertions(+) diff --git a/config/config.go b/config/config.go index 37cd6bb..229485d 100644 --- a/config/config.go +++ b/config/config.go @@ -144,6 +144,68 @@ func FromDefaultLocations() (*Config, error) { return nil, fmt.Errorf("config not found") } +func (c *Config) UpdateFromEnv() error { + // Server stuff + if val, found := os.LookupEnv("EZSHARE_SERVER_LOGLEVEL"); found { + c.Server.LogLevel = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_HOSTNAME"); found { + c.Server.Hostname = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_GRPCENDPOINT"); found { + c.Server.GRPCEndpoint = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_DATASTORE_TYPE"); found { + c.Server.DataStoreConfig.Type = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_DATASTORE_BOLT_PATH"); found { + c.Server.DataStoreConfig.BoltStoreConfig.Path = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_FILESTORE_TYPE"); found { + c.Server.FileStoreConfig.Type = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_FILESTORE_BOLT_PATH"); found { + c.Server.FileStoreConfig.BoltStoreConfig.Path = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_FILESTORE_FILESYSTEM_DIR"); found { + c.Server.FileStoreConfig.FSStoreConfig.Dir = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEKEYPATH"); found { + c.Server.GRPC.CACerts.CertificateKeyPath = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEPATH"); found { + c.Server.GRPC.CACerts.CertificatePath = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_CERTS_CERTIFICATEKEYPATH"); found { + c.Server.GRPC.Certs.CertificateKeyPath = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_CERTS_CERTIFICATEPATH"); found { + c.Server.GRPC.Certs.CertificatePath = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_GRPC_LISTENADDR"); found { + c.Server.GRPC.ListenAddr = val + } + if val, found := os.LookupEnv("EZSHARE_SERVER_HTTP_LISTENADDR"); found { + c.Server.HTTP.ListenAddr = val + } + + // Client stuff + if val, found := os.LookupEnv("EZSHARE_CLIENT_DEFAULTSERVER"); found { + c.Client.DefaultServer = val + } + if val, found := os.LookupEnv("EZSHARE_CLIENT_CERTS_CERTIFICATEKEYPATH"); found { + c.Client.Certs.CertificateKeyPath = val + } + if val, found := os.LookupEnv("EZSHARE_CLIENT_CERTS_CERTIFICATEPATH"); found { + c.Client.Certs.CertificatePath = val + } + if val, found := os.LookupEnv("EZSHARE_CLIENT_SERVERCERTPATH"); found { + c.Client.ServerCertPath = val + } + + return nil +} + func (c *Config) ValidForServer() error { // Verify that grpc-endpoint is set if c.Server.GRPCEndpoint == "" { diff --git a/ezshare.example.toml b/ezshare.example.toml index d1b410e..f469913 100644 --- a/ezshare.example.toml +++ b/ezshare.example.toml @@ -5,16 +5,19 @@ # Set server log-level # Must be one of: DEBUG, INFO, WARN, ERROR # Default: INFO +# ENV: EZSHARE_SERVER_LOGLEVEL LogLevel = "INFO" # Server hostname # Used for generating links # Required +# ENV: EZSHARE_SERVER_HOSTNAME Hostname = "localhost" # Endpoint reachable by clients # Fetched by clients for automatic setup # Required +# ENV: EZSHARE_SERVER_GRPCENDPOINT GRPCEndpoint = "localhost:50051" # File store configuration @@ -22,53 +25,64 @@ GRPCEndpoint = "localhost:50051" # How server stores file # Must be one of: filesystem, memory, bolt # Required +# ENV: EZSHARE_SERVER_FILESTORE_TYPE Type = "bolt" [Server.FileStore.Bolt] # Where the bolt-db is stored # Required if store-type is bolt +# ENV: EZSHARE_SERVER_FILESTORE_BOLT_PATH Path = "/data/files.db" [Server.FileStore.Filesystem] # Where files are stored # Required if store-type is filesystem +# ENV: EZSHARE_SERVER_FILESTORE_FILESYSTEM_DIR Dir = "/data" [Server.DataStore] # What store to use for users, certs and binaries # Must be one of: memory, bolt # Required +# ENV: EZSHARE_SERVER_DATASTORE_TYPE Type = "bolt" [Server.DataStore.Bolt] # Path to bolt database-file # Required if Server.Datastore is bolt +# ENV: EZSHARE_SERVER_DATASTORE_BOLT_PATH Path = "/data/users.db" # GRPC Configuration [Server.GRPC] # Address to listen to # Default: :50051 +# ENV: EZSHARE_SERVER_GRPC_LISTENADDR ListenAddr = ":50051" # GRPC Certificate Configuration [Server.GRPC.CACerts] # Path of PEM-encoded certificate file +# ENV: EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEPATH CertificatePath = "" # Path of PEM-encoded private key # Must be of type ecdsa CertificateKeyPath = "" +# ENV: EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEKEYPATH [Server.GRPC.Certs] # Path of PEM-encoded certificate file +# ENV: EZSHARE_SERVER_GRPC_CERTS_CERTIFICATEPATH CertificatePath = "" # Path of PEM-encoded private key # Must be of type ecdsa +# ENV: EZSHARE_SERVER_GRPC_CERTS_CERTIFICATEKEYPATH CertificateKeyPath = "" [Server.HTTP] # Address to listen to # Default: :8089 +# ENV: EZSHARE_SERVER_HTTP_LISTENADDR ListenAddr = ":8089" @@ -77,13 +91,17 @@ ListenAddr = ":8089" ######################## [Client] # Server used if not specified using command-line +# ENV: EZSHARE_CLIENT_DEFAULTSERVER DefaultServer = "localhost:50051" # Path to PEM-encoder server-certificate +# ENV: EZSHARE_CLIENT_SERVERCERTPATH ServerCertPath = "" [Client.Certs] # Path of PEM-encoded certificate file +# ENV: EZSHARE_CLIENT_CERTS_CERTIFICATEPATH CertificatePath = "" # Path of PEM-encoded private key # Must be of type ecdsa +# ENV: EZSHARE_CLIENT_CERTS_CERTIFICATEKEYPATH CertificateKeyPath = ""