Added initial command structure; added ping network test command
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
46
cmd/ping.go
Normal file
46
cmd/ping.go
Normal file
@ -0,0 +1,46 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-ping/ping"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
host string
|
||||
count int
|
||||
)
|
||||
|
||||
var pingCmd = &cobra.Command{
|
||||
Use: "ping",
|
||||
Short: "Ping test.",
|
||||
Long: "Uses ping to test network latency and aliveness.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
pinger, err := ping.NewPinger(host)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
pinger.Count = count
|
||||
pinger.OnFinish = func(stats *ping.Statistics) {
|
||||
fmt.Printf("--- %s ping statistics ---\n", stats.Addr)
|
||||
fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n",
|
||||
stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
|
||||
fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
|
||||
stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
|
||||
}
|
||||
fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
|
||||
err = pinger.Run() // Blocks until finished.
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
pingCmd.Flags().StringVarP(&host, "host", "a", "8.8.8.8", "Host to attempt pinging.")
|
||||
pingCmd.Flags().IntVarP(&count, "count", "c", 50, "Number of pings to send.")
|
||||
|
||||
// add ping command to the main program
|
||||
rootCmd.AddCommand(pingCmd)
|
||||
}
|
21
cmd/root.go
Normal file
21
cmd/root.go
Normal file
@ -0,0 +1,21 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "bgbench",
|
||||
Short: "Bit Goblin hardware benching tool.",
|
||||
Long: `A PC hardware benchmarking tool written by Bit Goblin.`,
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user