Skip to content

Commit

Permalink
Adds scribe logging implementation
Browse files Browse the repository at this point in the history
[#169791315]

Co-authored-by: Timothy Hitchener <[email protected]>
  • Loading branch information
Ryan Moran and thitch97 committed Nov 26, 2019
1 parent 7600411 commit a77405d
Show file tree
Hide file tree
Showing 13 changed files with 681 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ go 1.12

require (
code.cloudfoundry.org/lager v2.0.0+incompatible
github.com/cheggaaa/pb v2.0.7+incompatible
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/onsi/ginkgo v1.10.2
github.com/onsi/gomega v1.7.0
github.com/sclevine/spec v1.3.0
gopkg.in/VividCortex/ewma.v1 v1.1.1 // indirect
gopkg.in/cheggaaa/pb.v2 v2.0.7 // indirect
gopkg.in/fatih/color.v1 v1.7.0 // indirect
gopkg.in/mattn/go-colorable.v0 v0.1.0 // indirect
gopkg.in/mattn/go-isatty.v0 v0.0.4 // indirect
gopkg.in/mattn/go-runewidth.v0 v0.0.4 // indirect
)
28 changes: 28 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
code.cloudfoundry.org/lager v2.0.0+incompatible h1:WZwDKDB2PLd/oL+USK4b4aEjUymIej9My2nUQ9oWEwQ=
code.cloudfoundry.org/lager v2.0.0+incompatible/go.mod h1:O2sS7gKP3HM2iemG+EnwvyNQK7pTSC6Foi4QiMp9sSk=
github.com/cheggaaa/pb v2.0.7+incompatible h1:gLKifR1UkZ/kLkda5gC0K6c8g+jU2sINPtBeOiNlMhU=
github.com/cheggaaa/pb v2.0.7+incompatible/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.2 h1:uqH7bpe+ERSiDa34FDOF7RikN6RzXgduUF8yarlZp94=
github.com/onsi/ginkgo v1.10.2/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/sclevine/spec v1.3.0 h1:iTB51CYlnju5oRh0/l67fg1+RlQ2nqmFecwdvN+5TrI=
github.com/sclevine/spec v1.3.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/VividCortex/ewma.v1 v1.1.1 h1:tWHEKkKq802K/JT9RiqGCBU5fW3raAPnJGTE9ostZvg=
gopkg.in/VividCortex/ewma.v1 v1.1.1/go.mod h1:TekXuFipeiHWiAlO1+wSS23vTcyFau5u3rxXUSXj710=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/cheggaaa/pb.v2 v2.0.7 h1:beaAg8eacCdMQS9Y7obFEtkY7gQl0uZ6Zayb3ry41VY=
gopkg.in/cheggaaa/pb.v2 v2.0.7/go.mod h1:0CiZ1p8pvtxBlQpLXkHuUTpdJ1shm3OqCF1QugkjHL4=
gopkg.in/fatih/color.v1 v1.7.0 h1:bYGjb+HezBM6j/QmgBfgm1adxHpzzrss6bj4r9ROppk=
gopkg.in/fatih/color.v1 v1.7.0/go.mod h1:P7yosIhqIl/sX8J8UypY5M+dDpD2KmyfP5IRs5v/fo0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/mattn/go-colorable.v0 v0.1.0 h1:WYuADWvfvYC07fm8ygYB3LMcsc5CunpxfMGKawHkAos=
gopkg.in/mattn/go-colorable.v0 v0.1.0/go.mod h1:BVJlBXzARQxdi3nZo6f6bnl5yR20/tOL6p+V0KejgSY=
gopkg.in/mattn/go-isatty.v0 v0.0.4 h1:NtS1rQGQr4IaFWBGz4Cz4BhB///gyys4gDVtKA7hIsc=
gopkg.in/mattn/go-isatty.v0 v0.0.4/go.mod h1:wt691ab7g0X4ilKZNmMII3egK0bTxl37fEn/Fwbd8gc=
gopkg.in/mattn/go-runewidth.v0 v0.0.4 h1:r0P71TnzQDlNIcizCqvPSSANoFa3WVGtcNJf3TWurcY=
gopkg.in/mattn/go-runewidth.v0 v0.0.4/go.mod h1:BmXejnxvhwdaATwiJbB1vZ2dtXkQKZGu9yLFCZb4msQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
Expand Down
34 changes: 34 additions & 0 deletions scribe/bar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package scribe

import (
"io"

"github.com/cheggaaa/pb"
)

type Bar struct {
w io.Writer
p *pb.ProgressBar
}

func NewBar(w io.Writer) *Bar {
return &Bar{
w: w,
p: pb.New(100),
}
}

func (b *Bar) Start() {
b.p.Start()
b.p.SetWriter(b.w)
b.p.SetWidth(100)
b.p.SetTemplateString(`{{bar . "[" "-" ">" " " "]"}} {{percent .}} {{etime .}}`)
}

func (b *Bar) Increment() {
b.p.Increment()
}

func (b *Bar) Finish() {
b.p.Finish()
}
36 changes: 36 additions & 0 deletions scribe/bar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package scribe_test

import (
"bytes"
"testing"

"github.com/cloudfoundry/packit/scribe"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
)

func testBar(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

buffer *bytes.Buffer
bar *scribe.Bar
)

it.Before(func() {
buffer = bytes.NewBuffer(nil)

bar = scribe.NewBar(buffer)
})

it("renders a progress bar to the writer", func() {
bar.Start()
for i := 0; i < 40; i++ {
bar.Increment()
}
bar.Finish()

Expect(buffer.String()).To(Equal("[-----------------------------------> ] 40.00% 0s"))
})
}
43 changes: 43 additions & 0 deletions scribe/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package scribe

import (
"strconv"
)

var (
BlackColor = NewColor(false, 0, -1)
RedColor = NewColor(false, 1, -1)
GreenColor = NewColor(false, 2, -1)
YellowColor = NewColor(false, 3, -1)
BlueColor = NewColor(false, 4, -1)
MagentaColor = NewColor(false, 5, -1)
CyanColor = NewColor(false, 6, -1)
WhiteColor = NewColor(false, 7, -1)
GrayColor = NewColor(false, 244, -1)
)

type Color func(message string) string

func NewColor(bold bool, fg, bg int) Color {
return func(message string) string {
prefix := "\x1b["
if bold {
prefix = prefix + "1"
} else {
prefix = prefix + "0"
}

if fg >= 0 {
prefix = prefix + ";38;5;" + strconv.Itoa(fg)
}

if bg >= 0 {
prefix = prefix + ";48;5;" + strconv.Itoa(bg)
}

prefix = prefix + "m"
suffix := "\x1b[0m"

return prefix + message + suffix
}
}
82 changes: 82 additions & 0 deletions scribe/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package scribe_test

import (
"testing"

"github.com/cloudfoundry/packit/scribe"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
)

func testColor(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect

it("returns a function that wraps a string in color codes", func() {
redFgColor := scribe.NewColor(false, 1, -1)
Expect(redFgColor("some-text")).To(Equal("\x1b[0;38;5;1msome-text\x1b[0m"))

blueBgColor := scribe.NewColor(false, -1, 4)
Expect(blueBgColor("some-text")).To(Equal("\x1b[0;48;5;4msome-text\x1b[0m"))

magentaBoldFgColor := scribe.NewColor(true, 5, -1)
Expect(magentaBoldFgColor("some-text")).To(Equal("\x1b[1;38;5;5msome-text\x1b[0m"))

mixedFgBgColor := scribe.NewColor(false, 3, 244)
Expect(mixedFgBgColor("some-text")).To(Equal("\x1b[0;38;5;3;48;5;244msome-text\x1b[0m"))
})

context("BlackColor", func() {
it("returns a function that wraps a string in black color codes", func() {
Expect(scribe.BlackColor("some-text")).To(Equal("\x1b[0;38;5;0msome-text\x1b[0m"))
})
})

context("RedColor", func() {
it("returns a function that wraps a string in red color codes", func() {
Expect(scribe.RedColor("some-text")).To(Equal("\x1b[0;38;5;1msome-text\x1b[0m"))
})
})

context("GreenColor", func() {
it("returns a function that wraps a string in green color codes", func() {
Expect(scribe.GreenColor("some-text")).To(Equal("\x1b[0;38;5;2msome-text\x1b[0m"))
})
})

context("YellowColor", func() {
it("returns a function that wraps a string in yellow color codes", func() {
Expect(scribe.YellowColor("some-text")).To(Equal("\x1b[0;38;5;3msome-text\x1b[0m"))
})
})

context("BlueColor", func() {
it("returns a function that wraps a string in blue color codes", func() {
Expect(scribe.BlueColor("some-text")).To(Equal("\x1b[0;38;5;4msome-text\x1b[0m"))
})
})

context("MagentaColor", func() {
it("returns a function that wraps a string in magenta color codes", func() {
Expect(scribe.MagentaColor("some-text")).To(Equal("\x1b[0;38;5;5msome-text\x1b[0m"))
})
})

context("CyanColor", func() {
it("returns a function that wraps a string in cyan color codes", func() {
Expect(scribe.CyanColor("some-text")).To(Equal("\x1b[0;38;5;6msome-text\x1b[0m"))
})
})

context("WhiteColor", func() {
it("returns a function that wraps a string in white color codes", func() {
Expect(scribe.WhiteColor("some-text")).To(Equal("\x1b[0;38;5;7msome-text\x1b[0m"))
})
})

context("GrayColor", func() {
it("returns a function that wraps a string in gray color codes", func() {
Expect(scribe.GrayColor("some-text")).To(Equal("\x1b[0;38;5;244msome-text\x1b[0m"))
})
})
}
22 changes: 22 additions & 0 deletions scribe/formatted_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package scribe

import (
"sort"
"strings"
)

type FormattedList []string

func (l FormattedList) String() string {
sort.Strings(l)

var builder strings.Builder
for i, elem := range l {
builder.WriteString(elem)
if i < len(l)-1 {
builder.WriteRune('\n')
}
}

return builder.String()
}
24 changes: 24 additions & 0 deletions scribe/formatted_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scribe_test

import (
"testing"

"github.com/cloudfoundry/packit/scribe"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
)

func testFormattedList(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect

context("String", func() {
it("returns a formatted string representation of the list", func() {
Expect(scribe.FormattedList{
"third",
"first",
"second",
}.String()).To(Equal("first\nsecond\nthird"))
})
})
}
40 changes: 40 additions & 0 deletions scribe/formatted_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package scribe

import (
"fmt"
"sort"
"strings"
)

type FormattedMap map[string]interface{}

func (m FormattedMap) String() string {
var (
keys []string
padding int
)
for key := range m {
if len(key) > padding {
padding = len(key)
}
keys = append(keys, key)
}

sort.Strings(keys)

var builder strings.Builder
for _, key := range keys {
value := m[key]
if value == nil {
value = "<empty>"
}

for i := len(key); i < padding; i++ {
key = key + " "
}

builder.WriteString(fmt.Sprintf("%s -> %v\n", key, value))
}

return strings.TrimSpace(builder.String())
}
24 changes: 24 additions & 0 deletions scribe/formatted_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scribe_test

import (
"testing"

"github.com/cloudfoundry/packit/scribe"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
)

func testFormattedMap(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect

context("String", func() {
it("returns a formatted string representation of the map", func() {
Expect(scribe.FormattedMap{
"third": 3,
"first": 1,
"second": 2,
}.String()).To(Equal("first -> 1\nsecond -> 2\nthird -> 3"))
})
})
}
Loading

0 comments on commit a77405d

Please sign in to comment.