Skip to content

Commit

Permalink
[wip] add watch shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrotko committed Mar 12, 2024
1 parent 18bcf36 commit 71b9782
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 13 deletions.
10 changes: 10 additions & 0 deletions cmd/formatter/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,21 @@ func (l *logConsumer) Err(container, message string) {
}

var navColor = makeColorFunc("90")
var keyColor = makeColorFunc("37")

func (l *logConsumer) write(w io.Writer, container, message string) {
if l.ctx.Err() != nil {
return
}
// p := l.getPresenter(container)
// timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
// for _, line := range strings.Split(message, "\n") {
// if l.timestamp {
// fmt.Fprintf(w, "\033[K%s%s%s\n", p.prefix, timestamp, line)
// } else {
// fmt.Fprintf(w, "\033[K%s%s\n", p.prefix, line)
// }
// }
KeyboardInfo.PrintKeyboardInfo(func() {
p := l.getPresenter(container)
timestamp := time.Now().Format(jsonmessage.RFC3339NanoFixed)
Expand Down
38 changes: 34 additions & 4 deletions cmd/formatter/shortcut.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package formatter

import (
"context"
"fmt"
"strings"

"github.com/buger/goterm"
"github.com/docker/compose/v2/pkg/watch"
)

type LogKeyboard struct {
err error
started bool
err error
started bool
IsDockerDesktopActive bool
Watcher watch.Notify
Watching bool
Ctx context.Context
Cancel context.CancelFunc
}

var KeyboardInfo = LogKeyboard{}
var KeyboardInfo = LogKeyboard{Watching: true}
var errorColor = "\x1b[1;33m"

func (lk *LogKeyboard) NewContext(ctx context.Context) context.CancelFunc {
ctx, cancel := context.WithCancel(ctx)
lk.Ctx = ctx
lk.Cancel = cancel
return cancel
}

func (lk *LogKeyboard) PrintKeyboardInfo(print func()) {
fmt.Print("\033[?25l") // hide cursor
defer fmt.Printf("\033[?25h") // show cursor
Expand Down Expand Up @@ -51,10 +66,25 @@ func (lk *LogKeyboard) printInfo() {
}
fmt.Printf("\033[%d;0H", height) // Move to last line
// clear line
fmt.Print("\033[K" + navColor(" >> [CTRL+G] open project in Docker Desktop [$] get more features"))
lk.infoMessage()
fmt.Print("\0338") // restore cursor position
}

func (lk *LogKeyboard) infoMessage() {
options := navColor(" Options: ")
if lk.IsDockerDesktopActive {
options = options + keyColor("^V") + navColor("iew containers in Docker Desktop")
}
if lk.Watching {
if strings.Contains(options, "Docker Desktop") {
options = options + navColor(", ")
}
options = options + navColor("Enable ") + keyColor("^W") + navColor("atch Mode")
}

fmt.Print("\033[K" + options)
}

func (lk *LogKeyboard) ClearInfo() {
height := goterm.Height()
fmt.Print("\0337") // save cursor position
Expand Down
6 changes: 5 additions & 1 deletion pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *composeService) Close() error {
if s.dockerCli != nil {
errs = append(errs, s.dockerCli.Client().Close())
}
if s.desktopCli != nil {
if s.isDesktopIntegrationActive() {
errs = append(errs, s.desktopCli.Close())
}
return errors.Join(errs...)
Expand Down Expand Up @@ -318,3 +318,7 @@ func (s *composeService) RuntimeVersion(ctx context.Context) (string, error) {
return runtimeVersion.val, runtimeVersion.err

}

func (s *composeService) isDesktopIntegrationActive() bool {
return s.desktopCli != nil
}
40 changes: 34 additions & 6 deletions pkg/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
if err != nil {
panic(err)
}
// formatter.KeyboardInfo.IsDockerDesktopActive = s.isDesktopIntegrationActive()
formatter.KeyboardInfo.IsDockerDesktopActive = true
// formatter.KeyboardInfo.Watching = s.shouldWatch(project)
formatter.KeyboardInfo.Watching = true
defer keyboard.Close()
first := true
gracefulTeardown := func() {
Expand All @@ -103,12 +107,36 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
formatter.KeyboardInfo.ClearInfo()
gracefulTeardown()
case keyboard.KeyCtrlG:
link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name)
err := open.Run(link)
if err != nil {
formatter.KeyboardInfo.SError("Could not open Docker Desktop")
} else {
formatter.KeyboardInfo.Error(nil)
if formatter.KeyboardInfo.IsDockerDesktopActive {
link := fmt.Sprintf("docker-desktop://dashboard/apps/%s", project.Name)
err := open.Run(link)
if err != nil {
formatter.KeyboardInfo.SError("Could not open Docker Desktop")
} else {
formatter.KeyboardInfo.Error(nil)
}
}
case keyboard.KeyCtrlW:
if formatter.KeyboardInfo.Watching {
formatter.KeyboardInfo.Watching = !formatter.KeyboardInfo.Watching
fmt.Println("watching shortcut", formatter.KeyboardInfo.Watching)

if formatter.KeyboardInfo.Watching {
formatter.KeyboardInfo.Cancel()
} else {
formatter.KeyboardInfo.NewContext(ctx)
quit := make(chan error)
go func() {
buildOpts := *options.Create.Build
buildOpts.Quiet = true
err := s.Watch(formatter.KeyboardInfo.Ctx, project, options.Start.Services, api.WatchOptions{
Build: &buildOpts,
LogTo: options.Start.Attach,
})
quit <- err
}()
formatter.KeyboardInfo.Error(<-quit)
}
}
case keyboard.KeyEnter:
formatter.KeyboardInfo.PrintEnter()
Expand Down
14 changes: 12 additions & 2 deletions pkg/compose/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func (s *composeService) getSyncImplementation(project *types.Project) (sync.Syn

return sync.NewTar(project.Name, tarDockerClient{s: s}), nil
}
func (s *composeService) shouldWatch(project *types.Project) bool {
var shouldWatch bool
for i := range project.Services {
service := project.Services[i]

if service.Develop != nil && service.Develop.Watch != nil {
shouldWatch = true
}
}
return shouldWatch
}

func (s *composeService) Watch(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error { //nolint: gocyclo
var err error
Expand Down Expand Up @@ -158,14 +169,13 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
return err
}
watching = true

eg.Go(func() error {
defer watcher.Close() //nolint:errcheck
return s.watch(ctx, project, service.Name, options, watcher, syncer, config.Watch)
})
}

if !watching {
// options.LogTo.Err(api.WatchLogger, "FAILED")
return fmt.Errorf("none of the selected services is configured for watch, consider setting an 'develop' section")
}
options.LogTo.Log(api.WatchLogger, "watch enabled")
Expand Down

0 comments on commit 71b9782

Please sign in to comment.