-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.go
156 lines (129 loc) · 3.51 KB
/
lib.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
// Copyright 2016 José Santos <henrique_1609@me.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
)
type Service struct {
DevPort string `yaml:"devPort"`
AppPort string `yaml:"appPort"`
Refresh string `yaml:"refresh"`
Dir string `yaml:"dir"`
Env []string `yaml:"env"`
Commands map[string]*command `yaml:"commands"`
}
type command struct {
Building string `yaml:"-"`
Match string `yaml:"match"`
Command string `yaml:"command"`
Continue string `yaml:"continue"`
Oninit string `yaml:"oninit"`
Onexit string `yaml:"onexit"`
Dir string `yaml:"dir"`
Env []string `yaml:"env"`
Wait bool `yaml:"wait"`
Stderr bool `yaml:"stderr"`
Stdout bool `yaml:"stdout"`
pattern *regexp.Regexp
running map[string]*exec.Cmd
}
func (s *Service) Init() {
if _port != nil && *_port != "" {
ports := strings.SplitN(*_port, ":", 2)
s.DevPort = ports[0]
if len(ports) > 1 {
s.AppPort = ports[1]
}
if s.DevPort == "" {
s.DevPort = "8080"
}
if s.AppPort == "" {
port, _ := strconv.Atoi(s.AppPort)
s.AppPort = fmt.Sprint(port + 1)
}
} else if s.DevPort != "" && s.AppPort == "" {
s.AppPort = "8080"
}
if _tickerDuration != nil && *_tickerDuration != "" {
s.Refresh = *_tickerDuration
} else if s.Refresh == "" {
s.Refresh = ".5s"
}
if s.Dir == "" {
s.Dir, _ = os.Getwd()
} else {
s.Dir, _ = filepath.Abs(s.Dir)
}
s.Env = append(append([]string{}, os.Environ()...), s.Env...)
sExpander := expander(s.Env)
for commandName, command := range s.Commands {
if *_debug {
trace("loading command: %v pattern: %v cmd: %v", commandName, command.Match, command.Command)
}
if command.Match != "" {
command.pattern = regexp.MustCompile(command.Match)
}
if !command.Wait {
command.running = make(map[string]*exec.Cmd)
}
if len(command.Env) > 0 {
for i := 0; i < len(command.Env); i++ {
command.Env[i] = os.Expand(command.Env[i], sExpander)
}
command.Env = append(append(make([]string, 0, len(s.Env)), s.Env...), command.Env...)
} else {
command.Env = s.Env
}
expander := expander(command.Env)
command.Onexit = os.Expand(command.Onexit, expander)
command.Oninit = os.Expand(command.Oninit, expander)
command.Command = os.Expand(command.Command, expander)
if command.Dir != "" {
command.Dir, _ = filepath.Abs(os.Expand(command.Dir, expander))
}
if command.Oninit != "" {
trace("Running init command for %s: %q", commandName, command.Oninit)
cmd := newProcessCommand(command.Oninit)
cmd.Env = command.Env
err := cmd.Run()
if err != nil {
trace("err:%s", err)
return
}
}
}
}
func expander(envs []string) func(string) string {
return func(s string) (found string) {
for _, v := range envs {
env := strings.SplitN(v, "=", 2)
if env[0] == s {
found = env[1]
}
}
return
}
}
func (s *Service) GetRoot() string {
return s.Dir
}
func (s *Service) GetEnv() []string {
return s.Env
}