Skip to content

Commit 61943e0

Browse files
committed
detach wip
1 parent 97a344b commit 61943e0

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

cmd/services_run.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ import (
66
"github.com/moonwalker/luna/support"
77
)
88

9-
var svcRunCmd = &cobra.Command{
10-
Use: "run",
11-
Short: "Run services",
9+
var (
10+
detach bool
1211

13-
Run: func(cmd *cobra.Command, args []string) {
14-
pm := support.NewPM(cfg)
15-
pm.Run(args)
16-
},
17-
}
12+
svcRunCmd = &cobra.Command{
13+
Use: "run",
14+
Short: "Run services",
15+
16+
Run: func(cmd *cobra.Command, args []string) {
17+
pm := support.NewPM(cfg)
18+
pm.Run(args, detach)
19+
},
20+
}
21+
)
1822

1923
func init() {
24+
svcRunCmd.Flags().BoolVarP(&detach, "detach", "d", false, "run services in the background")
2025
servicesCmd.AddCommand(svcRunCmd)
2126
}

cmd/services_stop.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/moonwalker/luna/support"
7+
)
8+
9+
var (
10+
svcStopCmd = &cobra.Command{
11+
Use: "stop",
12+
Short: "Stop services",
13+
14+
Run: func(cmd *cobra.Command, args []string) {
15+
pm := support.NewPM(cfg)
16+
pm.Stop(args)
17+
},
18+
}
19+
)
20+
21+
func init() {
22+
servicesCmd.AddCommand(svcStopCmd)
23+
}

support/pm.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
type PM struct {
15-
config Config
15+
config Config
1616
services []string // Specific services to run, if empty, run all from config
1717
}
1818

@@ -22,10 +22,10 @@ func NewPM(cfg Config) *PM {
2222
}
2323
}
2424

25-
func (pm *PM) Run(services []string) {
25+
func (pm *PM) Run(services []string, detach bool) {
2626
for _, s := range services {
2727
hasService := false
28-
for name, _ := range pm.config.Services {
28+
for name := range pm.config.Services {
2929
if name == s {
3030
hasService = true
3131
}
@@ -37,7 +37,26 @@ func (pm *PM) Run(services []string) {
3737
}
3838
pm.services = services
3939
pm.start()
40-
waitSig()
40+
if !detach {
41+
waitSig()
42+
pm.stop()
43+
}
44+
}
45+
46+
func (pm *PM) Stop(services []string) {
47+
for _, s := range services {
48+
hasService := false
49+
for name := range pm.config.Services {
50+
if name == s {
51+
hasService = true
52+
}
53+
}
54+
if !hasService {
55+
fmt.Println("Could not find service " + s)
56+
os.Exit(1)
57+
}
58+
}
59+
pm.services = services
4160
pm.stop()
4261
}
4362

0 commit comments

Comments
 (0)