Skip to content

Commit

Permalink
feat: ability to disable log colors
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Jun 20, 2019
1 parent 66c9046 commit 5d000b0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app-builder-bin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "app-builder-bin",
"description": "app-builder precompiled binaries",
"version": "2.6.17",
"version": "2.6.18",
"files": [
"*.js",
"mac",
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ require (
github.com/develar/go-pkcs12 v0.0.0-20181115143544-54baa4f32c6a
github.com/disintegration/imaging v1.6.0
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.7.0
github.com/golang/protobuf v1.3.1 // indirect
github.com/json-iterator/go v1.1.6
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.2
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/mitchellh/go-homedir v1.1.0
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/disintegration/imaging v1.6.0 h1:nVPXRUUQ36Z7MNf0O77UzgnOb1mkMMor7lmJ
github.com/disintegration/imaging v1.6.0/go.mod h1:xuIt+sRxDFrHS0drzXUlCJthkJ8k7lkkUojDSR247MQ=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
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/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand All @@ -45,6 +47,10 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75 h1:Pijfgr7ZuvX7QIQiEwLdRVr3RoMG+i0SbBO1Qu+7yVk=
github.com/mcuadros/go-version v0.0.0-20190308113854-92cdf37c5b75/go.mod h1:76rfSfYPWj01Z85hUf/ituArm797mNKcvINh1OlsZKo=
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
Expand Down Expand Up @@ -88,6 +94,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181021155630-eda9bb28ed51/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/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-20190620070143-6f217b454f45 h1:Dl2hc890lrizvUppGbRWhnIh2f8jOTCQpY5IKWRS0oM=
golang.org/x/sys v0.0.0-20190620070143-6f217b454f45/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
return
}

var app = kingpin.New("app-builder", "app-builder").Version("2.6.17")
var app = kingpin.New("app-builder", "app-builder").Version("2.6.18")

node_modules.ConfigureCommand(app)
//codesign.ConfigureCommand(app)
Expand Down
2 changes: 1 addition & 1 deletion pkg/download/Part.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (part *Part) download(context context.Context, url string, index int, clien
}

request = request.WithContext(context)
request.Header.Set("User-Agent", userAgent)
request.Header.Set("User-Agent", getUserAgent())
if part.End > 0 {
request.Header.Set("Range", part.getRange())
}
Expand Down
62 changes: 38 additions & 24 deletions pkg/log-cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"fmt"
"io"
"os"
"strings"
"sync"

"github.com/apex/log"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
)

func InitLogger() {
Expand All @@ -16,26 +19,25 @@ func InitLogger() {
if isDebugDefined && debugEnv != "false" {
log.SetLevel(log.DebugLevel)
}

forceColor := os.Getenv("FORCE_COLOR")
if forceColor == "0" {
color.NoColor = true
} else if forceColor == "1" || forceColor == "true" {
color.NoColor = false
}
}

// Default handler outputting to stderr.
var Default = New(os.Stderr)

// colors.
const (
red = 31
yellow = 33
blue = 34
gray = 37
)

// Colors mapping.
var Colors = [...]int{
log.DebugLevel: gray,
log.InfoLevel: blue,
log.WarnLevel: yellow,
log.ErrorLevel: red,
log.FatalLevel: red,
var Colors = [...]*color.Color{
log.DebugLevel: color.New(color.FgWhite),
log.InfoLevel: color.New(color.FgBlue),
log.WarnLevel: color.New(color.FgYellow),
log.ErrorLevel: color.New(color.FgRed),
log.FatalLevel: color.New(color.FgRed),
}

// Strings mapping.
Expand All @@ -56,36 +58,48 @@ type Handler struct {

// New handler.
func New(w io.Writer) *Handler {
if !color.NoColor {
if f, ok := w.(*os.File); ok {
return &Handler{
Writer: colorable.NewColorable(f),
Padding: 2,
}
}
}

return &Handler{
Writer: w,
// change default padding from 3 to 2 (as electron-builder does, more compact)
Writer: w,
Padding: 2,
}
}

// HandleLog implements log.Handler.
func (h *Handler) HandleLog(e *log.Entry) error {
color := Colors[e.Level]
myColor := Colors[e.Level]
level := Strings[e.Level]
names := e.Fields.Names()

h.mu.Lock()
defer h.mu.Unlock()

_, _ = fmt.Fprintf(h.Writer, "\033[%dm%*s\033[0m %-25s", color, h.Padding+1, level, e.Message)
_, _ = myColor.Fprintf(h.Writer, "%*s ", h.Padding+1, level)
_, _ = fmt.Fprintf(h.Writer, "%s%s", e.Message, strings.Repeat(" ", max(1, 15 /* because first field adds space before */ - len(e.Message))))

for _, name := range names {
if name == "source" {
continue
}

_, _ = fmt.Fprintf(h.Writer, " \033[%dm%s\033[0m=%v", color, name, e.Fields.Get(name))
_, _ = fmt.Fprintf(h.Writer, " %s=%v", myColor.Sprint(name), e.Fields.Get(name))
}

_, err := fmt.Fprintln(h.Writer)
if err != nil {
return err
}
_, _ = fmt.Fprintln(h.Writer)

return nil
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
4 changes: 1 addition & 3 deletions pkg/package-format/snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/develar/app-builder/pkg/linuxTools"
"github.com/develar/app-builder/pkg/util"
"github.com/develar/errors"
fsutil "github.com/develar/go-fs-util"
"github.com/develar/go-fs-util"
"github.com/mcuadros/go-version"
)

Expand All @@ -44,8 +44,6 @@ type SnapOptions struct {
extraAppArgs *string
excludedAppFiles *[]string

dockerImage *string

arch *string
output *string
}
Expand Down

0 comments on commit 5d000b0

Please sign in to comment.