-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
104 lines (81 loc) · 1.55 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
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"syscall"
"github.com/NHAS/wag/commands"
)
var cmds = []commands.Command{
commands.Start(),
commands.Cleanup(),
commands.Registration(),
commands.Devices(),
commands.Users(),
commands.Firewall(),
commands.Webadmin(),
commands.VersionCmd(),
commands.GenConfig(),
}
func help(full bool) {
fmt.Println("\t\t\tWag")
if full {
fmt.Println("Adds 2fa and device enrolment to wireguard deployments.")
fmt.Print("\n")
}
fmt.Print("Supported commands: ")
for i, r := range cmds {
fmt.Print(r.Name())
if i != len(cmds)-1 {
fmt.Print(", ")
}
}
fmt.Print("\n")
if full {
fmt.Print("\n")
for _, r := range cmds {
r.PrintUsage()
fmt.Print("\n")
}
}
}
func root(args []string) error {
if len(args) < 1 {
help(false)
fmt.Println("No submodule specified (do you need (-h)elp?)")
return nil
}
subcommand := os.Args[1]
for _, cmd := range cmds {
if cmd.Name() == subcommand {
err := cmd.FlagSet().Parse(os.Args[2:])
if err != nil {
return err
}
if err := cmd.Check(); err != nil {
if errors.Is(err, flag.ErrHelp) {
fmt.Println("Error: ", err.Error())
cmd.PrintUsage()
return nil
}
return err
}
return cmd.Run()
}
}
needsHelp := subcommand == "-h" || subcommand == "--help" || subcommand == "-help"
help(needsHelp)
if !needsHelp {
fmt.Printf("Unknown subcommand: %s\n", subcommand)
}
return nil
}
func main() {
syscall.Umask(017)
if err := root(os.Args[1:]); err != nil {
log.Println(err)
os.Exit(1)
}
}