-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacci-ping.go
More file actions
157 lines (144 loc) · 4.34 KB
/
Copy pathacci-ping.go
File metadata and controls
157 lines (144 loc) · 4.34 KB
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
// Use of this source code is governed by a GPL-2 license that can be found in the LICENSE file.
//
// Copyright 2024-2025 Lexer747
//
// SPDX-License-Identifier: GPL-2.0-only
package main
import (
"flag"
"fmt"
"io"
"os"
acciping "github.com/Lexer747/acci-ping/cmd/subcommands/acci-ping"
"github.com/Lexer747/acci-ping/cmd/subcommands/drawframe"
"github.com/Lexer747/acci-ping/cmd/subcommands/ping"
"github.com/Lexer747/acci-ping/cmd/subcommands/rawdata"
"github.com/Lexer747/acci-ping/cmd/subcommands/version"
tabcompletion "github.com/Lexer747/acci-ping/cmd/tab_completion"
"github.com/Lexer747/acci-ping/terminal/ansi"
"github.com/Lexer747/acci-ping/utils/application"
"github.com/Lexer747/acci-ping/utils/errors"
"github.com/Lexer747/acci-ping/utils/exit"
"github.com/Lexer747/acci-ping/utils/flags"
)
// these looking more bash variables helps clue me into where these
// actually come from which is build time linking, see tools/build.sh.
//
//nolint:staticcheck
var (
COMMIT string
GO_VERSION string
BRANCH string
TIMESTAMP string
TAG string
)
var programName = ansi.Green("acci-ping")
const drawframeString = "drawframe"
const rawdataString = "rawdata"
const pingString = "ping"
const versionString = "version"
type subcommand struct {
subcommandName string
description string
}
var commandsUsage = []subcommand{
{
subcommandName: ansi.Red(drawframeString),
description: programName + " " + ansi.Red(drawframeString) +
" [file|folder]\n will draw a single frame of the graph for a given .pings file, or folder of .pings files.",
},
{
subcommandName: ansi.Red(rawdataString),
description: programName + " " + ansi.Red(rawdataString) +
" will print the statistics and all raw packets found in a .pings file to stdout.",
},
{
subcommandName: ansi.Red(pingString),
description: programName + " " + ansi.Red(pingString) +
" will run like any other ping command line tool and print the plain text packet statistics to stdout.",
},
{
subcommandName: ansi.Red(versionString),
description: programName + " " + ansi.Red(versionString) +
" simply prints the version of this program.",
},
}
var mainDescription = programName + " can run be with no arguments to start the graphing ping accumulator." +
" To exit simply kill the program via the normal control-c."
func main() {
info := application.MakeBuildInfo(COMMIT, GO_VERSION, BRANCH, TIMESTAMP, TAG)
a := acciping.GetFlags(info)
df := drawframe.GetFlags(info)
rd := rawdata.GetFlags()
p := ping.GetFlags()
v := version.GetFlags(info)
if len(os.Args) > 1 {
switch os.Args[1] {
case drawframeString:
flagParseError(df.Parse(os.Args[2:]))
PrintHelpDebugIfNeeded(df.HelpDebug(), df.FlagSet.FlagSet)
drawframe.RunDrawFrame(df)
exit.Success()
case rawdataString:
flagParseError(rd.Parse(os.Args[2:]))
rawdata.RunPrintData(rd)
exit.Success()
case pingString:
flagParseError(p.Parse(os.Args[2:]))
ping.RunPing(p)
exit.Success()
case versionString:
flagParseError(v.Parse(os.Args[2:]))
version.RunVersion(v)
exit.Success()
case tabcompletion.AutoCompleteString:
tabcompletion.Run(
os.Args,
tabcompletion.Command{Cmd: os.Args[0], Fs: a.FlagSet},
[]tabcompletion.Command{
{Cmd: drawframeString, Fs: df.FlagSet},
{Cmd: rawdataString, Fs: rd.FlagSet},
{Cmd: pingString, Fs: p.FlagSet},
{Cmd: versionString, Fs: v.FlagSet},
},
)
exit.Success()
default:
// fallthrough
}
}
a.Usage = func() {
subCommandUsage(a.Output())
if a.HelpDebug() {
flags.PrintFlagsFilter(a.FlagSet, flags.NoFilter())
} else {
flags.PrintFlagsFilter(a.FlagSet, flags.ExcludePrefix("debug"))
}
}
flagParseError(a.Parse(os.Args[1:]))
PrintHelpDebugIfNeeded(a.HelpDebug(), a.FlagSet.FlagSet)
acciping.RunAcciPing(a)
exit.Success()
}
func PrintHelpDebugIfNeeded(b bool, fs *flag.FlagSet) {
if b {
fs.Usage()
exit.Silent()
}
}
func subCommandUsage(w io.Writer) {
fmt.Fprint(w, " "+mainDescription+"\n\n")
for _, cmd := range commandsUsage {
fmt.Fprint(w, " "+cmd.subcommandName+"\n")
fmt.Fprint(w, " "+cmd.description+"\n")
}
fmt.Fprintf(w, "call any of the above subcommands with --help for extra details on those commands.\n")
fmt.Fprint(w, "\n"+programName+" arguments:\n")
}
func flagParseError(err error) {
if errors.Is(err, flag.ErrHelp) {
exit.Silent()
} else {
exit.OnError(err)
}
}