|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + |
| 24 | + "github.com/docker/compose-cli/api/client" |
| 25 | + "github.com/docker/compose-cli/api/compose" |
| 26 | +) |
| 27 | + |
| 28 | +type killOptions struct { |
| 29 | + *projectOptions |
| 30 | + Signal string |
| 31 | +} |
| 32 | + |
| 33 | +func killCommand(p *projectOptions) *cobra.Command { |
| 34 | + opts := killOptions{ |
| 35 | + projectOptions: p, |
| 36 | + } |
| 37 | + cmd := &cobra.Command{ |
| 38 | + Use: "kill [options] [SERVICE...]", |
| 39 | + Short: "Force stop service containers.", |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + return runKill(cmd.Context(), opts, args) |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + flags := cmd.Flags() |
| 46 | + flags.StringVarP(&opts.Signal, "signal", "s", "SIGKILL", "SIGNAL to send to the container.") |
| 47 | + |
| 48 | + return cmd |
| 49 | +} |
| 50 | + |
| 51 | +func runKill(ctx context.Context, opts killOptions, services []string) error { |
| 52 | + c, err := client.NewWithDefaultLocalBackend(ctx) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + project, err := opts.toProject(services) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + return c.ComposeService().Kill(ctx, project, compose.KillOptions{ |
| 61 | + Signal: opts.Signal, |
| 62 | + }) |
| 63 | +} |
0 commit comments