| 22 | } |
| 23 | |
| 24 | func RootCmd(dockerCli command.Cli) *cobra.Command { |
| 25 | cmd := cobra.Command{ |
| 26 | Use: "presocket", |
| 27 | Short: "testing plugin that does not connect to the socket", |
| 28 | // override PersistentPreRunE so that the plugin default |
| 29 | // PersistentPreRunE doesn't run, simulating a plugin built |
| 30 | // with a pre-socket-communication version of the CLI |
| 31 | PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 32 | return nil |
| 33 | }, |
| 34 | } |
| 35 | |
| 36 | cmd.AddCommand(&cobra.Command{ |
| 37 | Use: "test-no-socket", |
| 38 | Short: "test command that runs until it receives a SIGINT", |
| 39 | RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | go func() { |
| 41 | <-cmd.Context().Done() |
| 42 | _, _ = fmt.Fprintln(dockerCli.Out(), "test-no-socket: exiting after context was done") |
| 43 | os.Exit(2) |
| 44 | }() |
| 45 | signalCh := make(chan os.Signal, 10) |
| 46 | signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM) |
| 47 | go func() { |
| 48 | for range signalCh { |
| 49 | _, _ = fmt.Fprintln(dockerCli.Out(), "received SIGINT") |
| 50 | } |
| 51 | }() |
| 52 | <-time.After(3 * time.Second) |
| 53 | _, _ = fmt.Fprintln(dockerCli.Err(), "exit after 3 seconds") |
| 54 | return nil |
| 55 | }, |
| 56 | }) |
| 57 | |
| 58 | cmd.AddCommand(&cobra.Command{ |
| 59 | Use: "test-socket", |
| 60 | Short: "test command that runs until it receives a SIGINT", |
| 61 | PreRunE: func(cmd *cobra.Command, args []string) error { |
| 62 | return plugin.PersistentPreRunE(cmd, args) |
| 63 | }, |
| 64 | RunE: func(cmd *cobra.Command, args []string) error { |
| 65 | go func() { |
| 66 | <-cmd.Context().Done() |
| 67 | _, _ = fmt.Fprintln(dockerCli.Out(), "test-socket: exiting after context was done") |
| 68 | os.Exit(2) |
| 69 | }() |
| 70 | signalCh := make(chan os.Signal, 10) |
| 71 | signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM) |
| 72 | go func() { |
| 73 | for range signalCh { |
| 74 | _, _ = fmt.Fprintln(dockerCli.Out(), "received SIGINT") |
| 75 | } |
| 76 | }() |
| 77 | <-time.After(3 * time.Second) |
| 78 | _, _ = fmt.Fprintln(dockerCli.Err(), "exit after 3 seconds") |
| 79 | return nil |
| 80 | }, |
| 81 | }) |