From ecd9aa3c4635e6a912e79a96db66193f8de53992 Mon Sep 17 00:00:00 2001 From: Guillaume Lours <705411+glours@users.noreply.github.com> Date: Tue, 21 May 2024 15:59:39 +0200 Subject: [PATCH] add new navigation menu to open Compose app configuration in Docker Desktop Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com> --- cmd/formatter/shortcut.go | 35 ++++++++++++++++++++++++--- internal/experimental/experimental.go | 4 +++ pkg/compose/compose.go | 7 ++++++ pkg/compose/up.go | 3 ++- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cmd/formatter/shortcut.go b/cmd/formatter/shortcut.go index fea7a1d47f0..1f8974b7d87 100644 --- a/cmd/formatter/shortcut.go +++ b/cmd/formatter/shortcut.go @@ -106,6 +106,7 @@ type LogKeyboard struct { Watch KeyboardWatch IsDockerDesktopActive bool IsWatchConfigured bool + IsDDComposeUIActive bool logLevel KEYBOARD_LOG_LEVEL signalChannel chan<- os.Signal } @@ -113,7 +114,7 @@ type LogKeyboard struct { var KeyboardManager *LogKeyboard var eg multierror.Group -func NewKeyboardManager(ctx context.Context, isDockerDesktopActive, isWatchConfigured bool, +func NewKeyboardManager(ctx context.Context, isDockerDesktopActive, isWatchConfigured, isDockerDesktopConfigActive bool, sc chan<- os.Signal, watchFn func(ctx context.Context, project *types.Project, @@ -124,6 +125,7 @@ func NewKeyboardManager(ctx context.Context, isDockerDesktopActive, isWatchConfi km := LogKeyboard{} km.IsDockerDesktopActive = isDockerDesktopActive km.IsWatchConfigured = isWatchConfigured + km.IsDDComposeUIActive = isDockerDesktopConfigActive km.logLevel = INFO km.Watch.Watching = false @@ -192,8 +194,16 @@ func (lk *LogKeyboard) navigationMenu() string { if lk.IsDockerDesktopActive { openDDInfo = shortcutKeyColor("v") + navColor(" View in Docker Desktop") } - var watchInfo string + + var openDDUI string if openDDInfo != "" { + openDDUI = navColor(" ") + } + if lk.IsDDComposeUIActive { + openDDUI = openDDUI + shortcutKeyColor("o") + navColor(" View Config") + } + var watchInfo string + if openDDInfo != "" || openDDUI != "" { watchInfo = navColor(" ") } var isEnabled = " Enable" @@ -201,7 +211,7 @@ func (lk *LogKeyboard) navigationMenu() string { isEnabled = " Disable" } watchInfo = watchInfo + shortcutKeyColor("w") + navColor(isEnabled+" Watch") - return openDDInfo + watchInfo + return openDDInfo + openDDUI + watchInfo } func (lk *LogKeyboard) clearNavigationMenu() { @@ -234,6 +244,23 @@ func (lk *LogKeyboard) openDockerDesktop(ctx context.Context, project *types.Pro ) } +func (lk *LogKeyboard) openDDComposeUI(ctx context.Context, project *types.Project) { + if !lk.IsDDComposeUIActive { + return + } + eg.Go(tracing.EventWrapFuncForErrGroup(ctx, "menu/gui/composeview", tracing.SpanOptions{}, + func(ctx context.Context) error { + link := fmt.Sprintf("docker-desktop://dashboard/docker-compose/%s", project.Name) + err := open.Run(link) + if err != nil { + err = fmt.Errorf("Could not open Docker Desktop Compose UI") + lk.keyboardError("View Config", err) + } + return err + }), + ) +} + func (lk *LogKeyboard) keyboardError(prefix string, err error) { lk.kError.addError(prefix, err) @@ -284,6 +311,8 @@ func (lk *LogKeyboard) HandleKeyEvents(event keyboard.KeyEvent, ctx context.Cont lk.openDockerDesktop(ctx, project) case 'w': lk.StartWatch(ctx, project, options) + case 'o': + lk.openDDComposeUI(ctx, project) } switch key := event.Key; key { case keyboard.KeyCtrlC: diff --git a/internal/experimental/experimental.go b/internal/experimental/experimental.go index 253c0a49ab7..26b9b77bdf6 100644 --- a/internal/experimental/experimental.go +++ b/internal/experimental/experimental.go @@ -71,6 +71,10 @@ func (s *State) NavBar() bool { return s.determineFeatureState("ComposeNav") } +func (s *State) ComposeUI() bool { + return s.determineFeatureState("ComposeUIView") +} + func (s *State) determineFeatureState(name string) bool { if s == nil || !s.active || s.desktopValues == nil { return false diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 29c5f26d617..5ed9351204e 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -324,3 +324,10 @@ func (s *composeService) RuntimeVersion(ctx context.Context) (string, error) { func (s *composeService) isDesktopIntegrationActive() bool { return s.desktopCli != nil } + +func (s *composeService) isDesktopUIEnabled() bool { + if !s.isDesktopIntegrationActive() { + return false + } + return s.experiments.ComposeUI() +} diff --git a/pkg/compose/up.go b/pkg/compose/up.go index be1fe4c04d4..0528ff35835 100644 --- a/pkg/compose/up.go +++ b/pkg/compose/up.go @@ -97,9 +97,10 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options } else { isWatchConfigured := s.shouldWatch(project) isDockerDesktopActive := s.isDesktopIntegrationActive() + isDDComposeUI := s.isDesktopUIEnabled() tracing.KeyboardMetrics(ctx, options.Start.NavigationMenu, isDockerDesktopActive, isWatchConfigured) - formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, signalChan, s.Watch) + formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, isDDComposeUI, signalChan, s.Watch) if options.Start.Watch { formatter.KeyboardManager.StartWatch(ctx, project, options) }