@@ -19,22 +19,29 @@ package compose
1919import (
2020 "context"
2121 "fmt"
22+ "time"
2223
24+ "github.com/compose-spec/compose-go/types"
2325 "github.com/spf13/cobra"
2426
2527 "github.com/docker/compose-cli/api/compose"
2628)
2729
2830type createOptions struct {
29- * composeOptions
31+ Build bool
32+ noBuild bool
33+ removeOrphans bool
3034 forceRecreate bool
3135 noRecreate bool
36+ recreateDeps bool
37+ noInherit bool
38+ timeChanged bool
39+ timeout int
40+ quietPull bool
3241}
3342
3443func createCommand (p * projectOptions , backend compose.Service ) * cobra.Command {
35- opts := createOptions {
36- composeOptions : & composeOptions {},
37- }
44+ opts := createOptions {}
3845 cmd := & cobra.Command {
3946 Use : "create [SERVICE...]" ,
4047 Short : "Creates containers for a service." ,
@@ -47,17 +54,15 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
4754 }
4855 return nil
4956 }),
50- RunE : Adapt (func (ctx context.Context , args []string ) error {
51- return runCreateStart (ctx , backend , upOptions {
52- composeOptions : & composeOptions {
53- projectOptions : p ,
54- Build : opts .Build ,
55- noBuild : opts .noBuild ,
56- },
57- noStart : true ,
58- forceRecreate : opts .forceRecreate ,
59- noRecreate : opts .noRecreate ,
60- }, args )
57+ RunE : p .WithProject (func (ctx context.Context , project * types.Project ) error {
58+ return backend .Create (ctx , project , compose.CreateOptions {
59+ RemoveOrphans : opts .removeOrphans ,
60+ Recreate : opts .recreateStrategy (),
61+ RecreateDependencies : opts .dependenciesRecreateStrategy (),
62+ Inherit : ! opts .noInherit ,
63+ Timeout : opts .GetTimeout (),
64+ QuietPull : false ,
65+ })
6166 }),
6267 }
6368 flags := cmd .Flags ()
@@ -67,3 +72,46 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
6772 flags .BoolVar (& opts .noRecreate , "no-recreate" , false , "If containers already exist, don't recreate them. Incompatible with --force-recreate." )
6873 return cmd
6974}
75+
76+ func (opts createOptions ) recreateStrategy () string {
77+ if opts .noRecreate {
78+ return compose .RecreateNever
79+ }
80+ if opts .forceRecreate {
81+ return compose .RecreateForce
82+ }
83+ return compose .RecreateDiverged
84+ }
85+
86+ func (opts createOptions ) dependenciesRecreateStrategy () string {
87+ if opts .noRecreate {
88+ return compose .RecreateNever
89+ }
90+ if opts .recreateDeps {
91+ return compose .RecreateForce
92+ }
93+ return compose .RecreateDiverged
94+ }
95+
96+ func (opts createOptions ) GetTimeout () * time.Duration {
97+ if opts .timeChanged {
98+ t := time .Duration (opts .timeout ) * time .Second
99+ return & t
100+ }
101+ return nil
102+ }
103+
104+ func (opts createOptions ) Apply (project * types.Project ) {
105+ if opts .Build {
106+ for i , service := range project .Services {
107+ service .PullPolicy = types .PullPolicyBuild
108+ project .Services [i ] = service
109+ }
110+ }
111+ if opts .noBuild {
112+ for i , service := range project .Services {
113+ service .Build = nil
114+ project .Services [i ] = service
115+ }
116+ }
117+ }
0 commit comments