Skip to content

Commit

Permalink
Does not start keyboard manager if there is no tty
Browse files Browse the repository at this point in the history
Signed-off-by: Joana Hrotko <joana.hrotko@docker.com>
  • Loading branch information
jhrotko committed Mar 28, 2024
1 parent 339b331 commit 3436537
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
19 changes: 14 additions & 5 deletions cmd/compose/up.go
Expand Up @@ -79,6 +79,16 @@ func (opts upOptions) apply(project *types.Project, services []string) (*types.P
return project, nil
}

func (opts *upOptions) validateNavigationMenu(dockerCli command.Cli, experimentals *experimental.State) {
if !opts.navigationMenuChanged {
opts.navigationMenu = SetUnchangedOption(ComposeMenu, experimentals.NavBar())
}

if opts.navigationMenu && !dockerCli.Out().IsTerminal() {
opts.navigationMenu = false
}

Check warning on line 89 in cmd/compose/up.go

View check run for this annotation

Codecov / codecov/patch

cmd/compose/up.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}

func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service, experiments *experimental.State) *cobra.Command {
up := upOptions{}
create := createOptions{}
Expand All @@ -100,7 +110,10 @@ func upCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service, ex
if len(up.attach) != 0 && up.attachDependencies {
return errors.New("cannot combine --attach and --attach-dependencies")
}
return runUp(ctx, dockerCli, backend, experiments, create, up, build, project, services)

up.validateNavigationMenu(dockerCli, experiments)

return runUp(ctx, dockerCli, backend, create, up, build, project, services)
}),
ValidArgsFunction: completeServiceNames(dockerCli, p),
}
Expand Down Expand Up @@ -170,7 +183,6 @@ func runUp(
ctx context.Context,
dockerCli command.Cli,
backend api.Service,
experimentals *experimental.State,
createOptions createOptions,
upOptions upOptions,
buildOptions buildOptions,
Expand All @@ -190,9 +202,6 @@ func runUp(
if err != nil {
return err
}
if !upOptions.navigationMenuChanged {
upOptions.navigationMenu = SetUnchangedOption(ComposeMenu, experimentals.NavBar())
}

var build *api.BuildOptions
if !createOptions.noBuild {
Expand Down
7 changes: 1 addition & 6 deletions cmd/formatter/shortcut.go
Expand Up @@ -275,10 +275,6 @@ func (lk *LogKeyboard) StartWatch(ctx context.Context, project *types.Project, o
}
}

func (lk *LogKeyboard) KeyboardClose() {
_ = keyboard.Close()
}

func (lk *LogKeyboard) HandleKeyEvents(event keyboard.KeyEvent, ctx context.Context, project *types.Project, options api.UpOptions) {
switch kRune := event.Rune; kRune {
case 'v':
Expand All @@ -288,8 +284,7 @@ func (lk *LogKeyboard) HandleKeyEvents(event keyboard.KeyEvent, ctx context.Cont
}
switch key := event.Key; key {
case keyboard.KeyCtrlC:
lk.KeyboardClose()

_ = keyboard.Close()

Check warning on line 287 in cmd/formatter/shortcut.go

View check run for this annotation

Codecov / codecov/patch

cmd/formatter/shortcut.go#L287

Added line #L287 was not covered by tests
lk.clearNavigationMenu()
ShowCursor()

Expand Down
45 changes: 29 additions & 16 deletions pkg/compose/up.go
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/docker/compose/v2/pkg/progress"
"github.com/eiannone/keyboard"
"github.com/hashicorp/go-multierror"
"github.com/sirupsen/logrus"
)

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

var kEvents <-chan keyboard.KeyEvent
isWatchConfigured := s.shouldWatch(project)
isDockerDesktopActive := s.isDesktopIntegrationActive()

tracing.KeyboardMetrics(ctx, options.Start.NavigationMenu, isDockerDesktopActive, isWatchConfigured)
if options.Start.NavigationMenu {
kEvents, err = keyboard.GetKeys(100)
if err != nil {
panic(err)
}
formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, signalChan, s.Watch)
if options.Start.Watch {
formatter.KeyboardManager.StartWatch(ctx, project, options)
}
defer formatter.KeyboardManager.KeyboardClose()
}
kEvents := s.startMenu(ctx, project, options, signalChan)
for {
select {
case <-doneCh:
Expand Down Expand Up @@ -179,3 +165,30 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
}
return err
}

func (s *composeService) startMenu(
ctx context.Context,
project *types.Project,
options api.UpOptions,
sc chan<- os.Signal,
) <-chan keyboard.KeyEvent {
if !options.Start.NavigationMenu {
return nil
}
kEvents, err := keyboard.GetKeys(100)
keyboardInitiated := err == nil
if !keyboardInitiated {
logrus.Warn("Could not start Menu - an error occurred while starting.")
return nil
}

Check warning on line 183 in pkg/compose/up.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/up.go#L178-L183

Added lines #L178 - L183 were not covered by tests

isWatchConfigured := s.shouldWatch(project)
isDockerDesktopActive := s.isDesktopIntegrationActive()
tracing.KeyboardMetrics(ctx, options.Start.NavigationMenu, isDockerDesktopActive, isWatchConfigured)

formatter.NewKeyboardManager(ctx, isDockerDesktopActive, isWatchConfigured, sc, s.Watch)
if options.Start.Watch {
formatter.KeyboardManager.StartWatch(ctx, project, options)
}
return kEvents

Check warning on line 193 in pkg/compose/up.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/up.go#L185-L193

Added lines #L185 - L193 were not covered by tests
}

0 comments on commit 3436537

Please sign in to comment.