From 98cfe89b05b7273624cb1a063bce59c71264516c Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 23 Feb 2015 19:06:28 -0500 Subject: [PATCH] Fix formatting --- store/store.go | 14 +++++---- tildescores/scores.go | 69 ++++++++++++++++++++++--------------------- where/where.go | 68 +++++++++++++++++++++--------------------- 3 files changed, 78 insertions(+), 73 deletions(-) diff --git a/store/store.go b/store/store.go index d76837a..44ab75a 100644 --- a/store/store.go +++ b/store/store.go @@ -1,11 +1,11 @@ package store import ( - "os" - "fmt" "bufio" - "strings" + "fmt" "io/ioutil" + "os" + "strings" ) type Row struct { @@ -24,7 +24,7 @@ func ReadData(path string) []byte { func ReadRows(path, delimiter string) *[]Row { f, _ := os.Open(path) - + defer f.Close() rows := []Row{} @@ -41,10 +41,12 @@ func ReadRows(path, delimiter string) *[]Row { } func WriteData(path string, data []byte) { - f, err := os.OpenFile(path, os.O_CREATE | os.O_RDWR | os.O_TRUNC, 0644) + f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644) if err != nil { fmt.Println(err) } + // TODO: check for Close() errors + // https://github.com/ncw/swift/blob/master/swift.go#L170 defer f.Close() _, err = f.Write(data) @@ -54,7 +56,7 @@ func WriteData(path string, data []byte) { } func WriteRows(path string, rows *[]Row, delimeter string) { - f, err := os.OpenFile(path, os.O_CREATE | os.O_RDWR, 0644) + f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644) if err != nil { fmt.Println(err) } diff --git a/tildescores/scores.go b/tildescores/scores.go index 5376e74..b8edd7a 100644 --- a/tildescores/scores.go +++ b/tildescores/scores.go @@ -1,30 +1,30 @@ package main import ( + "bufio" + "flag" + "fmt" + "io/ioutil" "os" "os/exec" - "fmt" - "time" - "flag" "sort" - "bufio" "strconv" "strings" - "io/ioutil" "text/template" + "time" "github.com/thebaer/tildes/store" ) var ( - scoresPath = "/home/krowbar/Code/irc/tildescores.txt" - jackpotPath = "/home/krowbar/Code/irc/tildejackpot.txt" + scoresPath = "/home/krowbar/Code/irc/tildescores.txt" + jackpotPath = "/home/krowbar/Code/irc/tildejackpot.txt" addictionData = "/home/karlen/bin/tilderoyale" ) const ( scoreDeltasPath = "/home/bear/scoredeltas.txt" - deltaDelimiter = "+++" + deltaDelimiter = "+++" ) func main() { @@ -41,7 +41,7 @@ func main() { addictionData = "/home/bear/addicted.sh" } - headers := []string{ "User", "Tildes", "Last Collected", "Addiction", "# Asks", "Avg.", "Last Amt." } + headers := []string{"User", "Tildes", "Last Collected", "Addiction", "# Asks", "Avg.", "Last Amt."} scoresData := store.ReadRows(scoresPath, "&^%") updatesData := store.ReadRows(scoreDeltasPath, deltaDelimiter) @@ -54,21 +54,24 @@ func main() { type table struct { Headers []string - Rows []store.Row + Rows []store.Row } type By func(r1, r2 *store.Row) bool + func (by By) Sort(rows []store.Row) { - rs := &rowSorter { + rs := &rowSorter{ rows: rows, - by: by, + by: by, } sort.Sort(rs) } + type rowSorter struct { rows []store.Row - by func(r1, r2 *store.Row) bool + by func(r1, r2 *store.Row) bool } + func (r *rowSorter) Len() int { return len(r.rows) } @@ -117,20 +120,20 @@ func niceTime(sec int) string { if sec < 60 { return fmt.Sprintf("%dsec", sec) } else if sec < 3600 { - return fmt.Sprintf("%smin", trimTrailingZerosShort(float64(sec) / 60.0)) + return fmt.Sprintf("%smin", trimTrailingZerosShort(float64(sec)/60.0)) } else if sec < 86400 { - return fmt.Sprintf("%shr", trimTrailingZerosShort(float64(sec) / 3600.0)) + return fmt.Sprintf("%shr", trimTrailingZerosShort(float64(sec)/3600.0)) } - return fmt.Sprintf("%sdy", trimTrailingZerosShort(float64(sec) / 86400.0)) + return fmt.Sprintf("%sdy", trimTrailingZerosShort(float64(sec)/86400.0)) } type LastScore struct { - LastUpdate int - LastScore int + LastUpdate int + LastScore int LastIncrement int - Times int - ScoreOffset int - Addiction int + Times int + ScoreOffset int + Addiction int } func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row { @@ -150,7 +153,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row { times, _ := strconv.Atoi(r.Data[4]) so, _ := strconv.Atoi(r.Data[5]) - users[r.Data[0]] = LastScore{ LastScore: score, LastUpdate: update, LastIncrement: inc, Times: times, ScoreOffset: so } + users[r.Data[0]] = LastScore{LastScore: score, LastUpdate: update, LastIncrement: inc, Times: times, ScoreOffset: so} } // Fetch IRC log data @@ -175,7 +178,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row { u, exists := users[uname] if !exists { - u = LastScore{ ScoreOffset: 0 } + u = LastScore{ScoreOffset: 0} } u.ScoreOffset = 0 u.Times = asks @@ -197,7 +200,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row { // Fill in any missing users if !exists { - u = LastScore{ LastScore: score, LastIncrement: -1, LastUpdate: update, Times: 0, ScoreOffset: score, Addiction: 0 } + u = LastScore{LastScore: score, LastIncrement: -1, LastUpdate: update, Times: 0, ScoreOffset: score, Addiction: 0} users[r.Data[0]] = u } @@ -210,7 +213,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row { } r.Data = append(r.Data, niceTime(u.Addiction)) - + var asksStr string if u.Times > 0 { asksStr = strconv.Itoa(u.Times) @@ -221,7 +224,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row { var avgStr string if u.Times > 0 { - avg := float64(score - u.ScoreOffset) / float64(u.Times) + avg := float64(score-u.ScoreOffset) / float64(u.Times) avgStr = trimTrailingZeros(avg) } else { avgStr = "-" @@ -241,7 +244,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row { } // Write deltas - f, err := os.OpenFile(scoreDeltasPath, os.O_CREATE | os.O_RDWR, 0644) + f, err := os.OpenFile(scoreDeltasPath, os.O_CREATE|os.O_RDWR, 0644) if err != nil { fmt.Println(err) } @@ -285,11 +288,11 @@ func getFile(path string) string { } type Page struct { - Title string - Table table - Updated string + Title string + Table table + Updated string UpdatedForHumans string - Jackpot int + Jackpot int } func add(x, y int) int { @@ -303,10 +306,10 @@ func generate(title, jackpot string, table *table, outputFile string) { if err != nil { panic(err) } - + defer f.Close() - funcMap := template.FuncMap { + funcMap := template.FuncMap{ "add": add, } diff --git a/where/where.go b/where/where.go index 1d9d0ed..2f24498 100644 --- a/where/where.go +++ b/where/where.go @@ -1,21 +1,21 @@ package main import ( - "encoding/json" - "strings" "bufio" - "os" - "io/ioutil" - "os/exec" - "fmt" - "regexp" - "net/http" - "flag" - "time" - "text/template" "crypto/hmac" "crypto/sha256" "encoding/base64" + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "net/http" + "os" + "os/exec" + "regexp" + "strings" + "text/template" + "time" "github.com/thebaer/geo" "github.com/thebaer/tildes/store" @@ -56,22 +56,22 @@ func main() { } type user struct { - Name string `json:"name"` - IP string `json:"ip"` - Region string `json:"region"` - Country string `json:"country"` - CurrentTime string `json:"current_time"` - Latitude float64 `json:"lat"` - Longitude float64 `json:"lng"` - Public bool - Anonymous bool + Name string `json:"name"` + IP string `json:"ip"` + Region string `json:"region"` + Country string `json:"country"` + CurrentTime string `json:"current_time"` + Latitude float64 `json:"lat"` + Longitude float64 `json:"lng"` + Public bool + Anonymous bool } type publicUser struct { - Name string `json:"name"` - Region string `json:"region"` - Country string `json:"country"` - Latitude float64 `json:"lat"` + Name string `json:"name"` + Region string `json:"region"` + Country string `json:"country"` + Latitude float64 `json:"lat"` Longitude float64 `json:"lng"` } @@ -116,7 +116,7 @@ func who() []user { for ip, name := range ips { users[i] = user{Name: name, IP: ip, Public: true, Anonymous: false} - // Get user permissions, marking if they're not opted-in with a + // Get user permissions, marking if they're not opted-in with a // `.here` file in their $HOME dir. if _, err := os.Stat("/home/" + name + "/.here"); os.IsNotExist(err) { users[i].Public = false @@ -185,10 +185,10 @@ func getGeo(u *user) { } func computeHmac256(message string) string { - key := []byte(hashSecret) - h := hmac.New(sha256.New, key) - h.Write([]byte(message)) - return base64.StdEncoding.EncodeToString(h.Sum(nil)) + key := []byte(hashSecret) + h := hmac.New(sha256.New, key) + h.Write([]byte(message)) + return base64.StdEncoding.EncodeToString(h.Sum(nil)) } func getFuzzyCoords(u *user, apiKey string) { @@ -254,8 +254,8 @@ func prettyLocation(region, country string) string { } type page struct { - Users []user - Updated string + Users []user + Updated string UpdatedForHumans string } @@ -266,13 +266,13 @@ func generate(users []user, outputFile string) { if err != nil { panic(err) } - + defer f.Close() - funcMap := template.FuncMap { + funcMap := template.FuncMap{ "Location": prettyLocation, } - + w := bufio.NewWriter(f) template, err := template.New("").Funcs(funcMap).ParseFiles("../templates/where.html") if err != nil {