@@ -19,22 +19,29 @@ package compose
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "time"
22
23
24
+ "github.com/compose-spec/compose-go/types"
23
25
"github.com/spf13/cobra"
24
26
25
27
"github.com/docker/compose-cli/api/compose"
26
28
)
27
29
28
30
type createOptions struct {
29
- * composeOptions
31
+ Build bool
32
+ noBuild bool
33
+ removeOrphans bool
30
34
forceRecreate bool
31
35
noRecreate bool
36
+ recreateDeps bool
37
+ noInherit bool
38
+ timeChanged bool
39
+ timeout int
40
+ quietPull bool
32
41
}
33
42
34
43
func createCommand (p * projectOptions , backend compose.Service ) * cobra.Command {
35
- opts := createOptions {
36
- composeOptions : & composeOptions {},
37
- }
44
+ opts := createOptions {}
38
45
cmd := & cobra.Command {
39
46
Use : "create [SERVICE...]" ,
40
47
Short : "Creates containers for a service." ,
@@ -47,17 +54,15 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
47
54
}
48
55
return nil
49
56
}),
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
+ })
61
66
}),
62
67
}
63
68
flags := cmd .Flags ()
@@ -67,3 +72,46 @@ func createCommand(p *projectOptions, backend compose.Service) *cobra.Command {
67
72
flags .BoolVar (& opts .noRecreate , "no-recreate" , false , "If containers already exist, don't recreate them. Incompatible with --force-recreate." )
68
73
return cmd
69
74
}
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