-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
106 lines (90 loc) · 2.18 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"sync"
"time"
)
// This is a helper application which can made some stdout and stderr outputs.
// It will be used in the chain_test.go and is not part of the library. It exists
// only for test purposes.
func main() {
toErr := flag.String("e", "", "write this value to stderr")
toOut := flag.String("o", "", "write this value to stdout")
tickOut := flag.Duration("to", 0, "write one line at out per interval (see -ti) for X time")
tickErr := flag.Duration("te", 0, "write one line at err per interval (see -ti) for X time")
tickInt := flag.Duration("ti", 1*time.Second, "in which interval should the lines be written")
printEnv := flag.Bool("pe", false, "print environment variables to stdout")
printWorkDir := flag.Bool("pwd", false, "print the current working directory to stdout")
exitCode := flag.Int("x", 0, "the exit code")
flag.Parse()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs)
go func() {
<-sigs
os.Exit(125)
}()
if toErr != nil && *toErr != "" {
println(*toErr)
}
if toOut != nil && *toOut != "" {
fmt.Println(*toOut)
}
if *printEnv {
env := os.Environ()
for _, curEnv := range env {
fmt.Println(curEnv)
}
}
if *printWorkDir {
wd, _ := os.Getwd()
fmt.Println(wd)
}
wg := sync.WaitGroup{}
handleOut(tickOut, tickInt, &wg)
handleErr(tickErr, tickInt, &wg)
wg.Wait()
if exitCode != nil {
os.Exit(*exitCode)
}
}
func handleOut(tickOut *time.Duration, tickInt *time.Duration, wg *sync.WaitGroup) {
if tickOut != nil && *tickOut != 0 {
timer := time.NewTimer(*tickOut)
ticker := time.NewTicker(*tickInt)
wg.Add(1)
go func() {
defer wg.Done()
outLoop:
for {
select {
case <-ticker.C:
fmt.Fprintf(os.Stdout, "OUT\n")
case <-timer.C:
break outLoop
}
}
}()
}
}
func handleErr(tickErr *time.Duration, tickInt *time.Duration, wg *sync.WaitGroup) {
if tickErr != nil && *tickErr != 0 {
timer := time.NewTimer(*tickErr)
ticker := time.NewTicker(*tickInt)
wg.Add(1)
go func() {
defer wg.Done()
errLoop:
for {
select {
case <-ticker.C:
fmt.Fprintf(os.Stderr, "ERR\n")
case <-timer.C:
break errLoop
}
}
}()
}
}