2020-05-14 21:13:07 +02:00
/ *
2020-09-22 12:13:00 +02:00
Copyright 2020 Docker Compose CLI authors
2020-05-14 21:13:07 +02:00
2020-06-18 16:13:24 +02:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
2020-05-14 21:13:07 +02:00
2020-06-18 16:13:24 +02:00
http : //www.apache.org/licenses/LICENSE-2.0
2020-05-14 21:13:07 +02:00
2020-06-18 16:13:24 +02:00
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
2020-05-14 21:13:07 +02:00
* /
2020-05-14 21:04:38 +02:00
package compose
import (
"context"
2021-02-17 15:22:26 +01:00
"fmt"
2022-02-23 14:04:12 +01:00
"os"
2021-02-15 13:54:55 +01:00
"time"
2020-05-14 21:04:38 +02:00
2023-08-30 10:15:35 +02:00
"github.com/docker/cli/cli/command"
2022-03-04 16:38:21 +01:00
"github.com/docker/compose/v2/pkg/utils"
2022-02-17 11:48:09 +01:00
"github.com/sirupsen/logrus"
2020-05-14 21:04:38 +02:00
"github.com/spf13/cobra"
2022-02-17 11:48:09 +01:00
"github.com/spf13/pflag"
2020-05-14 21:04:38 +02:00
2021-08-31 18:53:24 +02:00
"github.com/docker/compose/v2/pkg/api"
2020-05-14 21:04:38 +02:00
)
2021-01-20 11:02:46 +01:00
type downOptions struct {
2022-12-19 16:38:36 +00:00
* ProjectOptions
2021-02-03 15:50:49 +01:00
removeOrphans bool
2021-02-15 13:54:55 +01:00
timeChanged bool
timeout int
2021-02-17 15:22:26 +01:00
volumes bool
images string
2021-01-20 11:02:46 +01:00
}
2023-08-30 10:15:35 +02:00
func downCommand ( p * ProjectOptions , dockerCli command . Cli , backend api . Service ) * cobra . Command {
2021-01-20 11:02:46 +01:00
opts := downOptions {
2022-12-19 16:38:36 +00:00
ProjectOptions : p ,
2021-01-20 11:02:46 +01:00
}
2020-05-14 21:04:38 +02:00
downCmd := & cobra . Command {
2023-05-10 15:21:34 +02:00
Use : "down [OPTIONS] [SERVICES]" ,
2020-12-10 15:43:20 +01:00
Short : "Stop and remove containers, networks" ,
2021-10-13 08:59:16 +05:30
PreRunE : AdaptCmd ( func ( ctx context . Context , cmd * cobra . Command , args [ ] string ) error {
2021-02-15 13:54:55 +01:00
opts . timeChanged = cmd . Flags ( ) . Changed ( "timeout" )
2021-02-17 15:22:26 +01:00
if opts . images != "" {
if opts . images != "all" && opts . images != "local" {
return fmt . Errorf ( "invalid value for --rmi: %q" , opts . images )
}
}
2021-05-19 18:49:33 +02:00
return nil
} ) ,
RunE : Adapt ( func ( ctx context . Context , args [ ] string ) error {
2023-08-30 10:15:35 +02:00
return runDown ( ctx , dockerCli , backend , opts , args )
2021-04-15 12:43:18 +02:00
} ) ,
2021-06-10 09:28:06 -03:00
ValidArgsFunction : noCompletion ( ) ,
2020-05-14 21:04:38 +02:00
}
2021-02-03 15:50:49 +01:00
flags := downCmd . Flags ( )
2023-05-14 16:50:41 +02:00
removeOrphans := utils . StringToBool ( os . Getenv ( ComposeRemoveOrphans ) )
2024-02-14 20:02:37 +01:00
flags . BoolVar ( & opts . removeOrphans , "remove-orphans" , removeOrphans , "Remove containers for services not defined in the Compose file" )
2023-06-12 15:18:25 +02:00
flags . IntVarP ( & opts . timeout , "timeout" , "t" , 0 , "Specify a shutdown timeout in seconds" )
2024-02-14 20:02:37 +01:00
flags . BoolVarP ( & opts . volumes , "volumes" , "v" , false , ` Remove named volumes declared in the "volumes" section of the Compose file and anonymous volumes attached to containers ` )
2021-06-07 11:21:57 +02:00
flags . StringVar ( & opts . images , "rmi" , "" , ` Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all") ` )
2022-02-17 11:48:09 +01:00
flags . SetNormalizeFunc ( func ( f * pflag . FlagSet , name string ) pflag . NormalizedName {
2022-07-13 01:00:36 +02:00
if name == "volume" {
2022-02-17 11:48:09 +01:00
name = "volumes"
logrus . Warn ( "--volume is deprecated, please use --volumes" )
}
return pflag . NormalizedName ( name )
} )
2020-05-14 21:04:38 +02:00
return downCmd
}
2023-08-30 10:15:35 +02:00
func runDown ( ctx context . Context , dockerCli command . Cli , backend api . Service , opts downOptions , services [ ] string ) error {
2024-02-05 17:38:15 -05:00
project , name , err := opts . projectOrName ( ctx , dockerCli , services ... )
2022-04-11 10:58:20 +02:00
if err != nil {
return err
2021-06-07 11:21:57 +02:00
}
2021-01-14 15:59:57 +01:00
2021-06-07 11:21:57 +02:00
var timeout * time . Duration
if opts . timeChanged {
timeoutValue := time . Duration ( opts . timeout ) * time . Second
timeout = & timeoutValue
}
2021-06-14 16:26:14 +02:00
return backend . Down ( ctx , name , api . DownOptions {
2021-06-07 11:21:57 +02:00
RemoveOrphans : opts . removeOrphans ,
Project : project ,
Timeout : timeout ,
Images : opts . images ,
Volumes : opts . volumes ,
2023-05-10 15:21:34 +02:00
Services : services ,
2020-08-19 16:15:34 +02:00
} )
2020-05-14 21:04:38 +02:00
}