forked from inhies/cjdcmd
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dump.go
165 lines (139 loc) · 3.6 KB
/
dump.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"github.com/fc00/go-cjdns/admin"
"github.com/spf13/cobra"
"os"
)
var (
Pretty bool
StopLevel int
)
func init() {
DumpCmd.Flags().BoolVarP(&Pretty, "pretty", "p", false, "pretty output")
DumpCmd.Flags().IntVarP(&StopLevel, "level", "l", 0, "stop after this many levels")
}
func dumpCmd(cmd *cobra.Command, args []string) {
c := Connect()
table, err := c.NodeStore_dumpTable()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if Pretty {
dumpTablePretty(table)
} else if Verbose {
dumpTableVerbose(table)
} else {
dumpTable(table)
}
}
func dumpTableVerbose(table admin.Routes) {
table.SortByQuality()
k := 1
for _, v := range table {
if v.Link >= 1 {
fmt.Fprintf(os.Stdout,
"%03d IP: %-39v -- Version: %d -- Path: %s -- Link: %s\n",
k, v.IP, v.Version, v.Path, v.Link)
k++
}
}
}
func dumpTable(table admin.Routes) {
for _, v := range table {
if v.Link >= 1 {
fmt.Fprintln(os.Stdout, v.IP)
}
}
}
func dumpTablePretty(table admin.Routes) {
table.SortByPath()
if len(table) < 2 {
return
}
fmt.Fprintf(os.Stdout, "%s┐\n", table[0].Path)
printPrettySubtable(table[1:], "", 0, StopLevel)
}
func printPrettySubtable(table admin.Routes, spacer string, curLevel, stop int) {
curLevel++
if curLevel == stop {
return
}
var sublevels []admin.Routes
for i, here := range table {
if here == nil {
continue
}
// Hit each entry once
table[i] = nil
// make a subtable with here at the front
sublevel := make(admin.Routes, 1)
sublevel[0] = here
for j, there := range table {
if there == nil {
continue
}
if there.Path.IsBehind(*here.Path) {
sublevel = append(sublevel, there)
table[j] = nil
}
}
sublevels = append(sublevels, sublevel)
}
// recurse through sublevels
if len(sublevels) == 1 {
sublevel := sublevels[0]
here := sublevel[0]
if len(sublevel) == 1 {
prettyPrintRoute("%s%s└─ %s\n", spacer, here)
} else {
prettyPrintRoute("%s%s└┬ %s\n", spacer, here)
printPrettySubtable(sublevel[1:], spacer+" ", curLevel, stop)
}
return
}
if len(sublevels) == 0 {
fmt.Fprintf(os.Stdout, " %s└╌┄┈ \n", spacer)
return
}
for _, sublevel := range sublevels[:len(sublevels)-1] {
here := sublevel[0]
if len(sublevel) == 1 {
prettyPrintRoute("%s%s├─ %s\n", spacer, here)
continue
}
prettyPrintRoute("%s%s├┬ %s\n", spacer, here)
printPrettySubtable(sublevel[1:], spacer+"│", curLevel, stop)
}
sublevel := sublevels[len(sublevels)-1]
here := sublevel[0]
if len(sublevel) == 1 {
prettyPrintRoute("%s%s└─ %s\n", spacer, here)
return
}
prettyPrintRoute("%s%s└┬ %s\n", spacer, here)
printPrettySubtable(sublevel[1:], spacer+" ", curLevel, stop)
}
func prettyPrintRoute(format, spacer string, route *admin.Route) {
ip := route.IP.String()
host, err := resolveIP(ip)
if err == nil {
fmt.Fprintf(os.Stdout, format, route.Path, spacer, host)
} else {
fmt.Fprintf(os.Stdout, format, route.Path, spacer, ip)
}
}