From 34afda42a18f9cf566246093ed1c24cef4790035 Mon Sep 17 00:00:00 2001 From: Gary Kramlich Date: Fri, 19 Nov 2021 17:15:52 -0600 Subject: [PATCH] Add a -f option to generate registration Because changes to the registration file require a restart of the matrix server changes to it should be minimized. Therefore, if generate-registration is ran while the file exists, we error out unless the --force/-f option is specified. --- registration/cmd.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/registration/cmd.go b/registration/cmd.go index 667506f..d3c50d8 100644 --- a/registration/cmd.go +++ b/registration/cmd.go @@ -2,6 +2,7 @@ package registration import ( "fmt" + "os" "regexp" "maunium.net/go/mautrix/appservice" @@ -12,9 +13,17 @@ import ( type Cmd struct { Filename string `kong:"flag,help='The filename to store the registration into',name='REGISTRATION',short='r',default='registration.yaml'"` + Force bool `kong:"flag,help='Overwrite an existing registration file if it already exists',short='f',default='0'"` } func (c *Cmd) Run(g *globals.Globals) error { + // Check if the file exists before blinding overwriting it. + if _, err := os.Stat(c.Filename); err == nil { + if c.Force == false { + return fmt.Errorf("file %q exists, use -f to overwrite", c.Filename) + } + } + cfg, err := config.FromFile(g.Config) if err != nil { return err