This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
/
main.go
145 lines (125 loc) · 2.52 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
144
145
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"gopkg.in/yaml.v2"
)
type Run struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
SleepSec int `yaml:"sleep_sec"`
}
type Process []Run
const vrcLogName = "output_log"
const vrcRelativeLogPath = `\AppData\LocalLow\VRChat\VRChat\`
func lunch(run Run) error {
cmd := &exec.Cmd{
Path: os.Getenv("COMSPEC"),
SysProcAttr: &syscall.SysProcAttr{
CmdLine: fmt.Sprintf(`/S /C start %s`, run.Path),
// Foreground: true,
}, // when run non windows environment please comment out this line. because this line is window only system call.
}
out, err := cmd.Output()
fmt.Printf("%s\n", out)
return err
}
func UserHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
var debug bool
func setupDebugMode(home string) {
debug = os.Getenv("DEBUG") == "true"
debug = debug || strings.Contains(home, "bootjp")
if debug {
fmt.Println("ENABLE DEBUG MODE")
}
}
func main() {
d, err := os.Getwd()
if err != nil {
fmt.Println("home directory not detected")
log.Fatal(err)
}
data, err := ioutil.ReadFile(d + "/example.yml")
if err != nil {
log.Fatalf("error: %v", err)
}
process := &Process{}
err = yaml.Unmarshal(data, &process)
if err != nil {
log.Fatalf("error: %v", err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer func() {
err = watcher.Close()
if err != nil {
log.Fatal(err)
}
}()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Create != fsnotify.Create {
continue
}
if !strings.Contains(event.Name, vrcLogName) {
continue
}
for _, r := range *process {
go func(r Run) {
log.SetPrefix(r.Name)
if r.SleepSec > 0 {
time.Sleep(time.Duration(r.SleepSec))
}
if err := lunch(r); err != nil {
log.Fatal(err)
}
}(r)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
home := UserHomeDir()
setupDebugMode(home)
if debug {
for _, v := range *process {
fmt.Println(v.Name)
fmt.Println(v.Path)
fmt.Println(v.SleepSec)
}
}
err = watcher.Add(home + vrcRelativeLogPath)
if err != nil {
log.Fatal(err)
}
<-done
}