Skip to content

Commit

Permalink
coloooors
Browse files Browse the repository at this point in the history
  • Loading branch information
seletskiy committed Jun 24, 2016
1 parent 3caebd4 commit e3f2fc8
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 68 deletions.
5 changes: 5 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func runRemoteExecution(
outputLock = nil
}

status.SetOutputLock(logLock)

errors := make(chan error, 0)
for _, node := range lockedNodes.nodes {
go func(node *distributedLockNode) {
Expand Down Expand Up @@ -198,6 +200,9 @@ func runRemoteExecutionNode(
)
}

stdout = &statusBarUpdateWriter{stdout}
stderr = &statusBarUpdateWriter{stderr}

if outputLock != (*sync.Mutex)(nil) {
sharedLock := newSharedLock(outputLock, 2)

Expand Down
94 changes: 93 additions & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,85 @@ package main

import (
"fmt"
"os"
"strings"

"github.com/fatih/color"

"github.com/kovetskiy/lorg"
"github.com/seletskiy/hierr"
)

func setupLogger(format *lorg.Format) {
logger.SetFormat(format)
}

func setLoggerOutputFormat(format outputFormat, logger *lorg.Log) {
if format == outputFormatJSON {
logger.SetOutput(&jsonOutputWriter{
stream: `stderr`,
node: ``,
output: os.Stderr,
})
}
}

func setLoggerVerbosity(level verbosity, logger *lorg.Log) {
logger.SetLevel(lorg.LevelWarning)

switch {
case level >= verbosityTrace:
logger.SetLevel(lorg.LevelTrace)

case level >= verbosityDebug:
logger.SetLevel(lorg.LevelDebug)

case level >= verbosityNormal:
logger.SetLevel(lorg.LevelInfo)
}
}

func colorize(
attributes ...color.Attribute,
) string {
if !isColorEnabled {
return ""
}

sequence := []string{}
for _, attribute := range attributes {
sequence = append(sequence, fmt.Sprint(attribute))
}

return fmt.Sprintf("\x1b[%sm", strings.Join(sequence, ";"))
}

func tracef(format string, args ...interface{}) {
if verbose < verbosityTrace {
return
}

args = serializeErrors(args)

logger.Debugf(format, args...)
logger.Tracef(format, args...)

drawStatus()
}

func debugf(format string, args ...interface{}) {
args = serializeErrors(args)

logger.Debugf(format, args...)

drawStatus()
}

func infof(format string, args ...interface{}) {
args = serializeErrors(args)

logger.Infof(format, args...)

drawStatus()
}

func warningf(format string, args ...interface{}) {
Expand All @@ -37,6 +91,8 @@ func warningf(format string, args ...interface{}) {
}

logger.Warningf(format, args...)

drawStatus()
}

func errorf(format string, args ...interface{}) {
Expand All @@ -55,6 +111,42 @@ func serializeErrors(args []interface{}) []interface{} {
return args
}

func shouldDrawStatus() bool {
if !isOutputOnTTY {
return false
}

if format != outputFormatText {
return false
}

if verbose <= verbosityQuiet {
return false
}

if status == nil {
return false
}

return true
}

func drawStatus() {
if !shouldDrawStatus() {
return
}

status.Draw(os.Stderr)
}

func clearStatus() {
if !shouldDrawStatus() {
return
}

status.Clear(os.Stderr)
}

func serializeError(err error) string {
if format == outputFormatText {
return fmt.Sprint(err)
Expand Down
9 changes: 5 additions & 4 deletions distributed_lock_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func (node *distributedLockNode) lock(
filename string,
) error {
lockCommandString := fmt.Sprintf(
`sh -c $'`+
`flock -nx %s -c \'`+
`printf "%s\\n" && cat\' || `+
`printf "%s\\n"'`,
`sh -c "`+
`flock -nx %s -c '`+
`printf \"%s\\n\" && cat' || `+
`printf \"%s\\n\"`+
`"`,
filename,
lockAcquiredString,
lockLockedString,
Expand Down
Loading

0 comments on commit e3f2fc8

Please sign in to comment.