fix errors & improve code quality

This commit is contained in:
Stephan Müller 2019-04-18 21:49:25 +02:00
parent a8a5a10410
commit a4951096c1
No known key found for this signature in database
GPG Key ID: 4650F39E5B5E1894
3 changed files with 14 additions and 17 deletions

10
file.go
View File

@ -58,17 +58,17 @@ func (b FileBackend) loop(watch bool) (<-chan *StoredData, <-chan error) {
return return
} }
if event.Op&fsnotify.Write == fsnotify.Write { if event.Op&fsnotify.Write == fsnotify.Write {
data, err := getStoredDataFromFile(b.Path) data, err1 := getStoredDataFromFile(b.Path)
if err != nil { if err1 != nil {
errCh <- err errCh <- err1
} }
dataCh <- data dataCh <- data
} }
case err, ok := <-watcher.Errors: case err1, ok := <-watcher.Errors:
if !ok { if !ok {
return return
} }
errCh <- err errCh <- err1
} }
} }
}() }()

7
kv.go
View File

@ -23,7 +23,6 @@ func getStoredDataFromGzip(value []byte) (*StoredData, error) {
data := &StoredData{} data := &StoredData{}
r, err := gzip.NewReader(bytes.NewBuffer(value)) r, err := gzip.NewReader(bytes.NewBuffer(value))
defer r.Close()
if err != nil { if err != nil {
return data, err return data, err
} }
@ -63,7 +62,7 @@ func register(backend string) (store.Backend, error) {
boltdb.Register() boltdb.Register()
return store.BOLTDB, nil return store.BOLTDB, nil
default: default:
return "", fmt.Errorf("No backend found for %v", backend) return "", fmt.Errorf("no backend found for %v", backend)
} }
} }
@ -90,14 +89,12 @@ func (b KVBackend) loop(watch bool) (<-chan *StoredData, <-chan error) {
stopCh := make(<-chan struct{}) stopCh := make(<-chan struct{})
events, _ := kvstore.Watch(storeKey, stopCh, nil) events, _ := kvstore.Watch(storeKey, stopCh, nil)
for { for {
select { kvpair := <-events
case kvpair := <-events:
storedData, err := getStoredDataFromGzip(kvpair.Value) storedData, err := getStoredDataFromGzip(kvpair.Value)
if err != nil { if err != nil {
errors <- err errors <- err
} }
dataCh <- storedData dataCh <- storedData
}
if !watch { if !watch {
close(dataCh) close(dataCh)
close(errors) close(errors)

View File

@ -25,10 +25,10 @@ func main() {
Long: `Dump the content of the "acme.json" file from Traefik to certificates.`, Long: `Dump the content of the "acme.json" file from Traefik to certificates.`,
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
source := cmd.Flag("source").Value.String() source := cmd.Flag("source").Value.String()
sourceFile := cmd.Flag("source-file").Value.String() sourceFile := cmd.Flag("file").Value.String()
if source == "file" { if source == "file" {
if _, err := os.Stat(sourceFile); os.IsNotExist(err) { if _, err := os.Stat(sourceFile); os.IsNotExist(err) {
return fmt.Errorf("--source-file (%q) does not exist", sourceFile) return fmt.Errorf("--file (%q) does not exist", sourceFile)
} }
} else if source != "consul" && source != "etcd" && source != "zookeeper" && source != "boltdb" { } else if source != "consul" && source != "etcd" && source != "zookeeper" && source != "boltdb" {
return fmt.Errorf("--source (%q) is not allowed, use one of 'file', 'consul', 'etcd', 'zookeeper', 'boltdb'", source) return fmt.Errorf("--source (%q) is not allowed, use one of 'file', 'consul', 'etcd', 'zookeeper', 'boltdb'", source)
@ -49,7 +49,7 @@ func main() {
RunE: func(cmd *cobra.Command, _ []string) error { RunE: func(cmd *cobra.Command, _ []string) error {
source := cmd.Flag("source").Value.String() source := cmd.Flag("source").Value.String()
acmeFile := cmd.Flag("source-file").Value.String() acmeFile := cmd.Flag("file").Value.String()
switch source { switch source {
case "file": case "file":