Skip to content

Commit

Permalink
Accept a negative number as logs since value
Browse files Browse the repository at this point in the history
Typing "-2" will now show logs since now - 2 minutes.

Also, an error during logs command will not cause dry to become
unresponsive.

Fixes #82.
  • Loading branch information
moncho committed Jan 20, 2019
1 parent 11ed2bc commit 06c1096
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/container_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func (h *containersScreenEventHandler) handleCharacter(key rune, f func(eventHan
}, f)
return nil
}); err != nil {

h.dry.appmessage("There was an error showing logs: " + err.Error())
}
case 's', 'S': //stats
Expand Down Expand Up @@ -536,7 +537,7 @@ func (h *containersScreenEventHandler) showLogs(id string, withTimestamp bool, f
f(h)
return
}

since = curateLogsDuration(since)
logs, err := h.dry.dockerDaemon.Logs(id, since, withTimestamp)
if err == nil {
appui.Stream(logs, forwarder.events(), func() {
Expand All @@ -545,6 +546,7 @@ func (h *containersScreenEventHandler) showLogs(id string, withTimestamp bool, f
refreshScreen()
})
} else {
f(h)
h.dry.appmessage("Error showing container logs: " + err.Error())

}
Expand Down
10 changes: 10 additions & 0 deletions app/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/moncho/dry/appui"
"github.com/moncho/dry/ui"
termbox "github.com/nsf/termbox-go"
"strings"
)

func logsPrompt() *appui.Prompt {
Expand Down Expand Up @@ -34,3 +35,12 @@ func inspect(
return nil
}
}

func curateLogsDuration(s string) string {
neg := strings.Index(s, "-")
if neg >= 0 {
return s[neg+1:]
}
return s

}
36 changes: 36 additions & 0 deletions app/misc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package app

import "testing"

func Test_curateLogsDuration(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
"-2",
args{
"-2",
},
"2",
},
{
"2",
args{
"2",
},
"2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := curateLogsDuration(tt.args.s); got != tt.want {
t.Errorf("curateLogsDuration() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 2 additions & 1 deletion app/service_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (h *servicesScreenEventHandler) showLogs(withTimestamp bool, f func(eventHa
}

showServiceLogs := func(serviceID string) error {
since = curateLogsDuration(since)
logs, err := h.dry.dockerDaemon.ServiceLogs(serviceID, since, withTimestamp)
if err == nil {
appui.Stream(logs, forwarder.events(),
Expand All @@ -214,8 +215,8 @@ func (h *servicesScreenEventHandler) showLogs(withTimestamp bool, f func(eventHa
return err
}
if err := h.widget.OnEvent(showServiceLogs); err != nil {
h.dry.appmessage("There was an error showing service logs: " + err.Error())
f(h)
h.dry.appmessage("There was an error showing service logs: " + err.Error())
}
}()
}

0 comments on commit 06c1096

Please sign in to comment.