organized prototype
This commit is contained in:
parent
1ef91ad673
commit
a3055abfc0
33
main.go
Normal file
33
main.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
const (
|
||||
BOOKMARK_FILE="/home/spike/.config/google-chrome-unstable/Default/Bookmarks"
|
||||
BOOKMARK_DIR="/home/spike/.config/google-chrome-unstable/Default/"
|
||||
)
|
||||
|
||||
func main() {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer watcher.Close()
|
||||
|
||||
done := make(chan bool)
|
||||
|
||||
go watcherThread(watcher)
|
||||
|
||||
|
||||
|
||||
err = watcher.Add(BOOKMARK_DIR)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
<-done
|
||||
}
|
72
parse.go
Normal file
72
parse.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
Type string
|
||||
Children []interface{}
|
||||
Url string `json:",omitempty"`
|
||||
Name string
|
||||
}
|
||||
|
||||
|
||||
type RootData struct {
|
||||
Name string
|
||||
Roots map[string]Node
|
||||
Version float64
|
||||
}
|
||||
|
||||
|
||||
|
||||
func mapToNode(childNode interface{}) (*Node, error) {
|
||||
if childNode == nil {
|
||||
return new(Node), nil
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
// Convert interface{} to json
|
||||
err := json.NewEncoder(buf).Encode(childNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//fmt.Println(buf)
|
||||
|
||||
out := new(Node)
|
||||
|
||||
// Convert json to Node struct
|
||||
err = json.NewDecoder(buf).Decode(out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func parseJsonNodes(node *Node) {
|
||||
|
||||
//fmt.Println("parsing node ", node.Name)
|
||||
|
||||
if (node.Type == "url") {
|
||||
fmt.Println(node.Url)
|
||||
} else if (len(node.Children) != 0) { // If node is Folder
|
||||
for _, _childNode := range node.Children {
|
||||
// Type of childNode is interface{}
|
||||
//childNode := Node{}
|
||||
childNode, err := mapToNode(_childNode)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
parseJsonNodes(childNode)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
41
watcher.go
Normal file
41
watcher.go
Normal file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
||||
func watcherThread(watcher *fsnotify.Watcher){
|
||||
for {
|
||||
select {
|
||||
case event := <-watcher.Events:
|
||||
if event.Op&fsnotify.Create == fsnotify.Create &&
|
||||
event.Name == BOOKMARK_FILE {
|
||||
log.Printf("event: %v| name: %v", event.Op, event.Name)
|
||||
log.Println("modified file:", event.Name)
|
||||
|
||||
log.Println("Parsing bookmark")
|
||||
|
||||
f, err := ioutil.ReadFile(BOOKMARK_FILE)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
rootData := RootData{}
|
||||
|
||||
_ = json.Unmarshal(f, &rootData)
|
||||
|
||||
for _, root := range rootData.Roots {
|
||||
parseJsonNodes(&root)
|
||||
}
|
||||
}
|
||||
case err := <-watcher.Errors:
|
||||
log.Println("error:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user