2022-02-26 00:18:48 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/showwin/speedtest-go/speedtest"
|
|
|
|
"github.com/spf13/cobra"
|
2022-02-26 20:42:24 -05:00
|
|
|
|
|
|
|
"git.metaunix.net/bitgoblin/hardware-tests/lib"
|
2022-02-26 00:18:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var netCmd = &cobra.Command{
|
|
|
|
Use: "net",
|
|
|
|
Short: "Network test using SpeedTest.net.",
|
|
|
|
Long: "A network test that utilizes the SpeedTest.net API to determine network bandwidth and latency.",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
user, _ := speedtest.FetchUserInfo()
|
|
|
|
|
|
|
|
serverList, _ := speedtest.FetchServers(user)
|
|
|
|
targets, _ := serverList.FindServer([]int{})
|
|
|
|
|
|
|
|
for _, s := range targets {
|
|
|
|
s.PingTest()
|
|
|
|
s.DownloadTest(false)
|
|
|
|
s.UploadTest(false)
|
|
|
|
|
2022-02-26 20:42:24 -05:00
|
|
|
// put together the result
|
|
|
|
result := fmt.Sprintf("Latency: %s, Download: %f, Upload: %f\n", s.Latency, s.DLSpeed, s.ULSpeed)
|
|
|
|
// log the result if it's configured, otherwise print it out
|
|
|
|
if Log != "false" {
|
|
|
|
lib.WriteToFile(Log, result)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s", result)
|
|
|
|
}
|
2022-02-26 00:18:48 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// add ping command to the main program
|
|
|
|
rootCmd.AddCommand(netCmd)
|
|
|
|
}
|