Skip to content

Commit 480fad6

Browse files
committed
Support Docker build
1 parent 5f449c2 commit 480fad6

File tree

9 files changed

+70
-16
lines changed

9 files changed

+70
-16
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin
2+
vendor

Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang
2+
3+
MAINTAINER Rick Yu <[email protected]>
4+
5+
ENV GOPATH /go
6+
7+
ADD . /go/src/github.com/cosmtrek/air
8+
WORKDIR /go/src/github.com/cosmtrek/air
9+
RUN make ci && make install
10+
11+
ENTRYPOINT ["/go/bin/air"]

Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ check:
2222
.PHONY: ci
2323
ci: init
2424
@glide install
25-
@make check
2625

2726
.PHONY: build
2827
build: check
@@ -34,5 +33,9 @@ install: check
3433

3534
.PHONY: release
3635
release: check
37-
GOOS=darwin go build -ldflags '$(LDFLAGS)' -o bin/darwin/air
38-
GOOS=linux go build -ldflags '$(LDFLAGS)' -o bin/linux/air
36+
GOOS=darwin GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o bin/darwin/air
37+
GOOS=linux GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o bin/linux/air
38+
39+
.PHONY: docker-image
40+
docker-image:
41+
docker build -t cosmtrek/air:latest -f ./Dockerfile .

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,27 @@ curl -fLo ~/.air https://raw.githubusercontent.com/cosmtrek/air/master/bin/linux
3939
chmod +x ~/.air
4040
```
4141

42-
Sorry for no Windows platform I'm not working on, but PRs are welcome :)
42+
### Docker way
43+
44+
```bash
45+
docker run -it --rm \
46+
-e "air_wd=<YOUR PROJECT DIR>" \
47+
-v $(pwd):<YOUR PROJECT DIR> \
48+
-p <PORT>:<YOUR APP SERVER PORT> \
49+
cosmtrek/air
50+
```
51+
52+
For example, one of my project runs in docker:
53+
54+
```bash
55+
docker run -it --rm \
56+
-e "air_wd=/go/src/github.com/cosmtrek/hub" \
57+
-v $(pwd):/go/src/github.com/cosmtrek/hub \
58+
-p 9090:9090 \
59+
cosmtrek/air
60+
```
61+
62+
Sorry for no Windows platform since I'm not working on it, but PRs are welcome :)
4363

4464
For less typing, you could add `alias air='~/.air'` to your `.bashrc` or `.zshrc`.
4565

bin/darwin/air

112 Bytes
Binary file not shown.

bin/linux/air

138 Bytes
Binary file not shown.

runner/config.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"github.com/pelletier/go-toml"
1010
)
1111

12-
const dftConf = ".air.conf"
12+
const (
13+
dftConf = ".air.conf"
14+
airWd = "air_wd"
15+
)
1316

1417
type config struct {
1518
Root string `toml:"root"`
@@ -42,10 +45,15 @@ func initConfig(path string) (*config, error) {
4245
dft := defaultConfig()
4346
if path == "" {
4447
useDftCfg = true
45-
// when path is blank, first find `.air.conf` in root directory, if not found, use defaults
46-
path, err = dftConfPath()
47-
if err != nil {
48-
return &dft, nil
48+
// when path is blank, first find `.air.conf` in `air_wd` and current working directory, if not found, use defaults
49+
wd := os.Getenv(airWd)
50+
if wd != "" {
51+
path = filepath.Join(wd, dftConf)
52+
} else {
53+
path, err = dftConfPath()
54+
if err != nil {
55+
return &dft, nil
56+
}
4957
}
5058
}
5159
cfg, err := readConfig(path)
@@ -99,6 +107,13 @@ func readConfig(path string) (*config, error) {
99107
func (c *config) preprocess() error {
100108
// TODO: merge defaults if some options are not set
101109
var err error
110+
cwd := os.Getenv(airWd)
111+
if cwd != "" {
112+
if err = os.Chdir(cwd); err != nil {
113+
return err
114+
}
115+
c.Root = cwd
116+
}
102117
c.Root, err = expandPath(c.Root)
103118
if c.TmpDir == "" {
104119
c.TmpDir = "tmp"

runner/engine.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"os"
9+
"os/exec"
910
"path/filepath"
1011
"sync"
1112
"time"
@@ -65,12 +66,12 @@ func NewEngine(cfgPath string, debugMode bool) (*Engine, error) {
6566

6667
// Run run run
6768
func (e *Engine) Run() {
68-
var err error
69+
e.mainDebug("CWD: %s", e.config.Root)
6970

71+
var err error
7072
if err = e.checkRunEnv(); err != nil {
7173
os.Exit(1)
7274
}
73-
7475
if err = e.watching(e.config.watchDirRoot()); err != nil {
7576
os.Exit(1)
7677
}
@@ -99,6 +100,7 @@ func (e *Engine) watching(root string) error {
99100
}
100101
// exclude tmp dir
101102
if e.isTmpDir(path) {
103+
e.watcherLog("!exclude %s", e.config.rel(path))
102104
return filepath.SkipDir
103105
}
104106
// exclude hidden directories like .git, .idea, etc.
@@ -286,8 +288,9 @@ func (e *Engine) runBin() error {
286288
go io.Copy(aw, stderr)
287289
go io.Copy(aw, stdout)
288290

289-
go func() {
291+
go func(cmd *exec.Cmd) {
290292
<-e.binStopCh
293+
e.mainDebug("trying to kill cmd %+v", cmd)
291294
pid, err := killCmd(cmd)
292295
if err != nil {
293296
e.mainLog("failed to kill PID %d, error: %s", pid, err.Error())
@@ -296,7 +299,7 @@ func (e *Engine) runBin() error {
296299
e.withLock(func() {
297300
e.binRunning = false
298301
})
299-
}()
302+
}(cmd)
300303
return nil
301304
}
302305

runner/utils.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (e *Engine) appDebug(format string, v ...interface{}) {
6161
}
6262

6363
func (e *Engine) isTmpDir(path string) bool {
64-
return e.config.fullPath(path) == e.config.tmpPath()
64+
return path == e.config.tmpPath()
6565
}
6666

6767
func isHiddenDirectory(path string) bool {
@@ -111,7 +111,7 @@ func (e *Engine) withLock(f func()) {
111111
}
112112

113113
func expandPath(path string) (string, error) {
114-
if strings.HasPrefix(path, "~") {
114+
if strings.HasPrefix(path, "~/") {
115115
home := os.Getenv("HOME")
116116
return home + path[1:], nil
117117
}
@@ -124,7 +124,7 @@ func expandPath(path string) (string, error) {
124124
return wd, nil
125125
}
126126
if strings.HasPrefix(path, "./") {
127-
return wd + path[2:], nil
127+
return wd + path[1:], nil
128128
}
129129
return path, nil
130130
}

0 commit comments

Comments
 (0)