*: golint and cleanup

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-10-13 17:28:37 -04:00
parent 49139446ab
commit 1bb1de3a11
3 changed files with 30 additions and 62 deletions

57
app.go
View file

@ -6,34 +6,8 @@
package main package main
import ( import "flag"
"flag"
"log"
"github.com/fsnotify/fsnotify"
)
/*
29044 2016-09-21 12:40:21 cd x
29045 2016-09-21 12:40:25 touch file
29046 2016-09-21 12:40:39 rm file
29047 2016-09-21 12:40:42 touch file
29048 2016-09-21 12:40:50 echo farts > file
29049 2016-09-21 12:40:57 rm file
produces:
$ ./sync_pass x
2016/09/21 12:40:25 event: "x/file": CREATE
2016/09/21 12:40:25 event: "x/file": CHMOD
2016/09/21 12:40:39 event: "x/file": REMOVE
2016/09/21 12:40:42 event: "x/file": CREATE
2016/09/21 12:40:42 event: "x/file": CHMOD
2016/09/21 12:40:50 event: "x/file": WRITE
2016/09/21 12:40:50 modified file: x/file
2016/09/21 12:40:50 event: "x/file": WRITE
2016/09/21 12:40:50 modified file: x/file
2016/09/21 12:40:57 event: "x/file": REMOVE
*/
func main() { func main() {
flag.Parse() flag.Parse()
@ -41,32 +15,3 @@ func main() {
WathPath(arg) WathPath(arg)
} }
} }
func WathPath(path string) {
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:
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
err = watcher.Add(path)
if err != nil {
log.Fatal(err)
}
<-done
}

View file

@ -1,13 +1,18 @@
package main package main
import "encoding/xml" import (
"encoding/xml"
"io"
)
// Config is the over all body for mapping the references to be synchronized
type Config struct { type Config struct {
XMLName xml.Name `xml:"config"` XMLName xml.Name `xml:"config"`
SyncRefs []SyncRef `xml:"refs>ref"` SyncRefs []SyncRef `xml:"refs>ref"`
Groups []Group `xml:"groups>group"` Groups []Group `xml:"groups>group"`
} }
// Group defines a set of references that changes will be propogated within
type Group struct { type Group struct {
XMLName xml.Name `xml:"group"` XMLName xml.Name `xml:"group"`
ID string `xml:"id,attr"` ID string `xml:"id,attr"`
@ -15,6 +20,7 @@ type Group struct {
Contains []string `xml:"contains>refID"` Contains []string `xml:"contains>refID"`
} }
// SyncRef is a reference to a location to be synchronized from/to
type SyncRef struct { type SyncRef struct {
XMLName xml.Name `xml:"ref"` XMLName xml.Name `xml:"ref"`
ID string `xml:"id,attr"` ID string `xml:"id,attr"`
@ -22,3 +28,20 @@ type SyncRef struct {
ReadOnly bool ReadOnly bool
Deletes bool Deletes bool
} }
// ReadConfig Parses a Config from an input
func ReadConfig(r io.Reader) (*Config, error) {
dec := xml.NewDecoder(r)
config := Config{}
if err := dec.Decode(&config); err != nil {
return nil, err
}
return &config, nil
}
// WriteConfig marshals the Config to output w
func WriteConfig(w io.Writer, config Config) error {
enc := xml.NewEncoder(w)
enc.Indent("", " ")
return enc.Encode(config)
}

View file

@ -1,7 +1,7 @@
package main package main
import ( import (
"encoding/xml" "bytes"
"net/url" "net/url"
"testing" "testing"
@ -34,13 +34,13 @@ func TestConfig(t *testing.T) {
}, },
}, },
} }
buf, err := xml.MarshalIndent(config, "", " ") buf := bytes.NewBuffer([]byte{})
if err != nil { if err := WriteConfig(buf, config); err != nil {
t.Fatal(err) t.Fatal(err)
} }
newConfig := Config{} newConfig, err := ReadConfig(bytes.NewReader(buf.Bytes()))
if err := xml.Unmarshal(buf, &newConfig); err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }