From 0dad5405421427ed92d99a8b04e0fc47f7366109 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 21 Apr 2019 20:02:17 +0200 Subject: [PATCH] Implement file watching --- cmd/file.go | 10 +++++++++- dumper/file/config.go | 7 +++++++ dumper/file/file.go | 35 +++++++++++++++++++++++++++++++++-- go.mod | 1 + go.sum | 3 +++ 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 dumper/file/config.go diff --git a/cmd/file.go b/cmd/file.go index 18f70f0..3d1ce4c 100644 --- a/cmd/file.go +++ b/cmd/file.go @@ -1,6 +1,8 @@ package cmd import ( + "strconv" + "github.com/ldez/traefik-certs-dumper/v2/dumper" "github.com/ldez/traefik-certs-dumper/v2/dumper/file" "github.com/spf13/cobra" @@ -13,8 +15,13 @@ var fileCmd = &cobra.Command{ Long: `Dump the content of the "acme.json" file from Traefik to certificates.`, RunE: runE(func(baseConfig *dumper.BaseConfig, cmd *cobra.Command) error { acmeFile := cmd.Flag("source").Value.String() + watch, _ := strconv.ParseBool(cmd.Flag("watch").Value.String()) - return file.Dump(acmeFile, baseConfig) + config := &file.Config{ + AcmeFile: acmeFile, + Watch: watch, + } + return file.Dump(config, baseConfig) }), } @@ -22,4 +29,5 @@ func init() { rootCmd.AddCommand(fileCmd) fileCmd.Flags().String("source", "./acme.json", "Path to 'acme.json' file.") + fileCmd.PersistentFlags().Bool("watch", false, "Enable watching changes.") } diff --git a/dumper/file/config.go b/dumper/file/config.go new file mode 100644 index 0000000..76e7599 --- /dev/null +++ b/dumper/file/config.go @@ -0,0 +1,7 @@ +package file + +// Config file configuration. +type Config struct { + AcmeFile string + Watch bool +} diff --git a/dumper/file/file.go b/dumper/file/file.go index 2ecee24..33af5a8 100644 --- a/dumper/file/file.go +++ b/dumper/file/file.go @@ -2,14 +2,20 @@ package file import ( "encoding/json" + "log" "os" "github.com/ldez/traefik-certs-dumper/v2/dumper" + "github.com/rjeczalik/notify" ) // Dump Dumps "acme.json" file to certificates. -func Dump(acmeFile string, baseConfig *dumper.BaseConfig) error { - data, err := readFile(acmeFile) +func Dump(config *Config, baseConfig *dumper.BaseConfig) error { + if config.Watch { + return watch(config.AcmeFile, baseConfig) + } + + data, err := readFile(config.AcmeFile) if err != nil { return err } @@ -17,6 +23,31 @@ func Dump(acmeFile string, baseConfig *dumper.BaseConfig) error { return dumper.Dump(data, baseConfig) } +func watch(acmeFile string, baseConfig *dumper.BaseConfig) error { + events := make(chan notify.EventInfo, 1) + + if err := notify.Watch(acmeFile, events, notify.Create, notify.Write, notify.Remove); err != nil { + return err + } + defer notify.Stop(events) + + for { + // wait for filesystem event + <-events + + data, err := readFile(acmeFile) + if err != nil { + return err + } + + if err := dumper.Dump(data, baseConfig); err != nil { + return err + } + + log.Println("Dumped new certificate data.") + } +} + func readFile(acmeFile string) (*dumper.StoredData, error) { source, err := os.Open(acmeFile) if err != nil { diff --git a/go.mod b/go.mod index f02c99f..25e69be 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( github.com/pkg/errors v0.8.1 // indirect github.com/prometheus/client_golang v0.9.2 // indirect github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect + github.com/rjeczalik/notify v0.9.2 github.com/sirupsen/logrus v1.4.1 // indirect github.com/soheilhy/cmux v0.1.4 // indirect github.com/spf13/cobra v0.0.3 diff --git a/go.sum b/go.sum index b2b4fa5..b1e7321 100644 --- a/go.sum +++ b/go.sum @@ -90,6 +90,8 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1: github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8= +github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= @@ -143,6 +145,7 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1YthsFqr/5mxHduZW2A=