-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
148 lines (141 loc) · 3.44 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
146
147
148
// Package main implements a simple CLI to use the library.
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/fatih/color"
"github.com/jesusprubio/up/internal"
)
const (
appName = "up"
appDesc = `
Troubleshoot problems with your Internet connection based on different
protocols and public servers.
OUTPUT
Details about each request:
{Protocol used} {Response time} {Remote server} {Extra info}
EXIT STATUS
This utility exits with one of the following values:
0 At least one response was heard.
2 The transmission was successful but no responses were received.
1 Any other error occurred.
`
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Only used for debugging.
lvl := new(slog.LevelVar)
lvl.Set(slog.LevelError)
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: lvl,
}))
var opts internal.Options
stdin, err := internal.ReadStdin()
if err != nil {
fatal(fmt.Errorf("reading stdin: %w", err))
}
inputs, err := internal.ProcessInputs(stdin)
if err != nil {
fatal(fmt.Errorf("failed to process the inputs: %w", err))
}
opts.Parse()
if opts.Debug {
lvl.Set(slog.LevelDebug)
}
logger.Debug("Starting ...", "options", opts)
dnsProtocol := &internal.DNS{Timeout: opts.Timeout}
if opts.DNSResolver != "" {
dnsProtocol.Resolver = opts.DNSResolver
}
protocols := []internal.Protocol{
&internal.HTTP{Timeout: opts.Timeout},
&internal.TCP{Timeout: opts.Timeout},
dnsProtocol,
}
if opts.Protocol != "" {
var protocol internal.Protocol
for _, p := range protocols {
if p.String() == opts.Protocol {
protocol = p
break
}
}
if protocol == nil {
fatal(fmt.Errorf("unknown protocol: %s", opts.Protocol))
}
protocols = []internal.Protocol{protocol}
}
logger.Info("Starting ...", "protocols", protocols, "count", opts.Count)
if opts.Help {
fmt.Fprintf(os.Stderr, "%s\n", appDesc)
flag.Usage()
os.Exit(1)
}
if opts.NoColor {
color.NoColor = true
}
// To wait for termination signals.
// - 'Interrupt': Ctrl+C from terminal.
// - 'SIGTERM': Sent from Kubernetes.
sigCh := make(chan os.Signal, 1)
defer close(sigCh)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
logger.Debug("Listening for termination signals")
<-sigCh
logger.Debug("Termination signal received")
cancel()
}()
reportCh := make(chan *internal.Report)
defer close(reportCh)
probe := internal.Probe{
Protocols: protocols,
Count: opts.Count,
Delay: opts.Delay,
Logger: logger,
ReportCh: reportCh,
Input: inputs,
}
var format internal.Format
switch {
case opts.JSONOutput:
format = internal.JSONFormat
case opts.GrepOutput:
format = internal.GrepFormat
default:
format = internal.HumanFormat
}
go func() {
logger.Debug("Listening for reports ...")
for report := range probe.ReportCh {
logger.Debug("New report", "report", *report)
repLine, err := report.String(format)
if err != nil {
fatal(err)
}
fmt.Println(repLine)
if report.Error == "" {
if opts.Stop {
logger.Debug("Stopping after first successful request")
cancel()
}
}
}
}()
logger.Debug("Running ...", "setup", probe)
err = probe.Do(ctx)
if err != nil {
fatal(fmt.Errorf("running probe: %w", err))
}
}
// Prints the error to the standard output and exits with status 1.
func fatal(err error) {
fmt.Fprintf(os.Stderr, "%s: %s\n", appName, err)
os.Exit(1)
}