hardware-tests/cmd/net.go

43 lines
1.0 KiB
Go
Raw Normal View History

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"
)
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)
}
}
},
}
func init() {
// add ping command to the main program
rootCmd.AddCommand(netCmd)
}