| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | /*   Copyright 2020 Docker, Inc.   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 mobycliimport (	"context"	"fmt"	"os"	"os/exec"	"os/signal"	"strings"	apicontext "github.com/docker/api/context"	"github.com/docker/api/context/store")var delegatedContextTypes = []string{store.DefaultContextType}// ComDockerCli name of the classic cli binaryconst ComDockerCli = "com.docker.cli"// ExecIfDefaultCtxType delegates to com.docker.cli if on moby or AWS context (until there is an AWS backend)func ExecIfDefaultCtxType(ctx context.Context) {	currentContext := apicontext.CurrentContext(ctx)	s := store.ContextStore(ctx)	currentCtx, err := s.Get(currentContext)	// Only run original docker command if the current context is not ours.	if err != nil || mustDelegateToMoby(currentCtx.Type()) {		Exec()	}}func mustDelegateToMoby(ctxType string) bool {	for _, ctype := range delegatedContextTypes {		if ctxType == ctype {			return true		}	}	return false}// Exec delegates to com.docker.cli if on moby contextfunc Exec() {	cmd := exec.Command(ComDockerCli, os.Args[1:]...)	cmd.Stdin = os.Stdin	cmd.Stdout = os.Stdout	cmd.Stderr = os.Stderr	signals := make(chan os.Signal)	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				}				err := cmd.Process.Signal(sig)				if err != nil {					fmt.Printf("WARNING could not forward signal %s to %s : %s\n", sig.String(), ComDockerCli, err.Error())				}			case <-childExit:				return			}		}	}()	err := cmd.Run()	childExit <- true	if err != nil {		if exiterr, ok := err.(*exec.ExitError); ok {			os.Exit(exiterr.ExitCode())		}		fmt.Fprintln(os.Stderr, err)		os.Exit(1)	}	os.Exit(0)}// IsDefaultContextCommand checks if the command exists in the classic cli (issues a shellout --help)func IsDefaultContextCommand(dockerCommand string) bool {	cmd := exec.Command(ComDockerCli, dockerCommand, "--help")	b, e := cmd.CombinedOutput()	if e != nil {		fmt.Println(e)	}	output := string(b)	contains := strings.Contains(output, "Usage:\tdocker "+dockerCommand)	return contains}// ExecSilent executes a command and do redirect output to stdOut, return outputfunc ExecSilent(ctx context.Context) ([]byte, error) {	cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)	return cmd.CombinedOutput()}
 |