Skip to content

Commit 3436537

Browse files
committed
Does not start keyboard manager if there is no tty
Signed-off-by: Joana Hrotko <[email protected]>
1 parent 339b331 commit 3436537

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

cmd/compose/up.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ func (opts upOptions) apply(project *types.Project, services []string) (*types.P
7979
return project, nil
8080
}
8181

82+
func (opts *upOptions) validateNavigationMenu(dockerCli command.Cli, experimentals *experimental.State) {
83+
if !opts.navigationMenuChanged {
84+
opts.navigationMenu = SetUnchangedOption(ComposeMenu, experimentals.NavBar())
85+
}
86+
87+
if opts.navigationMenu && !dockerCli.Out().IsTerminal() {
88+
opts.navigationMenu = false
89+
}
90+
}
91+
8292
func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service, experiments *experimental.State) *cobra.Command {
8393
up := upOptions{}
8494
create := createOptions{}
@@ -100,7 +110,10 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service, ex
100110
if len(up.attach) != 0 && up.attachDependencies {
101111
return errors.New("cannot combine --attach and --attach-dependencies")
102112
}
103-
return runUp(ctx, dockerCli, backend, experiments, create, up, build, project, services)
113+
114+
up.validateNavigationMenu(dockerCli, experiments)
115+
116+
return runUp(ctx, dockerCli, backend, create, up, build, project, services)
104117
}),
105118
ValidArgsFunction: completeServiceNames(dockerCli, p),
106119
}
@@ -170,7 +183,6 @@ func runUp(
170183
ctx context.Context,
171184
dockerCli command.Cli,
172185
backend api.Service,
173-
experimentals *experimental.State,
174186
createOptions createOptions,
175187
upOptions upOptions,
176188
buildOptions buildOptions,
@@ -190,9 +202,6 @@ func runUp(
190202
if err != nil {
191203
return err
192204
}
193-
if !upOptions.navigationMenuChanged {
194-
upOptions.navigationMenu = SetUnchangedOption(ComposeMenu, experimentals.NavBar())
195-
}
196205

197206
var build *api.BuildOptions
198207
if !createOptions.noBuild {

cmd/formatter/shortcut.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,6 @@ func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, o
275275
}
276276
}
277277

278-
func (lk *LogKeyboard) KeyboardClose() {
279-
_ = keyboard.Close()
280-
}
281-
282278
func (lk *LogKeyboard) HandleKeyEvents(event keyboard.KeyEvent, ctx context.Context, project *types.Project, options api.UpOptions) {
283279
switch kRune := event.Rune; kRune {
284280
case 'v':
@@ -288,8 +284,7 @@ func (lk *LogKeyboard) HandleKeyEvents(event keyboard.KeyEvent, ctx context.Cont
288284
}
289285
switch key := event.Key; key {
290286
case keyboard.KeyCtrlC:
291-
lk.KeyboardClose()
292-
287+
_ = keyboard.Close()
293288
lk.clearNavigationMenu()
294289
ShowCursor()
295290

pkg/compose/up.go

+29-16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/docker/compose/v2/pkg/progress"
3232
"github.com/eiannone/keyboard"
3333
"github.com/hashicorp/go-multierror"
34+
"github.com/sirupsen/logrus"
3435
)
3536

3637
func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo
@@ -89,22 +90,7 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
8990
first = false
9091
}
9192

92-
var kEvents <-chan keyboard.KeyEvent
93-
isWatchConfigured := s.shouldWatch(project)
94-
isDockerDesktopActive := s.isDesktopIntegrationActive()
95-
96-
tracing.KeyboardMetrics(ctx, options.Start.NavigationMenu, isDockerDesktopActive, isWatchConfigured)
97-
if options.Start.NavigationMenu {
98-
kEvents, err = keyboard.GetKeys(100)
99-
if err != nil {
100-
panic(err)
101-
}
102-
formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, signalChan, s.Watch)
103-
if options.Start.Watch {
104-
formatter.KeyboardManager.StartWatch(ctx, project, options)
105-
}
106-
defer formatter.KeyboardManager.KeyboardClose()
107-
}
93+
kEvents := s.startMenu(ctx, project, options, signalChan)
10894
for {
10995
select {
11096
case <-doneCh:
@@ -179,3 +165,30 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
179165
}
180166
return err
181167
}
168+
169+
func (s *composeService) startMenu(
170+
ctx context.Context,
171+
project *types.Project,
172+
options api.UpOptions,
173+
sc chan<- os.Signal,
174+
) <-chan keyboard.KeyEvent {
175+
if !options.Start.NavigationMenu {
176+
return nil
177+
}
178+
kEvents, err := keyboard.GetKeys(100)
179+
keyboardInitiated := err == nil
180+
if !keyboardInitiated {
181+
logrus.Warn("Could not start Menu - an error occurred while starting.")
182+
return nil
183+
}
184+
185+
isWatchConfigured := s.shouldWatch(project)
186+
isDockerDesktopActive := s.isDesktopIntegrationActive()
187+
tracing.KeyboardMetrics(ctx, options.Start.NavigationMenu, isDockerDesktopActive, isWatchConfigured)
188+
189+
formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, sc, s.Watch)
190+
if options.Start.Watch {
191+
formatter.KeyboardManager.StartWatch(ctx, project, options)
192+
}
193+
return kEvents
194+
}

0 commit comments

Comments
 (0)