-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathstart.go
170 lines (135 loc) · 4.19 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package commands
import (
"fmt"
"kool-dev/kool/core/builder"
"kool-dev/kool/core/environment"
"kool-dev/kool/core/network"
"kool-dev/kool/services/checker"
"kool-dev/kool/services/updater"
"strings"
"github.com/spf13/cobra"
)
// KoolStartFlags holds the flags for the kool start command
type KoolStartFlags struct {
Foreground bool
Rebuild bool
Profile string
}
// KoolStart holds handlers and functions for starting containers logic
type KoolStart struct {
DefaultKoolService
Flags *KoolStartFlags
check checker.Checker
net network.Handler
envStorage environment.EnvStorage
start builder.Command
rebuilder KoolService
}
// KoolRebuild holds handlers for updating the service's images
type KoolRebuild struct {
DefaultKoolService
pull, build builder.Command
}
// NewStartCommand initializes new kool start Cobra command
func NewStartCommand(start *KoolStart) (startCmd *cobra.Command) {
startCmd = &cobra.Command{
Use: "start [SERVICE...]",
SuggestFor: []string{"up"},
Short: "Start service containers defined in docker-compose.yml",
Long: `Start one or more specified [SERVICE] containers. If no [SERVICE] is provided,
all containers are started. If the containers are already running, they are recreated.`,
RunE: DefaultCommandRunFunction(CheckNewVersion(start, &updater.DefaultUpdater{RootCommand: rootCmd}, version == DEV_VERSION)),
DisableFlagsInUseLine: true,
}
startCmd.Flags().BoolVarP(&start.Flags.Foreground, "foreground", "f", false, "Start containers in foreground mode")
startCmd.Flags().BoolVarP(&start.Flags.Rebuild, "rebuild", "b", false, "Updates and builds service's images")
startCmd.Flags().StringVarP(&start.Flags.Profile, "profile", "", "", "Specify a profile to enable")
return
}
// NewKoolStart creates a new pointer with default KoolStart service
// dependencies.
func NewKoolStart() *KoolStart {
defaultKoolService := newDefaultKoolService()
return &KoolStart{
*defaultKoolService,
&KoolStartFlags{false, false, ""},
checker.NewChecker(defaultKoolService.shell),
network.NewHandler(defaultKoolService.shell),
environment.NewEnvStorage(),
builder.NewCommand("docker", "compose", "up", "--force-recreate"),
&KoolRebuild{
*newDefaultKoolService(),
builder.NewCommand("docker", "compose", "pull"),
builder.NewCommand("docker", "compose", "build", "--pull"),
},
}
}
func AddKoolStart(root *cobra.Command) {
root.AddCommand(NewStartCommand(NewKoolStart()))
}
// Execute runs the rebuild logic
func (r *KoolRebuild) Execute(args []string) (err error) {
if err = r.Shell().Interactive(r.pull); err != nil {
return
}
err = r.Shell().Interactive(r.build)
return
}
// Execute runs the start logic with incoming arguments
func (s *KoolStart) Execute(args []string) (err error) {
if s.Flags.Rebuild {
if err = s.rebuild(); err != nil {
return
}
}
if len(s.Flags.Profile) > 0 {
s.start.AppendArgs("--profile", s.Flags.Profile)
}
if !s.Flags.Foreground {
s.start.AppendArgs("-d")
}
if err = s.checkDependencies(); err != nil {
if strings.HasPrefix(err.Error(), "no configuration file provided: not found") {
err = fmt.Errorf("could not find docker-compose.yml - check your current working directory.\n\n[err: %v]", err)
}
return
}
err = s.Shell().Interactive(s.start, args...)
return
}
func (s *KoolStart) rebuild() (err error) {
var task = NewKoolTask("Updating service's images", s.rebuilder)
task.SetFrameOutput(false)
task.Shell().SetInStream(s.Shell().InStream())
task.Shell().SetOutStream(s.Shell().OutStream())
task.Shell().SetErrStream(s.Shell().ErrStream())
err = task.Run(nil)
return
}
func (s *KoolStart) checkDependencies() (err error) {
chErrDocker, chErrNetwork := s.checkDocker(), s.checkNetwork()
errDocker, errNetwork := <-chErrDocker, <-chErrNetwork
if errDocker != nil {
err = errDocker
return
}
if errNetwork != nil {
err = errNetwork
return
}
return
}
func (s *KoolStart) checkDocker() <-chan error {
err := make(chan error)
go func() {
err <- s.check.Check()
}()
return err
}
func (s *KoolStart) checkNetwork() <-chan error {
err := make(chan error)
go func() {
err <- s.net.HandleGlobalNetwork(s.envStorage.Get("KOOL_GLOBAL_NETWORK"))
}()
return err
}