forked from nextdns/nextdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (67 loc) · 1.73 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
package main
import (
"fmt"
"os"
"runtime"
)
var (
version = "dev"
platform = runtime.GOOS
)
type command struct {
name string
run func(args []string) error
desc string
}
var commands = []command{
{"install", svc, "install service init on the system"},
{"uninstall", svc, "uninstall service init from the system"},
{"start", svc, "start installed service"},
{"stop", svc, "stop installed service"},
{"restart", svc, "restart installed service"},
{"status", svc, "return service status"},
{"log", svc, "show service logs"},
{"upgrade", upgrade, "upgrade the cli to the latest version"},
{"run", run, "run the daemon"},
{"config", cfg, "manage configuration"},
{"activate", activation, "setup the system to use NextDNS as a resolver"},
{"deactivate", activation, "restore the resolver configuration"},
{"discovered", ctlCmd, "display discovered clients"},
{"cache-stats", ctlCmd, "display cache statistics"},
{"cache-keys", ctlCmd, "dump the list of cached entries"},
{"trace", ctlCmd, "display a stack trace dump"},
{"version", showVersion, "show current version"},
}
func showCommands() {
fmt.Println("Usage: nextdns <command> [arguments]")
fmt.Println("")
fmt.Println("The commands are:")
fmt.Println("")
for _, cmd := range commands {
fmt.Printf(" %-15s %s\n", cmd.name, cmd.desc)
}
fmt.Println("")
os.Exit(1)
}
func showVersion(args []string) error {
fmt.Printf("nextdns version %s\n", version)
return nil
}
func main() {
if len(os.Args) < 2 {
showCommands()
}
cmd := os.Args[1]
for _, c := range commands {
if c.name != cmd {
continue
}
if err := c.run(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
return
}
// Command not found
showCommands()
}