breto

A status bar written in Go.
Log | Files | Refs | README | LICENSE

commit a98156b566b179914dd47d7393304c588aef6922
parent 9ecf162d95a6ca0390e12eb72ffe38850e0490c9
Author: J. R. Swab <jrswab@protonmail.com>
Date:   Fri, 31 May 2019 20:22:27 +0000

Merge branch 'errorAndTests' into 'master'

Error and tests

See merge request jrswab/dwm-status!9
Diffstat:
MCHANGELOG.md | 8++++++++
Dlibs/ram/ram.go | 35-----------------------------------
Dlibs/wttr/wttr.go | 34----------------------------------
Mmain.go | 16++++++++++++----
Aram/ram.go | 35+++++++++++++++++++++++++++++++++++
Aram/ram_test.go | 27+++++++++++++++++++++++++++
Awttr/wttr.go | 35+++++++++++++++++++++++++++++++++++
Awttr/wttr_test.go | 28++++++++++++++++++++++++++++
8 files changed, 145 insertions(+), 73 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.2.5] - +### Added +- Go test for wttr package +- Another Go channel for wttr errors + +### Changed +- location of package directories to top level + ## [1.2.4] - May 29, 2019 ### Changed - Removed the counters from the bottom of main.go and cleaned up comments diff --git a/libs/ram/ram.go b/libs/ram/ram.go @@ -1,35 +0,0 @@ -package ram - -import ( - "fmt" - "math" - "os/exec" - "strings" - "time" -) - -func Free(cRam chan string) { - var passed, tenSecs float64 - start := time.Now() // set to determine seconds passed - ticker := time.NewTicker(time.Second) - - for range ticker.C { // run every 10 seconds - passed = time.Since(start).Seconds() // get total seconds passed - tenSecs = math.Floor(math.Remainder(passed, 10)) - - if passed < 5 || tenSecs == 0 { // trigger: asap or divisible by ten - ramFree := "" - ramCmd := "free -h | gawk '/Mem:/ {print $4}'" // set shell command - - ramGib, err := exec.Command("sh", "-c", ramCmd).Output() // run and save the output - if err != nil { - cRam <- err.Error() - } - - ramFree = fmt.Sprintf("Ram: %s free | ", - strings.TrimSpace(string(ramGib))) // set string - - cRam <- ramFree // send string - } - } -} diff --git a/libs/wttr/wttr.go b/libs/wttr/wttr.go @@ -1,34 +0,0 @@ -package wttr - -import ( - "fmt" - "io/ioutil" - "math" - "net/http" - "strings" - "time" -) - -func Local(cWttr chan string) { - var passed, hour float64 - start := time.Now() // to determine seconds passed - ticker := time.NewTicker(time.Second) - - for range ticker.C { - passed = time.Since(start).Seconds() // total seconds passed - hour = math.Floor(math.Remainder(passed, 3600)) - - if passed < 10 || hour == 0 { - resp, err := http.Get("https://wttr.in/?format=%t+%w") // for options see https://wttr.in/:help - if err != nil { - errMessage := "wttr connection issue" - cWttr <- errMessage - } - - bodyData, _ := ioutil.ReadAll(resp.Body) - weather := fmt.Sprintf("%s | ", - strings.TrimSpace(string(bodyData))) // convert responce to string for return - cWttr <- weather - } - } -} diff --git a/main.go b/main.go @@ -2,21 +2,25 @@ package main import ( "fmt" - "gitlab.com/jrswab/dwm-status/libs/ram" - "gitlab.com/jrswab/dwm-status/libs/wttr" + "gitlab.com/jrswab/dwm-status/ram" + "gitlab.com/jrswab/dwm-status/wttr" + "log" "os/exec" "time" ) func main() { var status, hTime, weather, ramFree string + var wttrErr, ramErr error var cmd *exec.Cmd cWttr := make(chan string) // start weather data routine - go wttr.Local(cWttr) + eWttr := make(chan error) + go wttr.Local(cWttr, eWttr) cRam := make(chan string) // start free ram data routine - go ram.Free(cRam) + eRam := make(chan error) + go ram.Free(cRam, eRam) ticker := time.NewTicker(time.Second) for range ticker.C { @@ -24,7 +28,11 @@ func main() { select { // update the go routine channels as they send data case weather = <-cWttr: + case wttrErr = <-eWttr: + log.Println(wttrErr.Error()) case ramFree = <-cRam: + case ramErr = <-eRam: + log.Println(ramErr.Error()) default: } diff --git a/ram/ram.go b/ram/ram.go @@ -0,0 +1,35 @@ +package ram + +import ( + "fmt" + "math" + "os/exec" + "strings" + "time" +) + +func Free(cRam chan string, eRam chan error) { + var passed, tenSecs float64 + start := time.Now() // set to determine seconds passed + ticker := time.NewTicker(time.Second) + + for range ticker.C { // run every 10 seconds + passed = time.Since(start).Seconds() // get total seconds passed + tenSecs = math.Floor(math.Remainder(passed, 10)) + + if passed < 5 || tenSecs == 0 { // trigger: asap or divisible by ten + ramFree := "" + ramCmd := "free -h | gawk '/Mem:/ {print $4}'" // set shell command + + ramGib, err := exec.Command("sh", "-c", ramCmd).Output() // run and save the output + if err != nil { + eRam <- err + } + + ramFree = fmt.Sprintf("Ram: %s free | ", + strings.TrimSpace(string(ramGib))) // set string + + cRam <- ramFree // send string + } + } +} diff --git a/ram/ram_test.go b/ram/ram_test.go @@ -0,0 +1,27 @@ +package ram + +import ( + "fmt" + "testing" +) + +// A basic test to make sure the function runs +// and outputs data instead of an error +func TestFree(t *testing.T) { + cRam := make(chan string) + eRam := make(chan error) + var ram string + var runError error + + go Free(cRam, eRam) + + select { // grab first avalible channel + case ram = <-cRam: + case runError = <-eRam: + } + + if runError != nil { + t.Error("Expected Free Ram got:", runError.Error()) + } + fmt.Println(ram) +} diff --git a/wttr/wttr.go b/wttr/wttr.go @@ -0,0 +1,35 @@ +package wttr + +import ( + "fmt" + "io/ioutil" + "math" + "net/http" + "strings" + "time" +) + +func Local(cWttr chan string, eWttr chan error) { + var passed, hour float64 + start := time.Now() // to determine seconds passed + ticker := time.NewTicker(time.Second) + + for range ticker.C { + passed = time.Since(start).Seconds() // total seconds passed + hour = math.Floor(math.Remainder(passed, 3600)) + + if passed < 10 || hour == 0 { + // for options see https://wttr.in/:help + resp, err := http.Get("https://wttr.in/?format=%t+%w") + if err != nil { + eWttr <- err + } + + bodyData, _ := ioutil.ReadAll(resp.Body) + // convert responce to string for go channel + weather := fmt.Sprintf("%s | ", + strings.TrimSpace(string(bodyData))) + cWttr <- weather + } + } +} diff --git a/wttr/wttr_test.go b/wttr/wttr_test.go @@ -0,0 +1,28 @@ +package wttr + +import ( + "fmt" + "testing" +) + +// A basic test to make sure the function runs +// and outputs data over instead of an error +func TestLocal(t *testing.T) { + cWttr := make(chan string) + eWttr := make(chan error) + var weather string + var wttrError error + + go Local(cWttr, eWttr) + + // wait for weather or error + select { + case weather = <-cWttr: + case wttrError = <-eWttr: + } + + if wttrError != nil { + t.Error("Expected Weather Data got:", wttrError.Error()) + } + fmt.Println(weather) +}