6868 initConfigEmbed string
6969 initConfigTemplate = template .Must (template .New ("initConfig" ).Parse (initConfigEmbed ))
7070 invalidProjectId = regexp .MustCompile ("[^a-zA-Z0-9_.-]+" )
71+ envPattern = regexp .MustCompile (`^env\((.*)\)$` )
7172)
7273
7374// Type for turning human-friendly bytes string ("5MB", "32kB") into an int64 during toml decoding.
@@ -164,31 +165,35 @@ type (
164165 }
165166
166167 sms struct {
167- EnableSignup bool `toml:"enable_signup"`
168- EnableConfirmations bool `toml:"enable_confirmations"`
169- Twilio * twilioConfig `toml:"twilio" mapstructure:"twilio"`
170- Messagebird * messagebirdConfig `toml:"messagebird" mapstructure:"messagebird"`
171- Textlocal * textlocalConfig `toml:"textlocal" mapstructure:"textlocal"`
172- Vonage * vonageConfig `toml:"vonage" mapstructure:"vonage"`
168+ EnableSignup bool `toml:"enable_signup"`
169+ EnableConfirmations bool `toml:"enable_confirmations"`
170+ Twilio twilioConfig `toml:"twilio" mapstructure:"twilio"`
171+ Messagebird messagebirdConfig `toml:"messagebird" mapstructure:"messagebird"`
172+ Textlocal textlocalConfig `toml:"textlocal" mapstructure:"textlocal"`
173+ Vonage vonageConfig `toml:"vonage" mapstructure:"vonage"`
173174 }
174175
175176 twilioConfig struct {
177+ Enabled bool `toml:"enabled"`
176178 AccountSid string `toml:"account_sid"`
177179 MessageServiceSid string `toml:"message_service_sid"`
178180 AuthToken string `toml:"auth_token" mapstructure:"auth_token"`
179181 }
180182
181183 messagebirdConfig struct {
184+ Enabled bool `toml:"enabled"`
182185 Originator string `toml:"originator"`
183186 AccessKey string `toml:"access_key" mapstructure:"access_key"`
184187 }
185188
186189 textlocalConfig struct {
187- Sender string `toml:"sender"`
188- ApiKey string `toml:"api_key" mapstructure:"api_key"`
190+ Enabled bool `toml:"enabled"`
191+ Sender string `toml:"sender"`
192+ ApiKey string `toml:"api_key" mapstructure:"api_key"`
189193 }
190194
191195 vonageConfig struct {
196+ Enabled bool `toml:"enabled"`
192197 From string `toml:"from"`
193198 ApiKey string `toml:"api_key" mapstructure:"api_key"`
194199 ApiSecret string `toml:"api_secret" mapstructure:"api_secret"`
@@ -315,7 +320,7 @@ func LoadConfigFS(fsys afero.Fs) error {
315320 Config .Auth .ServiceRoleKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU"
316321 }
317322
318- if Config .Auth .Sms .Twilio != nil {
323+ if Config .Auth .Sms .Twilio . Enabled {
319324 if len (Config .Auth .Sms .Twilio .AccountSid ) == 0 {
320325 return errors .New ("Missing required field in config: auth.sms.twilio.account_sid" )
321326 }
@@ -326,23 +331,23 @@ func LoadConfigFS(fsys afero.Fs) error {
326331 return errors .New ("Missing required field in config: auth.sms.twilio.auth_token" )
327332 }
328333 }
329- if Config .Auth .Sms .Messagebird != nil {
334+ if Config .Auth .Sms .Messagebird . Enabled {
330335 if len (Config .Auth .Sms .Messagebird .Originator ) == 0 {
331336 return errors .New ("Missing required field in config: auth.sms.messagebird.originator" )
332337 }
333338 if len (Config .Auth .Sms .Messagebird .AccessKey ) == 0 {
334339 return errors .New ("Missing required field in config: auth.sms.messagebird.access_key" )
335340 }
336341 }
337- if Config .Auth .Sms .Textlocal != nil {
342+ if Config .Auth .Sms .Textlocal . Enabled {
338343 if len (Config .Auth .Sms .Textlocal .Sender ) == 0 {
339344 return errors .New ("Missing required field in config: auth.sms.textlocal.sender" )
340345 }
341346 if len (Config .Auth .Sms .Textlocal .ApiKey ) == 0 {
342347 return errors .New ("Missing required field in config: auth.sms.textlocal.api_key" )
343348 }
344349 }
345- if Config .Auth .Sms .Vonage != nil {
350+ if Config .Auth .Sms .Vonage . Enabled {
346351 if len (Config .Auth .Sms .Vonage .From ) == 0 {
347352 return errors .New ("Missing required field in config: auth.sms.vonage.from" )
348353 }
@@ -365,21 +370,6 @@ func LoadConfigFS(fsys afero.Fs) error {
365370 Secret : "" ,
366371 }
367372 } else if Config .Auth .External [ext ].Enabled {
368- maybeLoadEnv := func (s string ) (string , error ) {
369- matches := regexp .MustCompile (`^env\((.*)\)$` ).FindStringSubmatch (s )
370- if len (matches ) == 0 {
371- return s , nil
372- }
373-
374- envName := matches [1 ]
375- value := os .Getenv (envName )
376- if value == "" {
377- return "" , errors .New (`Error evaluating "env(` + envName + `)": environment variable ` + envName + " is unset." )
378- }
379-
380- return value , nil
381- }
382-
383373 var clientId , secret , redirectUri , url string
384374
385375 if Config .Auth .External [ext ].ClientId == "" {
@@ -400,15 +390,13 @@ func LoadConfigFS(fsys afero.Fs) error {
400390 }
401391 secret = v
402392 }
403-
404393 if Config .Auth .External [ext ].RedirectUri != "" {
405394 v , err := maybeLoadEnv (Config .Auth .External [ext ].RedirectUri )
406395 if err != nil {
407396 return err
408397 }
409398 redirectUri = v
410399 }
411-
412400 if Config .Auth .External [ext ].Url != "" {
413401 v , err := maybeLoadEnv (Config .Auth .External [ext ].Url )
414402 if err != nil {
@@ -463,6 +451,20 @@ func LoadConfigFS(fsys afero.Fs) error {
463451 return nil
464452}
465453
454+ func maybeLoadEnv (s string ) (string , error ) {
455+ matches := envPattern .FindStringSubmatch (s )
456+ if len (matches ) == 0 {
457+ return s , nil
458+ }
459+
460+ envName := matches [1 ]
461+ if value := os .Getenv (envName ); value != "" {
462+ return value , nil
463+ }
464+
465+ return "" , fmt .Errorf (`Error evaluating "env(%s)": environment variable %s is unset.` , s , envName )
466+ }
467+
466468func sanitizeProjectId (src string ) string {
467469 // A valid project ID must only contain alphanumeric and special characters _.-
468470 sanitized := invalidProjectId .ReplaceAllString (src , "_" )
0 commit comments