From 9f3fd4733ae85dd26ac3a3963016a6217e9c2cee Mon Sep 17 00:00:00 2001 From: Cameron Bergoon Date: Tue, 20 Nov 2018 20:59:32 -0500 Subject: [PATCH] Adds speedtest package to enable use of api --- speedtest.go | 14 +++----------- request.go => speedtest/request.go | 2 +- server.go => speedtest/server.go | 4 ++-- user.go => speedtest/user.go | 4 ++-- speedtest/util.go | 13 +++++++++++++ 5 files changed, 21 insertions(+), 16 deletions(-) rename request.go => speedtest/request.go (99%) rename server.go => speedtest/server.go (98%) rename user.go => speedtest/user.go (95%) create mode 100644 speedtest/util.go diff --git a/speedtest.go b/speedtest.go index c5a164e..85fdb22 100644 --- a/speedtest.go +++ b/speedtest.go @@ -1,18 +1,10 @@ package main import ( - "log" - "os" - "gopkg.in/alecthomas/kingpin.v2" + "github.com/cbergoon/speedtest-go/speedtest" ) -func checkError(err error) { - if err != nil { - log.Fatal(err) - os.Exit(1) - } -} func setTimeout() { if *timeoutOpt != 0 { @@ -33,10 +25,10 @@ func main() { setTimeout() - user := fetchUserInfo() + user := speedtest.FetchUserInfo() user.Show() - list := fetchServerList(user) + list := speedtest.FetchServerList(user) if *showList { list.Show() return diff --git a/request.go b/speedtest/request.go similarity index 99% rename from request.go rename to speedtest/request.go index abe2a1f..008194e 100644 --- a/request.go +++ b/speedtest/request.go @@ -1,4 +1,4 @@ -package main +package speedtest import ( "fmt" diff --git a/server.go b/speedtest/server.go similarity index 98% rename from server.go rename to speedtest/server.go index be2dfd2..11fe621 100644 --- a/server.go +++ b/speedtest/server.go @@ -1,4 +1,4 @@ -package main +package speedtest import ( "bytes" @@ -55,7 +55,7 @@ func (b ByDistance) Less(i, j int) bool { return b.Servers[i].Distance < b.Servers[j].Distance } -func fetchServerList(user User) ServerList { +func FetchServerList(user User) ServerList { // Fetch xml server data resp, err := http.Get("http://www.speedtest.net/speedtest-servers-static.php") checkError(err) diff --git a/user.go b/speedtest/user.go similarity index 95% rename from user.go rename to speedtest/user.go index c7d0bee..326150b 100644 --- a/user.go +++ b/speedtest/user.go @@ -1,4 +1,4 @@ -package main +package speedtest import ( "bytes" @@ -21,7 +21,7 @@ type Users struct { Users []User `xml:"client"` } -func fetchUserInfo() User { +func FetchUserInfo() User { // Fetch xml user data resp, err := http.Get("http://speedtest.net/speedtest-config.php") checkError(err) diff --git a/speedtest/util.go b/speedtest/util.go new file mode 100644 index 0000000..0961039 --- /dev/null +++ b/speedtest/util.go @@ -0,0 +1,13 @@ +package speedtest + +import ( + "log" + "os" +) + +func checkError(err error) { + if err != nil { + log.Fatal(err) + os.Exit(1) + } +}