forked from naggie/dsnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsnet.go
151 lines (132 loc) · 3.72 KB
/
dsnet.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
149
150
151
package main
import (
"errors"
"fmt"
"strings"
"github.com/naggie/dsnet"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
// Flags.
owner string
description string
confirm bool
// Commands.
rootCmd = &cobra.Command{}
initCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
dsnet.Init()
},
Use: "init",
Short: fmt.Sprintf(
"Create %s containing default configuration + new keys without loading. Edit to taste.",
dsnet.CONFIG_FILE,
),
}
addCmd = &cobra.Command{
PreRunE: func(cmd *cobra.Command, args []string) error {
// Make sure we have the hostname
if len(args) != 1 {
return errors.New("Missing hostname argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
dsnet.Add(args[0], owner, description, confirm)
},
Use: "add [hostname]",
Short: "Add a new peer + sync",
}
regenerateCmd = &cobra.Command{
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("Missing hostname argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
dsnet.Regenerate(args[0], confirm)
},
Use: "regenerate [hostname]",
Short: "Regenerate keys and config for peer",
}
upCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
dsnet.Up()
},
Use: "up",
Short: "Create the interface, run pre/post up, sync",
}
syncCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
dsnet.Sync()
},
Use: "sync",
Short: fmt.Sprintf("Update wireguard configuration from %s after validating", dsnet.CONFIG_FILE),
}
reportCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
dsnet.Report()
},
Use: "report",
Short: fmt.Sprintf("Generate a JSON status report to the location configured in %s.", dsnet.CONFIG_FILE),
}
removeCmd = &cobra.Command{
PreRunE: func(cmd *cobra.Command, args []string) error {
// Make sure we have the hostname
if len(args) != 1 {
return errors.New("Missing hostname argument")
}
viper.Set("hostname", args[0])
return nil
},
Run: func(cmd *cobra.Command, args []string) {
dsnet.Remove(args[0], confirm)
},
Use: "remove [hostname]",
Short: "Remove a peer by hostname provided as argument + sync",
}
downCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
dsnet.Down()
},
Use: "down",
Short: "Destroy the interface, run pre/post down",
}
versionCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("dsnet version %s\ncommit %s\nbuilt %s", dsnet.VERSION, dsnet.GIT_COMMIT, dsnet.BUILD_DATE)
},
Use: "version",
Short: "Print version",
}
)
func main() {
// Flags.
rootCmd.PersistentFlags().String("output", "wg-quick", "config file format: vyatta/wg-quick/nixos")
addCmd.Flags().StringVar(&owner, "owner", "", "owner of the new peer")
addCmd.Flags().StringVar(&description, "description", "", "description of the new peer")
addCmd.Flags().BoolVar(&confirm, "confirm", false, "confirm")
removeCmd.Flags().BoolVar(&confirm, "confirm", false, "confirm")
// Environment variable handling.
viper.AutomaticEnv()
viper.SetEnvPrefix("DSNET")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if err := viper.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output")); err != nil {
dsnet.ExitFail(err.Error())
}
// Adds subcommands.
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(regenerateCmd)
rootCmd.AddCommand(upCmd)
rootCmd.AddCommand(syncCmd)
rootCmd.AddCommand(reportCmd)
rootCmd.AddCommand(removeCmd)
rootCmd.AddCommand(downCmd)
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
dsnet.ExitFail(err.Error())
}
}