Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support Docker build
  • Loading branch information
cosmtrek committed Oct 18, 2017
1 parent 5f449c2 commit 480fad6
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
@@ -0,0 +1,2 @@
bin
vendor
11 changes: 11 additions & 0 deletions Dockerfile
@@ -0,0 +1,11 @@
FROM golang

MAINTAINER Rick Yu <cosmtrek@gmail.com>

ENV GOPATH /go

ADD . /go/src/github.com/cosmtrek/air
WORKDIR /go/src/github.com/cosmtrek/air
RUN make ci && make install

ENTRYPOINT ["/go/bin/air"]
9 changes: 6 additions & 3 deletions Makefile
Expand Up @@ -22,7 +22,6 @@ check:
.PHONY: ci
ci: init
@glide install
@make check

.PHONY: build
build: check
Expand All @@ -34,5 +33,9 @@ install: check

.PHONY: release
release: check
GOOS=darwin go build -ldflags '$(LDFLAGS)' -o bin/darwin/air
GOOS=linux go build -ldflags '$(LDFLAGS)' -o bin/linux/air
GOOS=darwin GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o bin/darwin/air
GOOS=linux GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o bin/linux/air

.PHONY: docker-image
docker-image:
docker build -t cosmtrek/air:latest -f ./Dockerfile .
22 changes: 21 additions & 1 deletion README.md
Expand Up @@ -39,7 +39,27 @@ curl -fLo ~/.air https://raw.githubusercontent.com/cosmtrek/air/master/bin/linux
chmod +x ~/.air
```

Sorry for no Windows platform I'm not working on, but PRs are welcome :)
### Docker way

```bash
docker run -it --rm \
-e "air_wd=<YOUR PROJECT DIR>" \
-v $(pwd):<YOUR PROJECT DIR> \
-p <PORT>:<YOUR APP SERVER PORT> \
cosmtrek/air
```

For example, one of my project runs in docker:

```bash
docker run -it --rm \
-e "air_wd=/go/src/github.com/cosmtrek/hub" \
-v $(pwd):/go/src/github.com/cosmtrek/hub \
-p 9090:9090 \
cosmtrek/air
```

Sorry for no Windows platform since I'm not working on it, but PRs are welcome :)

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

Expand Down
Binary file modified bin/darwin/air
Binary file not shown.
Binary file modified bin/linux/air
Binary file not shown.
25 changes: 20 additions & 5 deletions runner/config.go
Expand Up @@ -9,7 +9,10 @@ import (
"github.com/pelletier/go-toml"
)

const dftConf = ".air.conf"
const (
dftConf = ".air.conf"
airWd = "air_wd"
)

type config struct {
Root string `toml:"root"`
Expand Down Expand Up @@ -42,10 +45,15 @@ func initConfig(path string) (*config, error) {
dft := defaultConfig()
if path == "" {
useDftCfg = true
// when path is blank, first find `.air.conf` in root directory, if not found, use defaults
path, err = dftConfPath()
if err != nil {
return &dft, nil
// when path is blank, first find `.air.conf` in `air_wd` and current working directory, if not found, use defaults
wd := os.Getenv(airWd)
if wd != "" {
path = filepath.Join(wd, dftConf)
} else {
path, err = dftConfPath()
if err != nil {
return &dft, nil
}
}
}
cfg, err := readConfig(path)
Expand Down Expand Up @@ -99,6 +107,13 @@ func readConfig(path string) (*config, error) {
func (c *config) preprocess() error {
// TODO: merge defaults if some options are not set
var err error
cwd := os.Getenv(airWd)
if cwd != "" {
if err = os.Chdir(cwd); err != nil {
return err
}
c.Root = cwd
}
c.Root, err = expandPath(c.Root)
if c.TmpDir == "" {
c.TmpDir = "tmp"
Expand Down
11 changes: 7 additions & 4 deletions runner/engine.go
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
Expand Down Expand Up @@ -65,12 +66,12 @@ func NewEngine(cfgPath string, debugMode bool) (*Engine, error) {

// Run run run
func (e *Engine) Run() {
var err error
e.mainDebug("CWD: %s", e.config.Root)

var err error
if err = e.checkRunEnv(); err != nil {
os.Exit(1)
}

if err = e.watching(e.config.watchDirRoot()); err != nil {
os.Exit(1)
}
Expand Down Expand Up @@ -99,6 +100,7 @@ func (e *Engine) watching(root string) error {
}
// exclude tmp dir
if e.isTmpDir(path) {
e.watcherLog("!exclude %s", e.config.rel(path))
return filepath.SkipDir
}
// exclude hidden directories like .git, .idea, etc.
Expand Down Expand Up @@ -286,8 +288,9 @@ func (e *Engine) runBin() error {
go io.Copy(aw, stderr)
go io.Copy(aw, stdout)

go func() {
go func(cmd *exec.Cmd) {
<-e.binStopCh
e.mainDebug("trying to kill cmd %+v", cmd)
pid, err := killCmd(cmd)
if err != nil {
e.mainLog("failed to kill PID %d, error: %s", pid, err.Error())
Expand All @@ -296,7 +299,7 @@ func (e *Engine) runBin() error {
e.withLock(func() {
e.binRunning = false
})
}()
}(cmd)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions runner/utils.go
Expand Up @@ -61,7 +61,7 @@ func (e *Engine) appDebug(format string, v ...interface{}) {
}

func (e *Engine) isTmpDir(path string) bool {
return e.config.fullPath(path) == e.config.tmpPath()
return path == e.config.tmpPath()
}

func isHiddenDirectory(path string) bool {
Expand Down Expand Up @@ -111,7 +111,7 @@ func (e *Engine) withLock(f func()) {
}

func expandPath(path string) (string, error) {
if strings.HasPrefix(path, "~") {
if strings.HasPrefix(path, "~/") {
home := os.Getenv("HOME")
return home + path[1:], nil
}
Expand All @@ -124,7 +124,7 @@ func expandPath(path string) (string, error) {
return wd, nil
}
if strings.HasPrefix(path, "./") {
return wd + path[2:], nil
return wd + path[1:], nil
}
return path, nil
}
Expand Down

0 comments on commit 480fad6

Please sign in to comment.