Implement file watching
This commit is contained in:
parent
c05755948d
commit
0dad540542
10
cmd/file.go
10
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.")
|
||||
}
|
||||
|
||||
7
dumper/file/config.go
Normal file
7
dumper/file/config.go
Normal file
@ -0,0 +1,7 @@
|
||||
package file
|
||||
|
||||
// Config file configuration.
|
||||
type Config struct {
|
||||
AcmeFile string
|
||||
Watch bool
|
||||
}
|
||||
@ -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 {
|
||||
|
||||
1
go.mod
1
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
|
||||
|
||||
3
go.sum
3
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=
|
||||
|
||||
Loading…
Reference in New Issue
Block a user