From dfbb3581877ab0b17956c079758e8d1952a4fb49 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Fri, 25 Aug 2023 21:20:34 +0000 Subject: [PATCH] jpictl: introduce `write` command rewriting all config files Signed-off-by: Alejandro Mery --- cmd/jpictl/write.go | 23 +++++++++++++++++++++++ pkg/zones/sync.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 cmd/jpictl/write.go create mode 100644 pkg/zones/sync.go diff --git a/cmd/jpictl/write.go b/cmd/jpictl/write.go new file mode 100644 index 0000000..93fffb7 --- /dev/null +++ b/cmd/jpictl/write.go @@ -0,0 +1,23 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +// Command +var writeCmd = &cobra.Command{ + Use: "write", + Short: "rewrites all config files", + RunE: func(_ *cobra.Command, _ []string) error { + m, err := cfg.LoadZones() + if err != nil { + return err + } + + return m.SyncAll() + }, +} + +func init() { + rootCmd.AddCommand(writeCmd) +} diff --git a/pkg/zones/sync.go b/pkg/zones/sync.go new file mode 100644 index 0000000..76e3776 --- /dev/null +++ b/pkg/zones/sync.go @@ -0,0 +1,33 @@ +package zones + +// SyncAll updates all config files +func (m *Zones) SyncAll() error { + for _, fn := range []func() error{ + m.SyncAllWireguard, + } { + if err := fn(); err != nil { + return err + } + } + + return nil +} + +// SyncAllWireguard updates all wireguard config files +func (m *Zones) SyncAllWireguard() error { + var err error + + for ring := 0; ring < RingsCount; ring++ { + err = m.PruneWireguardConfig(ring) + if err != nil { + return err + } + + err = m.WriteWireguardKeys(ring) + if err != nil { + return err + } + } + + return nil +}