Signed-off-by: Mathieu Champlon <mathieu.champlon@docker.com>
This commit is contained in:
Mathieu Champlon 2021-11-04 17:10:11 +01:00
Родитель 461f022b81
Коммит 48ad1486ac
12 изменённых файлов: 97 добавлений и 19 удалений

Просмотреть файл

@ -48,7 +48,7 @@ func exportCommand() *cobra.Command {
Use: "export",
Short: "Export a context to a tar or kubeconfig file",
Run: func(cmd *cobra.Command, args []string) {
mobycli.Exec()
mobycli.Exec(cmd.Root())
},
}
cmd.Flags().Bool("kubeconfig", false, "Export as a kubeconfig file")
@ -60,7 +60,7 @@ func importCommand() *cobra.Command {
Use: "import",
Short: "Import a context from a tar or zip file",
Run: func(cmd *cobra.Command, args []string) {
mobycli.Exec()
mobycli.Exec(cmd.Root())
},
}
return cmd

Просмотреть файл

@ -69,7 +69,7 @@ $ docker context create my-context --description "some description" --docker "ho
Use: "create CONTEXT",
Short: "Create new context",
RunE: func(cmd *cobra.Command, args []string) error {
mobycli.Exec()
mobycli.Exec(cmd.Root())
return nil
},
Long: longHelp,

Просмотреть файл

@ -27,7 +27,7 @@ func inspectCommand() *cobra.Command {
Use: "inspect",
Short: "Display detailed information on one or more contexts",
RunE: func(cmd *cobra.Command, args []string) error {
mobycli.Exec()
mobycli.Exec(cmd.Root())
return nil
},
}

Просмотреть файл

@ -53,7 +53,7 @@ func listCommand() *cobra.Command {
Aliases: []string{"ls"},
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runList(opts)
return runList(cmd, opts)
},
}
cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only show context names")
@ -62,14 +62,14 @@ func listCommand() *cobra.Command {
return cmd
}
func runList(opts lsOpts) error {
func runList(cmd *cobra.Command, opts lsOpts) error {
err := opts.validate()
if err != nil {
return err
}
format := strings.ToLower(strings.ReplaceAll(opts.format, " ", ""))
if format != "" && format != formatter.JSON && format != formatter.PRETTY && format != formatter.TemplateLegacyJSON {
mobycli.Exec()
mobycli.Exec(cmd.Root())
return nil
}

Просмотреть файл

@ -56,7 +56,7 @@ $ docker context update my-context --description "some description" --docker "ho
Short: "Update a context",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runUpdate(args[0])
return runUpdate(cmd, args[0])
},
Long: longHelp,
}
@ -71,7 +71,7 @@ $ docker context update my-context --description "some description" --docker "ho
return cmd
}
func runUpdate(name string) error {
func runUpdate(cmd *cobra.Command, name string) error {
s := store.Instance()
dockerContext, err := s.Get(name)
if err == nil && dockerContext != nil {
@ -80,6 +80,6 @@ func runUpdate(name string) error {
}
}
mobycli.Exec()
mobycli.Exec(cmd.Root())
return nil
}

Просмотреть файл

@ -49,7 +49,7 @@ func Command() *cobra.Command {
}
func runLogin(cmd *cobra.Command, args []string) error {
mobycli.Exec()
mobycli.Exec(cmd.Root())
return nil
}

Просмотреть файл

@ -37,6 +37,6 @@ func Command() *cobra.Command {
}
func runLogout(cmd *cobra.Command, args []string) error {
mobycli.Exec()
mobycli.Exec(cmd.Root())
return nil
}

Просмотреть файл

@ -80,7 +80,7 @@ func getOutFromMoby(cmd *cobra.Command, args ...string) (string, error) {
// we don't want to fail on error, there is an error if the engine is not available but it displays client version info
// Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
if versionResult == nil {
mobycli.Exec()
mobycli.Exec(cmd.Root())
return "", nil
}
return string(versionResult), err

Просмотреть файл

@ -195,7 +195,7 @@ func main() {
// --version should immediately be forwarded to the original cli
if opts.Version {
mobycli.Exec()
mobycli.Exec(root)
}
if opts.Config == "" {
@ -209,7 +209,7 @@ func main() {
s, err := store.New(configDir)
if err != nil {
mobycli.Exec()
mobycli.Exec(root)
}
store.WithContextStore(s)

Просмотреть файл

@ -21,6 +21,7 @@ import (
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
@ -48,7 +49,7 @@ func ExecIfDefaultCtxType(ctx context.Context, root *cobra.Command) {
currentCtx, err := s.Get(currentContext)
// Only run original docker command if the current context is not ours.
if err != nil || mustDelegateToMoby(currentCtx.Type()) {
Exec()
Exec(root)
}
}
@ -62,8 +63,10 @@ func mustDelegateToMoby(ctxType string) bool {
}
// Exec delegates to com.docker.cli if on moby context
func Exec() {
err := RunDocker(os.Args[1:]...)
func Exec(root *cobra.Command) {
childExit := make(chan bool)
err := RunDocker(childExit, os.Args[1:]...)
childExit <- true
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
exitCode := exiterr.ExitCode()
@ -88,7 +91,7 @@ func Exec() {
}
// 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(args ...string) error {
func RunDocker(childExit chan bool, args ...string) error {
execBinary, err := resolvepath.LookPath(ComDockerCli)
if err != nil {
execBinary = findBinary(ComDockerCli)
@ -102,6 +105,29 @@ func RunDocker(args ...string) error {
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
}
// In go1.14+, the go runtime issues SIGURG as an interrupt to
// support preemptable system calls on Linux. Since we can't
// forward that along we'll check that here.
if isRuntimeSig(sig) {
continue
}
_ = cmd.Process.Signal(sig)
case <-childExit:
return
}
}
}()
return cmd.Run()
}

29
cli/mobycli/exec_unix.go Normal file
Просмотреть файл

@ -0,0 +1,29 @@
// +build !windows
/*
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 mobycli
import (
"os"
"golang.org/x/sys/unix"
)
func isRuntimeSig(s os.Signal) bool {
return s == unix.SIGURG
}

Просмотреть файл

@ -0,0 +1,23 @@
/*
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 mobycli
import "os"
func isRuntimeSig(s os.Signal) bool {
return false
}