Browse Source

*: golint and cleanup

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
master
Vincent Batts 5 years ago
parent
commit
1bb1de3a11
3 changed files with 30 additions and 62 deletions
  1. +1
    -56
      app.go
  2. +24
    -1
      config.go
  3. +5
    -5
      config_test.go

+ 1
- 56
app.go View File

@ -6,34 +6,8 @@
package main
import (
"flag"
"log"
import "flag"
"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() {
flag.Parse()
@ -41,32 +15,3 @@ func main() {
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
}

+ 24
- 1
config.go View File

@ -1,13 +1,18 @@
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 {
XMLName xml.Name `xml:"config"`
SyncRefs []SyncRef `xml:"refs>ref"`
Groups []Group `xml:"groups>group"`
}
// Group defines a set of references that changes will be propogated within
type Group struct {
XMLName xml.Name `xml:"group"`
ID string `xml:"id,attr"`
@ -15,6 +20,7 @@ type Group struct {
Contains []string `xml:"contains>refID"`
}
// SyncRef is a reference to a location to be synchronized from/to
type SyncRef struct {
XMLName xml.Name `xml:"ref"`
ID string `xml:"id,attr"`
@ -22,3 +28,20 @@ type SyncRef struct {
ReadOnly 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)
}

+ 5
- 5
config_test.go View File

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