package autosignr

import (
	log "github.com/Sirupsen/logrus"
	"gopkg.in/fsnotify.v1"
)

func WatchDir(conf Config) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		for {
			select {
			case event := <-watcher.Events:
				if event.Op&fsnotify.Create == fsnotify.Create {
					result, _ := CheckCert(conf, event.Name)
					if result {
						SignCert(conf, CertnameFromFilename(event.Name))
					}
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	log.Printf("watching %s", conf.Dir)
	err = watcher.Add(conf.Dir)
	if err != nil {
		log.Fatal(err)
	}
	<-done
}

package autosignr

import (
	log "github.com/Sirupsen/logrus"
	"gopkg.in/fsnotify.v1"
)

func WatchDir(conf Config) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		for {
			select {
			case event := <-watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write {
					result, _ := CheckCert(conf, event.Name)
					if result {
						SignCert(conf, CertnameFromFilename(event.Name))
					}
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	log.Printf("watching %s", conf.Dir)
	err = watcher.Add(conf.Dir)
	if err != nil {
		log.Fatal(err)
	}
	<-done
}

Use Write instead of Create fsnotify event
Use Write instead of Create fsnotify event

I noticed that occasionally the cert file would get created and
autosignr would pick up on it before the cert would get written out.
This would lead to a `No PEM data found in block` error, when in fact
the cert just hadn't been written to the file yet. This change now waits
until the file is written to and should now contain the data.
