-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use NewAttachCommand from 'docker container attach' to impl 'docker c…
…ompose attach'
- Loading branch information
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/docker/cli/cli/command" | ||
"github.com/docker/cli/cli/command/container" | ||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func attachCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command { | ||
// uses NewAttachCommand from `docker container attach`: | ||
// - lookup container by service name (and optionally index) | ||
// - invoke underlying `container attach` with container ID | ||
|
||
// FYI changes to the underlying NewAttachCommand could require changes here, i.e. help messages or options added that don't make sense with `compose attach` | ||
// - That said, the command flags/behavior are pretty stable | ||
|
||
cmd := container.NewAttachCommand(dockerCli) | ||
containerAttachRunE := cmd.RunE | ||
|
||
// Options: | ||
// inherits `container attach` options: --detach-keys, --no-stdin, --sig-proxy, --dry-run | ||
// additional options specific to compose (in this case to lookup a container): | ||
opts := api.LookupContainerOptions{} | ||
cmd.Flags().IntVar(&opts.Index, "index", 1, "index of the container if service has multiple replicas") | ||
|
||
// Help messages: (override to name first arg SERVICE instead of CONTAINER): | ||
cmd.Use = "attach [OPTIONS] SERVICE" | ||
cmd.Short = "Attach local standard input, output, and error streams to a service's running container" | ||
|
||
// clear alias for `docker attach` | ||
cmd.Annotations = map[string]string{} | ||
|
||
// todo is this for completions or validation? or both? | ||
cmd.ValidArgsFunction = completeServiceNames(dockerCli, p) | ||
|
||
// Args: why require a service name? | ||
// - It's possible to make `compose attach` work without specifying a service (i.e. just use first container). | ||
// - But, I don't see that convention used in other similar compose commands, i.e. `compose exec` which targets a single container too and requires the service name. | ||
// - Also, the underlying `container attach` command requires 1 arg so it would have to be patched to ignore no args | ||
// - And, other compose commands optionally take a service name, i.e. `compose logs`, but the behavior there is to view logs of all containers not just one. | ||
|
||
cmd.RunE = Adapt(func(ctx context.Context, args []string) error { | ||
projectName, err := p.toProjectName(dockerCli) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(args) > 0 { | ||
opts.Service = args[0] | ||
} | ||
|
||
containerID, err := backend.LookupContainer(ctx, projectName, opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO? --dry-run? would it be helpful to print the matching container name/ID and not attach? | ||
|
||
return containerAttachRunE(cmd, []string{containerID}) | ||
}) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/docker/compose/v2/pkg/api" | ||
) | ||
|
||
func (s *composeService) LookupContainer(ctx context.Context, projectName string, options api.LookupContainerOptions) (string, error) { | ||
projectName = strings.ToLower(projectName) | ||
container, err := s.getSpecifiedContainer(ctx, projectName, oneOffExclude, false, options.Service, options.Index) | ||
if err != nil { | ||
return "", err | ||
} | ||
return container.ID, nil | ||
} |