Skip to content

Commit a12f0f4

Browse files
author
norbertwagner
committed
godemon funcs created
1 parent e187503 commit a12f0f4

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

app/app

2.04 MB
Binary file not shown.

app/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@ func main() {
66
fmt.Println("Hello World! 1")
77
fmt.Println("Hello World! 2")
88
fmt.Println("Hello World! 3")
9+
fmt.Println("Hello World! 4")
10+
fmt.Println("Hello World! 5")
11+
fmt.Println("Hello World! 6")
12+
fmt.Println("Hello World! 7")
13+
fmt.Println("Hello world! 8")
14+
fmt.Println("Hello world! 9")
15+
fmt.Println("Hello world! 10")
916
}

main.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"time"
88
)
99

10-
func watchFile(filePath string) error {
11-
initialStat, err := os.Stat(filePath)
10+
func watch(fileordirPath string) error {
11+
initialStat, err := os.Stat(fileordirPath)
1212
if err != nil {
1313
return err
1414
}
1515

1616
for {
17-
stat, err := os.Stat(filePath)
17+
stat, err := os.Stat(fileordirPath)
1818
if err != nil {
1919
return err
2020
}
@@ -31,24 +31,42 @@ func watchFile(filePath string) error {
3131

3232
func main() {
3333
doneChan := make(chan bool)
34+
filepath := os.Args[1]
35+
modOrFile := os.Args[2]
36+
fmt.Println(filepath)
3437
for true {
3538
go func(doneChan chan bool) {
3639
defer func() {
3740
doneChan <- true
3841
}()
3942

40-
err := watchFile("./app/main.go")
43+
err := watch(filepath)
4144
if err != nil {
4245
fmt.Println(err)
4346
}
4447

4548
fmt.Println("File has been changed")
46-
cmd := exec.Command("go", "run", "./app/main.go")
47-
cmd.Stdout = os.Stdout
48-
cmd.Stderr = os.Stderr
49-
cmd.Run()
50-
}(doneChan)
49+
os.Chdir(filepath)
50+
51+
if modOrFile == "mod" {
52+
cmd := exec.Command("go", "build")
53+
cmd.Stdout = os.Stdout
54+
cmd.Stderr = os.Stderr
55+
cmd.Run()
56+
cmd = exec.Command("./app")
57+
cmd.Stdout = os.Stdout
58+
cmd.Stderr = os.Stderr
59+
cmd.Run()
60+
cmd.Process.Kill()
61+
} else if modOrFile == "file" {
62+
cmd := exec.Command("go", "run", filepath)
63+
cmd.Stdout = os.Stdout
64+
cmd.Stderr = os.Stderr
65+
cmd.Run()
66+
cmd.Process.Kill()
67+
}
5168

69+
}(doneChan)
5270
<-doneChan
5371
}
5472
}

0 commit comments

Comments
 (0)