@@ -33,9 +33,18 @@ type App struct {
3333 Target string
3434 Bind string
3535 Proxy bool
36- Users map [string ]string // username:password
37- Emails []string // list of emails or @domains
38- Roles map [string ]string // username/email:role
36+
37+ // username:password
38+ Users map [string ]string
39+ usersFile string
40+
41+ // list of emails or @domains
42+ Emails []string
43+ emailsFile string
44+
45+ // username/email:role
46+ Roles map [string ]string
47+ rolesFile string
3948
4049 RedirectAllowlist []url.URL
4150
@@ -205,9 +214,9 @@ func (c *App) Set() {
205214 emails := viper .GetStringSlice ("app.emails" )
206215
207216 // load emails from a file
208- emailsFile : = viper .GetString ("app.emails_file" )
209- if emailsFile != "" {
210- emailsBytes , err := os .ReadFile (emailsFile )
217+ c . emailsFile = viper .GetString ("app.emails_file" )
218+ if c . emailsFile != "" {
219+ emailsBytes , err := os .ReadFile (c . emailsFile )
211220 if err != nil {
212221 log .Panic ().Err (err ).Msgf ("error opening emails file" )
213222 }
@@ -220,9 +229,9 @@ func (c *App) Set() {
220229 users := viper .GetStringSlice ("app.users" )
221230
222231 // load users from a file
223- usersFile : = viper .GetString ("app.users_file" )
224- if usersFile != "" {
225- usersBytes , err := os .ReadFile (usersFile )
232+ c . usersFile = viper .GetString ("app.users_file" )
233+ if c . usersFile != "" {
234+ usersBytes , err := os .ReadFile (c . usersFile )
226235 if err != nil {
227236 log .Panic ().Err (err ).Msgf ("error opening users file" )
228237 }
@@ -235,9 +244,9 @@ func (c *App) Set() {
235244 roles := viper .GetStringSlice ("app.roles" )
236245
237246 // load roles from a file
238- rolesFile : = viper .GetString ("app.roles_file" )
239- if rolesFile != "" {
240- rolesBytes , err := os .ReadFile (rolesFile )
247+ c . rolesFile = viper .GetString ("app.roles_file" )
248+ if c . rolesFile != "" {
249+ rolesBytes , err := os .ReadFile (c . rolesFile )
241250 if err != nil {
242251 log .Panic ().Err (err ).Msgf ("error opening roles file" )
243252 }
@@ -324,6 +333,46 @@ func (c *App) Set() {
324333 c .Expiration .Session = time .Duration (viper .GetInt64 ("app.expiration.session" )) * time .Second
325334}
326335
336+ func (c * App ) SaveEmails () error {
337+ // if there is no emails file, we cannot save anything
338+ if c .emailsFile == "" {
339+ return fmt .Errorf ("no emails file specified" )
340+ }
341+
342+ payload := []byte (strings .Join (c .Emails , "\n " ))
343+ return os .WriteFile (c .emailsFile , payload , 0644 )
344+ }
345+
346+ func (c * App ) SaveUsers () error {
347+ // if there is no users file, we cannot save anything
348+ if c .usersFile == "" {
349+ return fmt .Errorf ("no users file specified" )
350+ }
351+
352+ users := []string {}
353+ for username , secret := range c .Users {
354+ users = append (users , fmt .Sprintf ("%s:%s" , username , secret ))
355+ }
356+
357+ payload := []byte (strings .Join (users , "\n " ))
358+ return os .WriteFile (c .usersFile , payload , 0644 )
359+ }
360+
361+ func (c * App ) SaveRoles () error {
362+ // if there is no roles file, we cannot save anything
363+ if c .rolesFile == "" {
364+ return fmt .Errorf ("no roles file specified" )
365+ }
366+
367+ roles := []string {}
368+ for user , role := range c .Roles {
369+ roles = append (roles , fmt .Sprintf ("%s=%s" , user , role ))
370+ }
371+
372+ payload := []byte (strings .Join (roles , "\n " ))
373+ return os .WriteFile (c .rolesFile , payload , 0644 )
374+ }
375+
327376//
328377// tmpl
329378//
@@ -492,3 +541,31 @@ func (c *Redis) Set() {
492541 c .Password = viper .GetString ("redis.password" )
493542 c .Database = viper .GetInt ("redis.database" )
494543}
544+
545+ //
546+ // gui
547+ //
548+
549+ type Gui struct {
550+ Enabled bool
551+ Bind string
552+ }
553+
554+ func (Gui ) Init (cmd * cobra.Command ) error {
555+ cmd .PersistentFlags ().Bool ("gui.enabled" , false , "If GUI should be enabled." )
556+ if err := viper .BindPFlag ("gui.enabled" , cmd .PersistentFlags ().Lookup ("gui.enabled" )); err != nil {
557+ return err
558+ }
559+
560+ cmd .PersistentFlags ().String ("gui.bind" , "127.0.0.1:8081" , "Address, where is HTTP server listening." )
561+ if err := viper .BindPFlag ("gui.bind" , cmd .PersistentFlags ().Lookup ("gui.bind" )); err != nil {
562+ return err
563+ }
564+
565+ return nil
566+ }
567+
568+ func (c * Gui ) Set () {
569+ c .Enabled = viper .GetBool ("gui.enabled" )
570+ c .Bind = viper .GetString ("gui.bind" )
571+ }
0 commit comments