package cmd import ( "fmt" "github.com/showwin/speedtest-go/speedtest" "github.com/spf13/cobra" "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) // 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) }