Skip to content

Commit 992e6b8

Browse files
committed
improve logging
Signed-off-by: Joana Hrotko <[email protected]>
1 parent 3a009bc commit 992e6b8

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

cmd/formatter/shortcut.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package formatter
33
import (
44
"context"
55
"fmt"
6-
"strings"
76
"time"
87

98
"github.com/buger/goterm"
@@ -39,12 +38,12 @@ var KeyboardManager *LogKeyboard
3938

4039
var errorColor = "\x1b[1;33m"
4140

42-
func NewKeyboardManager(isDockerDesktopActive, IsWatchConfigured bool, watchFn func(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error) {
41+
func NewKeyboardManager(isDockerDesktopActive, isWatchConfigured bool, watchFn func(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error) {
4342
km := LogKeyboard{}
4443
KeyboardManager = &km
4544
KeyboardManager.Watch.Watching = true
46-
KeyboardManager.IsDockerDesktopActive = true
47-
KeyboardManager.IsWatchConfigured = true
45+
KeyboardManager.IsDockerDesktopActive = isDockerDesktopActive
46+
KeyboardManager.IsWatchConfigured = isWatchConfigured
4847
KeyboardManager.Watch.WatchFn = watchFn
4948
}
5049

@@ -62,9 +61,9 @@ func (lk *LogKeyboard) PrintKeyboardInfo(print func()) {
6261
lk.printInfo()
6362
}
6463

65-
func (lk *LogKeyboard) Error(err error) {
64+
func (lk *LogKeyboard) Error(prefix string, err error) {
6665
lk.ErrorHandle.errStart = time.Now()
67-
lk.ErrorHandle.err = err
66+
lk.ErrorHandle.err = fmt.Errorf("[%s] %s", prefix, err.Error())
6867
}
6968

7069
// This avoids incorrect printing at the end of the terminal
@@ -77,7 +76,7 @@ func (lk *LogKeyboard) createBuffer() {
7776
func (lk *LogKeyboard) printError(height int) {
7877
if lk.ErrorHandle.err != nil && int(time.Since(lk.ErrorHandle.errStart).Seconds()) < DISPLAY_ERROR_TIME {
7978
fmt.Printf("\033[%d;0H", height-1) // Move to before last line
80-
fmt.Printf("\033[K" + errorColor + "[Error] " + lk.ErrorHandle.err.Error())
79+
fmt.Printf("\033[K" + errorColor + lk.ErrorHandle.err.Error())
8180
}
8281
}
8382

@@ -93,15 +92,16 @@ func (lk *LogKeyboard) printInfo() {
9392

9493
func (lk *LogKeyboard) infoMessage() {
9594
options := navColor(" Options: ")
95+
var openDDInfo string
9696
if lk.IsDockerDesktopActive {
97-
options = options + keyColor("^V") + navColor("iew containers in Docker Desktop")
97+
openDDInfo = keyColor("^V") + navColor("iew containers in Docker Desktop")
9898
}
99-
if lk.IsWatchConfigured {
100-
if strings.Contains(options, "Docker Desktop") {
101-
options = options + navColor(", ")
102-
}
103-
options = options + navColor("Enable ") + keyColor("^W") + navColor("atch Mode")
99+
var watchInfo string
100+
if openDDInfo != "" {
101+
watchInfo = navColor(", ")
104102
}
103+
watchInfo = watchInfo + navColor("Enable ") + keyColor("^W") + navColor("atch Mode")
104+
options = options + openDDInfo + watchInfo
105105

106106
fmt.Print("\033[K" + options)
107107
}
@@ -143,13 +143,18 @@ func (lk *LogKeyboard) openDockerDesktop(project *types.Project) {
143143
link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name)
144144
err := open.Run(link)
145145
if err != nil {
146-
lk.Error(fmt.Errorf("could not open Docker Desktop"))
146+
lk.Error("View", fmt.Errorf("could not open Docker Desktop"))
147147
} else {
148-
lk.Error(nil)
148+
lk.Error("", nil)
149149
}
150150
}
151151
}
152+
152153
func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, options api.UpOptions) {
154+
if !lk.IsWatchConfigured {
155+
lk.Error("Watch", fmt.Errorf("Watch is not yet configured. Learn more: https://docs.docker.com/compose/file-watch/"))
156+
return
157+
}
153158
lk.switchWatching()
154159
if lk.isWatching() {
155160
fmt.Println("watching shortcut")
@@ -166,7 +171,7 @@ func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, o
166171
})
167172
errW <- err
168173
}()
169-
lk.Error(<-errW)
174+
lk.Error("Watch", <-errW)
170175
}
171176
}
172177

pkg/compose/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
7777
if err != nil {
7878
panic(err)
7979
}
80-
formatter.NewKeyboardManager(true, true, s.Watch) // change after test
80+
formatter.NewKeyboardManager(true, false, s.Watch) // change after test
8181
// kManager.IsDockerDesktopActive = s.isDesktopIntegrationActive()
8282
// kManager.IsWatchConfigured = s.shouldWatch(project)
8383
defer keyboard.Close()

0 commit comments

Comments
 (0)