Bladeren bron

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.
Gary Kramlich 3 jaren geleden
bovenliggende
commit
34afda42a1
1 gewijzigde bestanden met toevoegingen van 9 en 0 verwijderingen
  1. 9 0
      registration/cmd.go

+ 9 - 0
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