diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-06-25 16:34:00 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-06-25 16:34:00 +0200 |
commit | 76fe5414ccc51a2ab7f02f7b55c3251760f5f934 (patch) | |
tree | 3a86e3dc36c9d0d3295972a759bb3e06fe234cf5 | |
parent | 426af6950afe0b25c5428cefee953ec345321319 (diff) | |
download | refcat-76fe5414ccc51a2ab7f02f7b55c3251760f5f934.tar.gz refcat-76fe5414ccc51a2ab7f02f7b55c3251760f5f934.zip |
cdx tweaks
-rw-r--r-- | skate/cdx.go | 26 | ||||
-rw-r--r-- | skate/cmd/skate-cdx-lookup/main.go | 22 |
2 files changed, 37 insertions, 11 deletions
diff --git a/skate/cdx.go b/skate/cdx.go index ecf6812..e7ca808 100644 --- a/skate/cdx.go +++ b/skate/cdx.go @@ -43,15 +43,24 @@ func (b ByDate) Less(i, j int) bool { return b[i].Date < b[j].Date } +type CDXSummary struct { + Last string `json:"last"` + LastOK string `json:"ok"` + DeltaDays int `json:"delta"` +} + +func (s *CDXSummary) String() string { + return fmt.Sprintf("last=%s ok=%s delta=%v", s.Last, s.LastOK, s.DeltaDays) +} + // Summary prints a short, task-specific summary to stdout. -func (c CDX) Summary() string { +func (c CDX) Summary() *CDXSummary { var ( dateLast = "NA" dateLastOK = "NA" - delta = "NA" ) if len(c) == 0 { - return fmt.Sprintf("last=%s ok=%s", dateLast, dateLastOK) + return &CDXSummary{Last: dateLast, LastOK: dateLastOK} } sort.Sort(sort.Reverse(ByDate(c))) dateLast = c[0].Date @@ -62,14 +71,11 @@ func (c CDX) Summary() string { } } d, err := tsDiff(dateLast, dateLastOK) - if err == nil { - if d.Hours()/24 > 365 { - delta = fmt.Sprintf("\033[31;1;4m%0.0f\033[0m", d.Hours()/24) - } else { - delta = fmt.Sprintf("%0.0f", d.Hours()/24) - } + if err != nil { + return &CDXSummary{Last: dateLast, LastOK: dateLastOK} + } else { + return &CDXSummary{Last: dateLast, LastOK: dateLastOK, DeltaDays: int(d.Hours() / 24)} } - return fmt.Sprintf("last=%s ok=%s delta=%v", dateLast, dateLastOK, delta) } // tsDiff returns the duration between two strings timestamps, like: diff --git a/skate/cmd/skate-cdx-lookup/main.go b/skate/cmd/skate-cdx-lookup/main.go index a6fc5f9..1bfbdb8 100644 --- a/skate/cmd/skate-cdx-lookup/main.go +++ b/skate/cmd/skate-cdx-lookup/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "encoding/json" "flag" "fmt" "io" @@ -20,6 +21,7 @@ var ( sleep = flag.Duration("s", 200*time.Millisecond, "sleep between requests") cacheDir = flag.String("c", path.Join(xdg.CacheHome, "skate-cdx"), "cache dir") bestEffort = flag.Bool("B", false, "best effort") + writeJson = flag.Bool("j", false, "json output") quiet = flag.Bool("q", false, "no logging") ) @@ -79,7 +81,25 @@ func main() { log.Fatal(err) } } - fmt.Printf("[%05d] % 10d %s %s\n", i, len(rows), rows.Summary(), line) + if *writeJson { + cdxSummary := rows.Summary() + var dummy = struct { + Summary *skate.CDXSummary `json:"summary"` + NumRows int `json:"numRows"` + Line string `json:"line"` + }{ + Summary: cdxSummary, + NumRows: len(rows), + Line: line, + } + b, err := json.Marshal(dummy) + if err != nil { + log.Fatal(err) + } + fmt.Println(string(b)) + } else { + fmt.Printf("[%05d] % 10d %s %s\n", i, len(rows), rows.Summary(), line) + } i++ } } |