Skip to content

Commit 3a009bc

Browse files
committed
partial cleanup
Signed-off-by: Joana Hrotko <[email protected]>
1 parent d71a2de commit 3a009bc

File tree

3 files changed

+116
-130
lines changed

3 files changed

+116
-130
lines changed

cmd/formatter/logs.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,7 @@ func (l *logConsumer) write(w io.Writer, container, message string) {
109109
if l.ctx.Err() != nil {
110110
return
111111
}
112-
// p := l.getPresenter(container)
113-
// timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
114-
// for _, line := range strings.Split(message, "\n") {
115-
// if l.timestamp {
116-
// fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
117-
// } else {
118-
// fmt.Fprintf(w, "%s%s\n", p.prefix, line)
119-
// }
120-
// }
121-
KeyboardManager.PrintKeyboardInfo(func() {
112+
print := func() {
122113
p := l.getPresenter(container)
123114
timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
124115
for _, line := range strings.Split(message, "\n") {
@@ -128,7 +119,13 @@ func (l *logConsumer) write(w io.Writer, container, message string) {
128119
fmt.Fprintf(w, "\033[K%s%s\n", p.prefix, line)
129120
}
130121
}
131-
})
122+
}
123+
if KeyboardManager != nil {
124+
KeyboardManager.PrintKeyboardInfo(print)
125+
} else {
126+
// FIXME: Need to handle this case
127+
print()
128+
}
132129
}
133130

134131
func (l *logConsumer) Status(container, msg string) {

cmd/formatter/shortcut.go

Lines changed: 103 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,53 @@ import (
77
"time"
88

99
"github.com/buger/goterm"
10+
"github.com/compose-spec/compose-go/v2/types"
11+
"github.com/docker/compose/v2/pkg/api"
1012
"github.com/docker/compose/v2/pkg/watch"
13+
"github.com/eiannone/keyboard"
14+
"github.com/skratchdot/open-golang/open"
1115
)
1216

1317
var DISPLAY_ERROR_TIME = 10
1418

19+
type KeyboardError struct {
20+
err error
21+
errStart time.Time
22+
}
23+
type KeyboardWatch struct {
24+
Watcher watch.Notify
25+
Watching bool
26+
WatchFn func(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error
27+
Ctx context.Context
28+
Cancel context.CancelFunc
29+
}
1530
type LogKeyboard struct {
16-
err error
17-
errStart time.Time
31+
ErrorHandle KeyboardError
32+
Watch KeyboardWatch
1833
started bool
1934
IsDockerDesktopActive bool
20-
Watcher watch.Notify
21-
Watching bool
22-
Ctx context.Context
23-
Cancel context.CancelFunc
35+
IsWatchConfigured bool
2436
}
2537

26-
var KeyboardManager = LogKeyboard{Watching: true}
38+
var KeyboardManager *LogKeyboard
39+
2740
var errorColor = "\x1b[1;33m"
2841

29-
func (lk *LogKeyboard) NewContext(ctx context.Context) context.CancelFunc {
30-
ctx, cancel := context.WithCancel(ctx)
31-
lk.Ctx = ctx
32-
lk.Cancel = cancel
33-
return cancel
42+
func NewKeyboardManager(isDockerDesktopActive, IsWatchConfigured bool, watchFn func(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error) {
43+
km := LogKeyboard{}
44+
KeyboardManager = &km
45+
KeyboardManager.Watch.Watching = true
46+
KeyboardManager.IsDockerDesktopActive = true
47+
KeyboardManager.IsWatchConfigured = true
48+
KeyboardManager.Watch.WatchFn = watchFn
3449
}
3550

3651
func (lk *LogKeyboard) PrintKeyboardInfo(print func()) {
3752
fmt.Print("\033[?25l") // hide cursor
3853
defer fmt.Printf("\033[?25h") // show cursor
3954

4055
if lk.started {
41-
lk.ClearInfo()
56+
lk.clearInfo()
4257
} else {
4358
lk.started = true
4459
}
@@ -47,13 +62,9 @@ func (lk *LogKeyboard) PrintKeyboardInfo(print func()) {
4762
lk.printInfo()
4863
}
4964

50-
func (lk *LogKeyboard) SError(err string) {
51-
lk.errStart = time.Now()
52-
lk.err = fmt.Errorf(err)
53-
}
5465
func (lk *LogKeyboard) Error(err error) {
55-
lk.errStart = time.Now()
56-
lk.err = err
66+
lk.ErrorHandle.errStart = time.Now()
67+
lk.ErrorHandle.err = err
5768
}
5869

5970
// This avoids incorrect printing at the end of the terminal
@@ -64,9 +75,9 @@ func (lk *LogKeyboard) createBuffer() {
6475
}
6576

6677
func (lk *LogKeyboard) printError(height int) {
67-
if lk.err != nil && int(time.Since(lk.errStart).Seconds()) < DISPLAY_ERROR_TIME {
78+
if lk.ErrorHandle.err != nil && int(time.Since(lk.ErrorHandle.errStart).Seconds()) < DISPLAY_ERROR_TIME {
6879
fmt.Printf("\033[%d;0H", height-1) // Move to before last line
69-
fmt.Printf("\033[K" + errorColor + "[Error] " + lk.err.Error())
80+
fmt.Printf("\033[K" + errorColor + "[Error] " + lk.ErrorHandle.err.Error())
7081
}
7182
}
7283

@@ -85,7 +96,7 @@ func (lk *LogKeyboard) infoMessage() {
8596
if lk.IsDockerDesktopActive {
8697
options = options + keyColor("^V") + navColor("iew containers in Docker Desktop")
8798
}
88-
if lk.Watching {
99+
if lk.IsWatchConfigured {
89100
if strings.Contains(options, "Docker Desktop") {
90101
options = options + navColor(", ")
91102
}
@@ -95,10 +106,10 @@ func (lk *LogKeyboard) infoMessage() {
95106
fmt.Print("\033[K" + options)
96107
}
97108

98-
func (lk *LogKeyboard) ClearInfo() {
109+
func (lk *LogKeyboard) clearInfo() {
99110
height := goterm.Height()
100111
fmt.Print("\0337") // save cursor position
101-
if lk.err != nil {
112+
if lk.ErrorHandle.err != nil {
102113
fmt.Printf("\033[%d;0H", height-1)
103114
fmt.Print("\033[2K") // clear line
104115
}
@@ -108,53 +119,74 @@ func (lk *LogKeyboard) ClearInfo() {
108119
}
109120

110121
func (lk *LogKeyboard) PrintEnter() {
111-
lk.ClearInfo()
122+
lk.clearInfo()
112123
lk.printInfo()
113124
}
114125

115-
// func HandleKeyEvents(ctx context.Context, event keyboard.KeyEvent, project types.Project, options api.UpOptions, handleTearDown func()) {
116-
// switch key := event.Key; key {
117-
// case keyboard.KeyCtrlC:
118-
// keyboard.Close()
119-
// KeyboardManager.ClearInfo()
120-
// handleTearDown()
121-
// case keyboard.KeyCtrlG:
122-
// if KeyboardManager.IsDockerDesktopActive {
123-
// link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name)
124-
// err := open.Run(link)
125-
// if err != nil {
126-
// KeyboardManager.SError("Could not open Docker Desktop")
127-
// } else {
128-
// KeyboardManager.Error(nil)
129-
// }
130-
// }
131-
// case keyboard.KeyCtrlW:
132-
// if KeyboardManager.Watching {
133-
// KeyboardManager.Watching = !KeyboardManager.Watching
134-
// fmt.Println("watching shortcut", KeyboardManager.Watching)
135-
136-
// if KeyboardManager.Watching {
137-
// KeyboardManager.Cancel()
138-
// } else {
139-
// KeyboardManager.NewContext(ctx)
140-
// quit := make(chan error)
141-
// go func() {
142-
// buildOpts := *options.Create.Build
143-
// buildOpts.Quiet = true
144-
// err := s.Watch(KeyboardManager.Ctx, project, options.Start.Services, api.WatchOptions{
145-
// Build: &buildOpts,
146-
// LogTo: options.Start.Attach,
147-
// })
148-
// quit <- err
149-
// }()
150-
// KeyboardManager.Error(<-quit)
151-
// }
152-
// }
153-
// case keyboard.KeyEnter:
154-
// KeyboardManager.PrintEnter()
155-
// default:
156-
// if key != 0 { // If some key is pressed
157-
// fmt.Println("key pressed: ", key)
158-
// }
159-
// }
160-
// }
126+
func (lk *LogKeyboard) isWatching() bool {
127+
return lk.Watch.Watching
128+
}
129+
130+
func (lk *LogKeyboard) switchWatching() {
131+
lk.Watch.Watching = !lk.Watch.Watching
132+
}
133+
134+
func (lk *LogKeyboard) newContext(ctx context.Context) context.CancelFunc {
135+
ctx, cancel := context.WithCancel(ctx)
136+
lk.Watch.Ctx = ctx
137+
lk.Watch.Cancel = cancel
138+
return cancel
139+
}
140+
141+
func (lk *LogKeyboard) openDockerDesktop(project *types.Project) {
142+
if lk.IsDockerDesktopActive {
143+
link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name)
144+
err := open.Run(link)
145+
if err != nil {
146+
lk.Error(fmt.Errorf("could not open Docker Desktop"))
147+
} else {
148+
lk.Error(nil)
149+
}
150+
}
151+
}
152+
func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, options api.UpOptions) {
153+
lk.switchWatching()
154+
if lk.isWatching() {
155+
fmt.Println("watching shortcut")
156+
lk.Watch.Cancel()
157+
} else {
158+
lk.newContext(ctx)
159+
errW := make(chan error)
160+
go func() {
161+
buildOpts := *options.Create.Build
162+
buildOpts.Quiet = true
163+
err := lk.Watch.WatchFn(lk.Watch.Ctx, project, options.Start.Services, api.WatchOptions{
164+
Build: &buildOpts,
165+
LogTo: options.Start.Attach,
166+
})
167+
errW <- err
168+
}()
169+
lk.Error(<-errW)
170+
}
171+
}
172+
173+
func (lk *LogKeyboard) HandleKeyEvents(ctx context.Context, event keyboard.KeyEvent, project *types.Project, options api.UpOptions, handleTearDown func()) {
174+
switch kRune := event.Rune; kRune {
175+
case 'V':
176+
lk.openDockerDesktop(project)
177+
case 'W':
178+
lk.StartWatch(ctx, project, options)
179+
}
180+
switch key := event.Key; key {
181+
case keyboard.KeyCtrlC:
182+
keyboard.Close()
183+
lk.clearInfo()
184+
handleTearDown()
185+
case keyboard.KeyEnter:
186+
lk.PrintEnter()
187+
default:
188+
if key != 0 { // If some key is pressed
189+
fmt.Println("key pressed: ", key)
190+
}
191+
}
192+
}

pkg/compose/up.go

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/docker/compose/v2/pkg/progress"
3232
"github.com/eiannone/keyboard"
3333
"github.com/hashicorp/go-multierror"
34-
"github.com/skratchdot/open-golang/open"
3534
)
3635

3736
func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo
@@ -78,10 +77,11 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
7877
if err != nil {
7978
panic(err)
8079
}
81-
// formatter.KeyboardInfo.IsDockerDesktopActive = s.isDesktopIntegrationActive()
82-
formatter.KeyboardManager.IsDockerDesktopActive = true
83-
// formatter.KeyboardInfo.Watching = s.shouldWatch(project)
80+
formatter.NewKeyboardManager(true, true, s.Watch) // change after test
81+
// kManager.IsDockerDesktopActive = s.isDesktopIntegrationActive()
82+
// kManager.IsWatchConfigured = s.shouldWatch(project)
8483
defer keyboard.Close()
84+
8585
first := true
8686
gracefulTeardown := func() {
8787
printer.Cancel()
@@ -100,50 +100,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
100100
for {
101101
select {
102102
case event := <-kEvents:
103-
switch key := event.Key; key {
104-
case keyboard.KeyCtrlC:
105-
keyboard.Close()
106-
formatter.KeyboardManager.ClearInfo()
107-
gracefulTeardown()
108-
case keyboard.KeyCtrlG:
109-
if formatter.KeyboardManager.IsDockerDesktopActive {
110-
link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name)
111-
err := open.Run(link)
112-
if err != nil {
113-
formatter.KeyboardManager.SError("Could not open Docker Desktop")
114-
} else {
115-
formatter.KeyboardManager.Error(nil)
116-
}
117-
}
118-
case keyboard.KeyCtrlW:
119-
if formatter.KeyboardManager.Watching {
120-
formatter.KeyboardManager.Watching = !formatter.KeyboardManager.Watching
121-
fmt.Println("watching shortcut", formatter.KeyboardManager.Watching)
122-
123-
if formatter.KeyboardManager.Watching {
124-
formatter.KeyboardManager.Cancel()
125-
} else {
126-
formatter.KeyboardManager.NewContext(ctx)
127-
errW := make(chan error)
128-
go func() {
129-
buildOpts := *options.Create.Build
130-
buildOpts.Quiet = true
131-
err := s.Watch(formatter.KeyboardManager.Ctx, project, options.Start.Services, api.WatchOptions{
132-
Build: &buildOpts,
133-
LogTo: options.Start.Attach,
134-
})
135-
errW <- err
136-
}()
137-
formatter.KeyboardManager.Error(<-errW)
138-
}
139-
}
140-
case keyboard.KeyEnter:
141-
formatter.KeyboardManager.PrintEnter()
142-
default:
143-
if key != 0 { // If some key is pressed
144-
fmt.Println("key pressed: ", key)
145-
}
146-
}
103+
formatter.KeyboardManager.HandleKeyEvents(ctx, event, project, options, gracefulTeardown)
147104
case <-doneCh:
148105
return nil
149106
case <-ctx.Done():

0 commit comments

Comments
 (0)