-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
run.go
158 lines (136 loc) · 2.91 KB
/
run.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
package run
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"github.com/asim/go-micro/cmd/gomu/cmd"
"go-micro.dev/v4/runtime"
"go-micro.dev/v4/runtime/local/git"
"github.com/fsnotify/fsnotify"
"github.com/urfave/cli/v2"
)
var (
DefaultRetries = 3
flags []cli.Flag = []cli.Flag{
&cli.StringFlag{
Name: "command",
Usage: "command to execute",
},
&cli.StringFlag{
Name: "args",
Usage: "command arguments",
},
&cli.StringFlag{
Name: "type",
Usage: "the type of service to operate on",
},
}
)
func init() {
cmd.Register(&cli.Command{
Name: "run",
Usage: "Build and run a service continuously, e.g. gomu run [github.com/auditemarlow/helloworld]",
Flags: flags,
Action: Run,
})
}
// Run runs a service and watches the project directory for change events. On
// write, the service is restarted. Exits on error.
func Run(ctx *cli.Context) error {
wd, err := os.Getwd()
if err != nil {
return err
}
source, err := git.ParseSourceLocal(wd, ctx.Args().First())
if err != nil {
return err
}
svc := &runtime.Service{
Name: source.RuntimeName(),
Source: source.RuntimeSource(),
Version: source.Ref,
Metadata: make(map[string]string),
}
typ := ctx.String("type")
command := strings.TrimSpace(ctx.String("command"))
args := strings.TrimSpace(ctx.String("args"))
r := *cmd.DefaultCmd.Options().Runtime
var retries = DefaultRetries
if ctx.IsSet("retries") {
retries = ctx.Int("retries")
}
opts := []runtime.CreateOption{
runtime.WithOutput(os.Stdout),
runtime.WithRetries(retries),
runtime.CreateType(typ),
}
if len(command) > 0 {
opts = append(opts, runtime.WithCommand(command))
}
if len(args) > 0 {
opts = append(opts, runtime.WithArgs(args))
}
if err := r.Create(svc, opts...); err != nil {
return err
}
done := make(chan bool)
if r.String() == "local" {
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
go func() {
<-sig
r.Delete(svc)
done <- true
}()
}
if source.Local {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println(err)
}
defer watcher.Close()
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
r.Update(svc)
}
if event.Op&fsnotify.Create == fsnotify.Create {
watcher.Add(event.Name)
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
watcher.Remove(event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
fmt.Println("ERROR", err)
}
}
}()
var files []string
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
files = append(files, path)
return nil
})
for _, file := range files {
if err := watcher.Add(file); err != nil {
return err
}
}
}
if r.String() == "local" {
<-done
}
return nil
}