github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/e2e/cli-plugins/plugins/presocket/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "os/signal" 7 "syscall" 8 "time" 9 10 "github.com/khulnasoft/cli/cli-plugins/manager" 11 "github.com/khulnasoft/cli/cli-plugins/plugin" 12 "github.com/khulnasoft/cli/cli/command" 13 "github.com/spf13/cobra" 14 ) 15 16 func main() { 17 plugin.Run(RootCmd, manager.Metadata{ 18 SchemaVersion: "0.1.0", 19 Vendor: "Docker Inc.", 20 Version: "test", 21 }) 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(), "context cancelled") 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(), "context cancelled") 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 }) 82 83 cmd.AddCommand(&cobra.Command{ 84 Use: "test-socket-ignore-context", 85 Short: "test command that runs until it receives a SIGINT", 86 PreRunE: func(cmd *cobra.Command, args []string) error { 87 return plugin.PersistentPreRunE(cmd, args) 88 }, 89 RunE: func(cmd *cobra.Command, args []string) error { 90 signalCh := make(chan os.Signal, 10) 91 signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM) 92 go func() { 93 for range signalCh { 94 fmt.Fprintln(dockerCli.Out(), "received SIGINT") 95 } 96 }() 97 <-time.After(3 * time.Second) 98 fmt.Fprintln(dockerCli.Err(), "exit after 3 seconds") 99 return nil 100 }, 101 }) 102 103 cmd.AddCommand(&cobra.Command{ 104 Use: "tty", 105 Short: "test command that attempts to read from the TTY", 106 RunE: func(cmd *cobra.Command, args []string) error { 107 done := make(chan struct{}) 108 go func() { 109 b := make([]byte, 1) 110 _, _ = dockerCli.In().Read(b) 111 done <- struct{}{} 112 }() 113 select { 114 case <-done: 115 case <-time.After(2 * time.Second): 116 fmt.Fprint(dockerCli.Err(), "timeout after 2 seconds") 117 } 118 return nil 119 }, 120 }) 121 122 return &cmd 123 }