AdaptCmd adapt a CobraCommand func to cobra library
(fn CobraCommand)
| 105 | |
| 106 | // AdaptCmd adapt a CobraCommand func to cobra library |
| 107 | func AdaptCmd(fn CobraCommand) func(cmd *cobra.Command, args []string) error { |
| 108 | return func(cmd *cobra.Command, args []string) error { |
| 109 | ctx, cancel := context.WithCancel(cmd.Context()) |
| 110 | |
| 111 | s := make(chan os.Signal, 1) |
| 112 | signal.Notify(s, syscall.SIGTERM, syscall.SIGINT) |
| 113 | go func() { |
| 114 | <-s |
| 115 | cancel() |
| 116 | signal.Stop(s) |
| 117 | close(s) |
| 118 | }() |
| 119 | |
| 120 | err := fn(ctx, cmd, args) |
| 121 | if api.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) { |
| 122 | err = dockercli.StatusError{ |
| 123 | StatusCode: 130, |
| 124 | } |
| 125 | } |
| 126 | if display.Mode == display.ModeJSON { |
| 127 | err = makeJSONError(err) |
| 128 | } |
| 129 | return err |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Adapt a Command func to cobra library |
| 134 | func Adapt(fn Command) func(cmd *cobra.Command, args []string) error { |
no test coverage detected