-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
120 lines (101 loc) · 2.65 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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"text/tabwriter"
"time"
"github.com/YutaroHayakawa/go-radv"
"github.com/YutaroHayakawa/go-radv/cmd/internal"
"gopkg.in/yaml.v3"
)
func usageRoot() {
fmt.Printf("Usage: %s <subcommand> [options]\n", os.Args[0])
fmt.Println()
fmt.Println("Subcommands:")
fmt.Println(" reload\tReload the configuration")
fmt.Println(" status\tGet the status of the service")
fmt.Println(" help\t\tShow this message")
}
func main() {
if len(os.Args) < 2 {
usageRoot()
os.Exit(1)
}
if os.Args[1] == "help" {
usageRoot()
os.Exit(0)
}
client := internal.NewClient("localhost:8888")
if os.Args[1] == "reload" {
var (
config string
)
command := flag.NewFlagSet("reload", flag.ExitOnError)
command.StringVar(&config, "f", "", "config file path")
command.Parse(os.Args[2:])
reload(client, config)
}
if os.Args[1] == "status" {
var (
output string
)
command := flag.NewFlagSet("status", flag.ExitOnError)
command.StringVar(&output, "o", "table", "Output format (table, json, or yaml)")
command.Parse(os.Args[2:])
status(client, output)
}
}
func reload(client *internal.Client, config string) {
if config == "" {
fmt.Printf("Config file path is required. Aborting.")
os.Exit(1)
}
c, err := radv.ParseConfigYAMLFile(config)
if err != nil {
fmt.Printf("Failed to parse the configuration file: %s\n", err.Error())
os.Exit(1)
}
if err := client.Reload(c); err != nil {
fmt.Printf("Failed to reload daemon: %s\n", err.Error())
os.Exit(1)
}
fmt.Println("Successfully Reloaded!")
os.Exit(0)
}
func status(client *internal.Client, output string) {
status, err := client.Status()
if err != nil {
fmt.Printf("Failed to get daemon status: %s\n", err.Error())
os.Exit(1)
}
switch output {
case "table":
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
fmt.Fprintln(w, "Name\tAge\tTxUnsolicited\tTxSolicited\tState\tMessage")
for _, iface := range status.Interfaces {
age := time.Duration(time.Now().Unix()-iface.LastUpdate) * time.Second
age = age.Round(time.Second)
fmt.Fprintf(w, "%s\t%s\t%d\t%d\t%s\t%s\n", iface.Name, age.String(), iface.TxUnsolicitedRA, iface.TxSolicitedRA, iface.State, iface.Message)
}
w.Flush()
case "json":
j, err := json.MarshalIndent(status, "", " ")
if err != nil {
fmt.Printf("Failed to indent the JSON: %s\n", err.Error())
os.Exit(1)
}
fmt.Print(string(j))
case "yaml":
out, err := yaml.Marshal(status)
if err != nil {
fmt.Printf("Failed to marshal the status: %s\n", err.Error())
os.Exit(1)
}
fmt.Print(string(out))
default:
fmt.Printf("Invalid output format: %s\n", output)
os.Exit(1)
}
}