-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (115 loc) · 3.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"flag"
"fmt"
"github.com/fsnotify/fsnotify"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)
var watcher *fsnotify.Watcher
var (
server string
username string
password string
shellScript string
rootPath string
delay int
ignoreArg string
ignores []string
)
func init() {
flag.StringVar(&server, "server", "", "Specifies the VM UUID or VM name")
flag.StringVar(&username, "username", "", "Specifies the user name on guest OS under which the process should run. This user name must already exist on the guest OS.")
flag.StringVar(&password, "password", "", "Password for given user name")
flag.StringVar(&shellScript, "shellScript", "", "Path to the shell script that will be executed when filesystem change occurs")
flag.StringVar(&rootPath, "path", "", "Root path where to watch for changes")
flag.IntVar(&delay, "delay", 100, "Delay in milliseconds before notifying about a file that's changed")
flag.StringVar(&ignoreArg, "ignore", "node_modules;.git;.idea", "Semicolon-separated list of directories to ignore. "+
"Glob expressions are supported.")
}
func main() {
flag.Parse()
ignores = strings.Split(ignoreArg, ";")
watcher, _ = fsnotify.NewWatcher()
defer watcher.Close()
if rootPath == "" {
rootPath = "."
}
if err := filepath.Walk(rootPath, watchDir); err != nil {
fmt.Println("ERROR", err)
}
// Map of filenames we're currently notifying about.
var processes sync.Map
for {
select {
case event := <-watcher.Events:
switch event.Op {
case fsnotify.Write:
if _, ok := processes.Load(event.Name); ok {
continue
}
processes.Store(event.Name, nil)
go func(event fsnotify.Event) {
defer processes.Delete(event.Name)
// Wait for further events to accomodate the way editors
// save files.
time.Sleep(time.Duration(delay) * time.Millisecond)
// Ensure the file hasn't been renamed or removed.
if _, ok := processes.Load(event.Name); !ok {
return
}
notifyVM(event)
}(event)
case fsnotify.Rename, fsnotify.Remove:
processes.Delete(event.Name)
}
case err := <-watcher.Errors:
fmt.Println("Error: ", err)
}
}
}
func notifyVM(event fsnotify.Event) {
if event.Op != fsnotify.Write {
return
}
var str strings.Builder
str.WriteString("--nologo guestcontrol ")
str.WriteString(server)
str.WriteString(" run --exe /bin/bash --username ")
str.WriteString(username)
str.WriteString(" --password ")
str.WriteString(password)
str.WriteString(" --wait-stdout --wait-stderr --unquoted-args -- bash/arg0 ")
str.WriteString( shellScript )
args := strings.Fields( str.String() )
cmd := exec.Command("VBoxManage", args[0:]...)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Error: %s\n", err)
}
fmt.Printf("%s\n", stdoutStderr)
}
func watchDir(path string, fi os.FileInfo, err error) error {
if !fi.Mode().IsDir() {
return nil
}
// Ignore hidden directories.
if len(path) > 1 && strings.HasPrefix(path, ".") {
return filepath.SkipDir
}
for _, pattern := range ignores {
ok, err := filepath.Match(pattern, fi.Name())
if err != nil {
return err
}
if ok {
return filepath.SkipDir
}
}
fmt.Println("Watching ", path)
return watcher.Add(path)
}