-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.go
126 lines (107 loc) · 3.39 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
// Command gardener helps with test-lists management.
package main
import (
"context"
"os"
"os/signal"
"syscall"
"time"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/ooni/probe-cli/v3/internal/cmd/gardener/internal/dnsfix"
"github.com/ooni/probe-cli/v3/internal/cmd/gardener/internal/dnsreport"
"github.com/ooni/probe-cli/v3/internal/cmd/gardener/internal/sync"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/version"
"github.com/spf13/cobra"
)
// repositoryDir is the path of the citizenlab/test-lists working copy.
const repositoryDir = "citizenlab-test-lists"
// dnsReportDatabase is the path of the database maintained by the dnsreport subcommand.
const dnsReportDatabase = "dnsreport.sqlite3"
func main() {
// select a colourful apex/log handler
log.SetHandler(cli.New(os.Stderr))
// create the root cobra command
rootCmd := &cobra.Command{
Use: "gardener",
Short: "Gardener helps with test-lists management",
Version: version.Version,
}
// create the sync subcommand
syncCmd := &cobra.Command{
Use: "sync",
Short: "Synchronizes the citizenlab/test-lists working copy",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
sc := &sync.Subcommand{
DNSReportDatabase: dnsReportDatabase,
RepositoryDir: repositoryDir,
OsChdir: os.Chdir,
OsGetwd: os.Getwd,
TimeNow: time.Now,
}
sc.Main()
},
}
rootCmd.AddCommand(syncCmd)
// create the dnsreport subcommand
dnsReportCmd := &cobra.Command{
Use: "dnsreport",
Short: "Generates a DNS report from the citizenlab/test-lists working copy",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
sc := &dnsreport.Subcommand{
APIURL: "https://api.ooni.io",
DNSOverHTTPSServerURL: "https://dns.google/dns-query",
Database: dnsReportDatabase,
ReportFile: "dnsreport.csv",
RepositoryDir: repositoryDir,
}
runInterruptible(sc.Main)
},
}
rootCmd.AddCommand(dnsReportCmd)
// create the dnsfix subcommand
dnsFixCmd := &cobra.Command{
Use: "dnsfix",
Short: "Edits the citizenlab/test-lists using the report generated by dnsreport",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
sc := &dnsfix.Subcommand{
ReportFile: "dnsreport.csv",
}
sc.Main()
},
}
rootCmd.AddCommand(dnsFixCmd)
// execute the root command
runtimex.Try0(rootCmd.Execute())
}
// mainSigCh is the signal where we post cancellation requests
var mainSigCh = make(chan os.Signal, 1024)
// runInterruptible runs the given function that takes in input a context
// and arrange for ^C to interrupt the function through the context.
func runInterruptible(fx func(ctc context.Context)) {
// create cancellable context so we can interrupt fx
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// make sure we receive SIGINT
signal.Notify(mainSigCh, syscall.SIGINT)
// run fx in a background goroutine
donech := make(chan any)
go func() {
defer close(donech)
fx(ctx)
}()
select {
case <-mainSigCh: // here we've been interrupted by a signal
log.Warnf("interrupted by signal")
// on signal interrupt the fx function
cancel()
log.Infof("waiting for background workers to terminate")
// await for fx to terminate
<-donech
case <-donech: // this is the normal termination
}
}