@@ -3,26 +3,19 @@ package plugin
33import (
44 "context"
55 "encoding/json"
6- "errors"
76 "fmt"
8- "io"
9- "net"
107 "os"
118 "sync"
129
1310 "github.com/docker/cli/cli"
1411 "github.com/docker/cli/cli-plugins/manager"
12+ "github.com/docker/cli/cli-plugins/socket"
1513 "github.com/docker/cli/cli/command"
1614 "github.com/docker/cli/cli/connhelper"
1715 "github.com/docker/docker/client"
1816 "github.com/spf13/cobra"
1917)
2018
21- // CLIPluginSocketEnvKey is used to pass the plugin being
22- // executed the abstract socket name it should listen on to know
23- // when the CLI has exited.
24- const CLIPluginSocketEnvKey = "DOCKER_CLI_PLUGIN_SOCKET"
25-
2619// PersistentPreRunE must be called by any plugin command (or
2720// subcommand) which uses the cobra `PersistentPreRun*` hook. Plugins
2821// which do not make use of `PersistentPreRun*` do not need to call
@@ -33,38 +26,6 @@ const CLIPluginSocketEnvKey = "DOCKER_CLI_PLUGIN_SOCKET"
3326// called.
3427var PersistentPreRunE func (* cobra.Command , []string ) error
3528
36- // closeOnCLISocketClose connects to the socket specified
37- // by the DOCKER_CLI_PLUGIN_SOCKET env var, if present, and attempts
38- // to read from it until it receives an EOF, which signals that
39- // the CLI is going to exit and the plugin should also exit.
40- func closeOnCLISocketClose (cancel func ()) {
41- socketAddr , ok := os .LookupEnv (CLIPluginSocketEnvKey )
42- if ! ok {
43- // if a plugin compiled against a more recent version of docker/cli
44- // is executed by an older CLI binary, ignore missing environment
45- // variable and behave as usual
46- return
47- }
48- addr , err := net .ResolveUnixAddr ("unix" , socketAddr )
49- if err != nil {
50- return
51- }
52- cliCloseConn , err := net .DialUnix ("unix" , nil , addr )
53- if err != nil {
54- return
55- }
56-
57- go func () {
58- b := make ([]byte , 1 )
59- for {
60- _ , err := cliCloseConn .Read (b )
61- if errors .Is (err , io .EOF ) {
62- cancel ()
63- }
64- }
65- }()
66- }
67-
6829// RunPlugin executes the specified plugin command
6930func RunPlugin (dockerCli * command.DockerCli , plugin * cobra.Command , meta manager.Metadata ) error {
7031 tcmd := newPluginCommand (dockerCli , plugin , meta )
@@ -81,7 +42,8 @@ func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager
8142 }
8243 ctx , cancel := context .WithCancel (cmdContext )
8344 cmd .SetContext (ctx )
84- closeOnCLISocketClose (cancel )
45+ // Set up the context to cancel based on signalling via CLI socket.
46+ socket .ConnectAndWait (cancel )
8547
8648 var opts []command.CLIOption
8749 if os .Getenv ("DOCKER_CLI_PLUGIN_USE_DIAL_STDIO" ) != "" {
0 commit comments