Merge pull request #1879 from ndeloof/signals

signals
This commit is contained in:
Mathieu Champlon 2021-09-21 12:09:52 +02:00 коммит произвёл GitHub
Родитель ba40ef0bd1 9215d68428
Коммит 8b76602054
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
12 изменённых файлов: 19 добавлений и 97 удалений

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

@ -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(cmd.Root())
mobycli.Exec()
},
}
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(cmd.Root())
mobycli.Exec()
},
}
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(cmd.Root())
mobycli.Exec()
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(cmd.Root())
mobycli.Exec()
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(cmd, opts)
return runList(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(cmd *cobra.Command, opts lsOpts) error {
func runList(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(cmd.Root())
mobycli.Exec()
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(cmd, args[0])
return runUpdate(args[0])
},
Long: longHelp,
}
@ -71,7 +71,7 @@ $ docker context update my-context --description "some description" --docker "ho
return cmd
}
func runUpdate(cmd *cobra.Command, name string) error {
func runUpdate(name string) error {
s := store.Instance()
dockerContext, err := s.Get(name)
if err == nil && dockerContext != nil {
@ -80,6 +80,6 @@ func runUpdate(cmd *cobra.Command, name string) error {
}
}
mobycli.Exec(cmd.Root())
mobycli.Exec()
return nil
}

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

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

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

@ -37,6 +37,6 @@ func Command() *cobra.Command {
}
func runLogout(cmd *cobra.Command, args []string) error {
mobycli.Exec(cmd.Root())
mobycli.Exec()
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(cmd.Root())
mobycli.Exec()
return "", nil
}
return string(versionResult), err

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

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

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

@ -21,7 +21,6 @@ import (
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"regexp"
@ -49,7 +48,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(root)
Exec()
}
}
@ -63,10 +62,8 @@ func mustDelegateToMoby(ctxType string) bool {
}
// Exec delegates to com.docker.cli if on moby context
func Exec(root *cobra.Command) {
childExit := make(chan bool)
err := RunDocker(childExit, os.Args[1:]...)
childExit <- true
func Exec() {
err := RunDocker(os.Args[1:]...)
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
exitCode := exiterr.ExitCode()
@ -91,7 +88,7 @@ func Exec(root *cobra.Command) {
}
// 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 {
func RunDocker(args ...string) error {
execBinary, err := resolvepath.LookPath(ComDockerCli)
if err != nil {
execBinary = findBinary(ComDockerCli)
@ -105,29 +102,6 @@ func RunDocker(childExit chan bool, 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()
}

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

@ -1,29 +0,0 @@
// +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
}

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

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