Cleanup entrypoint

* use the `fatal` function when we can
* rename moby.ExecRegardlessContext to Exec
This commit is contained in:
Djordje Lukic 2020-06-26 10:23:37 +02:00
parent 4fb039164d
commit 98976e4cb4
2 changed files with 16 additions and 19 deletions

View File

@ -60,6 +60,7 @@ var (
"serve": {},
"version": {},
}
unknownCommandRegexp = regexp.MustCompile(`unknown command "([^"]*)"`)
)
func init() {
@ -140,11 +141,9 @@ func main() {
ctx, cancel := newSigContext()
defer cancel()
if opts.Host != "" {
mobycli.ExecRegardlessContext(ctx)
}
if opts.Version {
mobycli.ExecRegardlessContext(ctx)
// --host and --version should immediately be forwarded to the original cli
if opts.Host != "" || opts.Version {
mobycli.Exec(ctx)
}
if opts.Config == "" {
@ -171,30 +170,26 @@ func main() {
ctx = apicontext.WithCurrentContext(ctx, currentContext)
ctx = store.WithContextStore(ctx, s)
err = root.ExecuteContext(ctx)
if err != nil {
if err = root.ExecuteContext(ctx); err != nil {
// Context should always be handled by new CLI
requiredCmd, _, _ := root.Find(os.Args[1:])
if requiredCmd != nil && isOwnCommand(requiredCmd) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
fatal(err)
}
mobycli.ExecIfDefaultCtxType(ctx)
checkIfUnknownCommandExistInDefaultContext(err, currentContext)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
fatal(err)
}
}
func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string) {
re := regexp.MustCompile(`unknown command "([^"]*)"`)
submatch := re.FindSubmatch([]byte(err.Error()))
submatch := unknownCommandRegexp.FindSubmatch([]byte(err.Error()))
if len(submatch) == 2 {
dockerCommand := string(submatch[1])
if mobycli.IsDefaultContextCommand(dockerCommand) {
fmt.Fprintf(os.Stderr, "Command \"%s\" not available in current context (%s), you can use the \"default\" context to run this command\n", dockerCommand, currentContext)
fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s), you can use the \"default\" context to run this command\n", dockerCommand, currentContext)
os.Exit(1)
}
}

View File

@ -41,7 +41,7 @@ func ExecIfDefaultCtxType(ctx context.Context) {
currentCtx, err := s.Get(currentContext)
// Only run original docker command if the current context is not ours.
if err != nil || mustDelegateToMoby(currentCtx.Type()) {
ExecRegardlessContext(ctx)
Exec(ctx)
}
}
@ -49,8 +49,8 @@ func mustDelegateToMoby(ctxType string) bool {
return ctxType == store.DefaultContextType || ctxType == store.AwsContextType
}
// ExecRegardlessContext delegates to com.docker.cli if on moby context
func ExecRegardlessContext(ctx context.Context) {
// Exec delegates to com.docker.cli if on moby context
func Exec(ctx context.Context) {
cmd := exec.CommandContext(ctx, ComDockerCli, os.Args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
@ -65,9 +65,11 @@ func ExecRegardlessContext(ctx context.Context) {
os.Exit(0)
}
// ExecCmd delegates the cli command to com.docker.cli. The error is never returned (process will exit with docker classic exit code), the return type is to make it easier to use with cobra commands
// ExecCmd delegates the cli command to com.docker.cli. The error is never
// returned (process will exit with docker classic exit code), the return type
// is to make it easier to use with cobra commands
func ExecCmd(command *cobra.Command) error {
ExecRegardlessContext(command.Context())
Exec(command.Context())
return nil
}