Skip to content

Commit

Permalink
Merge pull request #612 from smira/610-stderr
Browse files Browse the repository at this point in the history
Print error messagge 'unable to open database' to stderr
  • Loading branch information
smira committed Aug 8, 2017
2 parents a626e46 + e057687 commit e70ef0a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions aptly/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ type Progress interface {
Printf(msg string, a ...interface{})
// ColoredPrintf does printf in colored way + newline
ColoredPrintf(msg string, a ...interface{})
// PrintfStdErr does printf but in safe manner to stderr
PrintfStdErr(msg string, a ...interface{})
}

// Downloader is parallel HTTP fetcher
Expand Down
13 changes: 13 additions & 0 deletions console/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package console

import (
"fmt"
"os"
"strings"

"github.com/cheggaaa/pb"
Expand All @@ -11,6 +12,7 @@ import (

const (
codePrint = iota
codePrintStdErr
codeProgress
codeHideProgress
codeStop
Expand Down Expand Up @@ -127,6 +129,11 @@ func (p *Progress) Printf(msg string, a ...interface{}) {
p.queue <- printTask{code: codePrint, message: fmt.Sprintf(msg, a...)}
}

// PrintfStdErr does printf but in safe manner to stderr
func (p *Progress) PrintfStdErr(msg string, a ...interface{}) {
p.queue <- printTask{code: codePrintStdErr, message: fmt.Sprintf(msg, a...)}
}

// ColoredPrintf does printf in colored way + newline
func (p *Progress) ColoredPrintf(msg string, a ...interface{}) {
if RunningOnTerminal() {
Expand Down Expand Up @@ -182,6 +189,12 @@ func (p *Progress) worker() {
p.barShown = false
}
fmt.Print(task.message)
case codePrintStdErr:
if p.barShown {
fmt.Print("\r\033[2K")
p.barShown = false
}
fmt.Fprint(os.Stderr, task.message)
case codeProgress:
if hasBar {
fmt.Print("\r" + task.message)
Expand Down
2 changes: 1 addition & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (context *AptlyContext) _database() (database.Storage, error) {
delay = time.Second
}

context._progress().Printf("Unable to open database, sleeping %s, attempts left %d...\n", delay, tries)
context._progress().PrintfStdErr("Unable to open database, sleeping %s, attempts left %d...\n", delay, tries)
time.Sleep(delay)
}
}
Expand Down

0 comments on commit e70ef0a

Please sign in to comment.