mirror of
https://github.com/docker/compose.git
synced 2025-07-22 13:14:29 +02:00
Adapt cli/mobycli to avoid duplicating shellout code
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
parent
e92b2f80d2
commit
2b1158f4c2
@ -62,35 +62,8 @@ func mustDelegateToMoby(ctxType string) bool {
|
|||||||
|
|
||||||
// Exec delegates to com.docker.cli if on moby context
|
// Exec delegates to com.docker.cli if on moby context
|
||||||
func Exec(root *cobra.Command) {
|
func Exec(root *cobra.Command) {
|
||||||
execBinary, err := resolvepath.LookPath(ComDockerCli)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
cmd := exec.Command(execBinary, os.Args[1:]...)
|
|
||||||
cmd.Stdin = os.Stdin
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
signals := make(chan os.Signal, 1)
|
|
||||||
childExit := make(chan bool)
|
childExit := make(chan bool)
|
||||||
signal.Notify(signals) // catch all signals
|
err := RunDocker(childExit, os.Args[1:]...)
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case sig := <-signals:
|
|
||||||
if cmd.Process == nil {
|
|
||||||
continue // can happen if receiving signal before the process is actually started
|
|
||||||
}
|
|
||||||
// nolint errcheck
|
|
||||||
cmd.Process.Signal(sig)
|
|
||||||
case <-childExit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
err = cmd.Run()
|
|
||||||
childExit <- true
|
childExit <- true
|
||||||
if err != nil {
|
if err != nil {
|
||||||
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
|
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
|
||||||
@ -110,6 +83,38 @@ func Exec(root *cobra.Command) {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RunDocker runs a docker command, and forward signals to the shellout command (stops listening to signals when an event is sent to childExit)
|
||||||
|
func RunDocker(childExit chan bool, args ...string) error {
|
||||||
|
execBinary, err := resolvepath.LookPath(ComDockerCli)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
cmd := exec.Command(execBinary, args...)
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
signals := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(signals) // catch all signals
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case sig := <-signals:
|
||||||
|
if cmd.Process == nil {
|
||||||
|
continue // can happen if receiving signal before the process is actually started
|
||||||
|
}
|
||||||
|
// nolint errcheck
|
||||||
|
cmd.Process.Signal(sig)
|
||||||
|
case <-childExit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
// IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
|
// IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)
|
||||||
func IsDefaultContextCommand(dockerCommand string) bool {
|
func IsDefaultContextCommand(dockerCommand string) bool {
|
||||||
cmd := exec.Command(ComDockerCli, dockerCommand, "--help")
|
cmd := exec.Command(ComDockerCli, dockerCommand, "--help")
|
||||||
|
@ -22,7 +22,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
"github.com/docker/compose-cli/local/moby"
|
"github.com/docker/compose-cli/cli/mobycli"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
)
|
)
|
||||||
@ -60,7 +60,10 @@ func (s *composeService) windowsBuild(project *types.Project, options compose.Bu
|
|||||||
|
|
||||||
args := cmd.getArguments()
|
args := cmd.getArguments()
|
||||||
// shell out to moby cli
|
// shell out to moby cli
|
||||||
err := moby.Exec(args)
|
childExit := make(chan bool)
|
||||||
|
err := mobycli.RunDocker(childExit, args...)
|
||||||
|
childExit <- true
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,62 +0,0 @@
|
|||||||
/*
|
|
||||||
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 moby
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"os/signal"
|
|
||||||
|
|
||||||
"github.com/docker/compose-cli/cli/mobycli/resolvepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ComDockerCli name of the classic cli binary
|
|
||||||
const ComDockerCli = "com.docker.cli"
|
|
||||||
|
|
||||||
// Exec delegates to com.docker.cli
|
|
||||||
func Exec(args []string) error {
|
|
||||||
// look up the path of the classic cli binary
|
|
||||||
execBinary, err := resolvepath.LookPath(ComDockerCli)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cmd := exec.Command(execBinary, args...)
|
|
||||||
cmd.Stdin = os.Stdin
|
|
||||||
cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
signals := make(chan os.Signal, 1)
|
|
||||||
childExit := make(chan bool)
|
|
||||||
signal.Notify(signals) // catch all signals
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case sig := <-signals:
|
|
||||||
if cmd.Process == nil {
|
|
||||||
continue // can happen if receiving signal before the process is actually started
|
|
||||||
}
|
|
||||||
// nolint errcheck
|
|
||||||
cmd.Process.Signal(sig)
|
|
||||||
case <-childExit:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
err = cmd.Run()
|
|
||||||
childExit <- true
|
|
||||||
return err
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user