Skip to content

Commit 8e02bc3

Browse files
committed
Initial commit
0 parents  commit 8e02bc3

File tree

16 files changed

+850
-0
lines changed

16 files changed

+850
-0
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; top-most EditorConfig file
2+
root = true
3+
4+
[*]
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
9+
[*.go]
10+
indent_style = tab
11+
indent_size = 4
12+
13+
[*.yml]
14+
indent_size = 2
15+
16+
[*.md]
17+
trim_trailing_whitespace = false
18+
19+
[Makefile]
20+
indent_style = tab

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# binaries
2+
*.exe
3+
gaper
4+
srv
5+
vendor
6+
coverage.out

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: go
2+
3+
go:
4+
# - 1.7.x
5+
# - 1.8.x
6+
# - 1.9.x
7+
- 1.10.x
8+
# - master
9+
10+
script:
11+
- go version
12+
- make setup
13+
- make lint
14+
- make test
15+
16+
after_success:
17+
- bash <(curl -s https://codecov.io/bash)
18+
19+
notifications:
20+
email: false

CONTRIBUTING.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Contributing to httpfake
2+
3+
:+1::tada: First off, thanks for taking the time to contribute! :tada::+1:
4+
5+
There are few ways of contributing to gaper
6+
7+
* Report an issue.
8+
* Contribute to the code base.
9+
10+
## Report an issue
11+
12+
* Before opening the issue make sure there isn't an issue opened for the same problem
13+
* Include the Go and Gaper version you are using
14+
* If it is a bug, please include all info to reproduce the problem
15+
16+
## Contribute to the code base
17+
18+
### Pull Request
19+
20+
* Please discuss the suggested changes on a issue before working on it. Just to make sure the change makes sense before you spending any time on it.
21+
22+
### Setupping development
23+
24+
```
25+
make setup
26+
```
27+
28+
### Running gaper in development
29+
30+
```
31+
make build && ./gaper --verbose --bin-name srv --build-path ./testdata/server
32+
```
33+
34+
### Running lint
35+
36+
```
37+
make lint
38+
```
39+
40+
### Running tests
41+
42+
All tests:
43+
```
44+
make test
45+
```
46+
47+
A single test:
48+
```
49+
go test -run TestSimplePost ./...
50+
```
51+

Gopkg.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[[constraint]]
2+
name = "github.com/fatih/color"
3+
version = "1.7.0"
4+
5+
[[constraint]]
6+
name = "github.com/mattn/go-shellwords"
7+
version = "1.0.3"
8+
9+
[[constraint]]
10+
name = "github.com/urfave/cli"
11+
version = "1.20.0"
12+
13+
[prune]
14+
go-tests = true
15+
unused-packages = true

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Max Claus Nunes
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
OS := $(shell uname -s)
2+
TEST_PACKAGES := $(shell go list ./...)
3+
COVER_PACKAGES := $(shell go list ./... | paste -sd "," -)
4+
LINTER := $(shell command -v gometalinter 2> /dev/null)
5+
6+
.PHONY: setup
7+
8+
setup:
9+
ifeq ($(OS), Darwin)
10+
brew install dep
11+
else
12+
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
13+
endif
14+
dep ensure -vendor-only
15+
ifndef LINTER
16+
@echo "Installing linter"
17+
@go get -u github.com/alecthomas/gometalinter
18+
@gometalinter --install
19+
endif
20+
21+
build:
22+
@go build .
23+
24+
## lint: Validate golang code
25+
lint:
26+
@gometalinter \
27+
--deadline=120s \
28+
--line-length=120 \
29+
--enable-all \
30+
--vendor ./...
31+
32+
test:
33+
@go test -v -coverpkg $(COVER_PACKAGES) \
34+
-covermode=atomic -coverprofile=coverage.out $(TEST_PACKAGES)
35+
36+
cover: test
37+
@go tool cover -html=coverage.out
38+
39+
fmt:
40+
@find . -name '*.go' -not -wholename './vendor/*' | \
41+
while read -r file; do gofmt -w -s "$$file"; goimports -w "$$file"; done

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
gaper
2+
=====
3+
4+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
5+
[![Build Status](https://travis-ci.org/maxcnunes/gaper.svg?branch=master)](https://travis-ci.org/maxcnunes/gaper)
6+
[![Coverage Status](https://codecov.io/gh/maxcnunes/gaper/branch/master/graph/badge.svg)](https://codecov.io/gh/maxcnunes/gaper)
7+
[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/maxcnunes/gaper)
8+
[![Go Report Card](https://goreportcard.com/badge/github.com/maxcnunes/gaper)](https://goreportcard.com/report/github.com/maxcnunes/gaper)
9+
10+
Restarts programs when they crash or a watched file changes.
11+
12+
**NOT STABLE YET, STILL IN DEVELOPMENT**: Please, check out [this ticket](https://github.com/maxcnunes/gaper/issues/1) to follow its progress.
13+
14+
## Installation
15+
16+
```
17+
go get -u github.com/maxcnunes/gaper
18+
```
19+
20+
## Changelog
21+
22+
See [Releases](https://github.com/maxcnunes/gaper/releases) for detailed history changes.
23+
24+
## Usage
25+
26+
```
27+
NAME:
28+
gaper - Used to restart programs when they crash or a watched file changes
29+
30+
USAGE:
31+
gaper [global options] command [command options] [arguments...]
32+
33+
VERSION:
34+
0.0.0
35+
36+
COMMANDS:
37+
help, h Shows a list of commands or help for one command
38+
39+
GLOBAL OPTIONS:
40+
--bin-name value name for the binary built by Gaper for the executed program
41+
--build-path value path to the program source code
42+
--build-args value build arguments passed to the program
43+
--verbose turns on the verbose messages from Gaper
44+
--watch value, -w value a comma-delimited list of folders or files to watch for changes
45+
--ignore value, -i value a comma-delimited list of folders or files to ignore for changes
46+
--poll-interval value, -p value how often in milliseconds to poll watched files for changes (default: 500)
47+
--extensions value, -e value a comma-delimited list of file extensions to watch for changes (default: "go")
48+
----no-restart-on value, -n value don't automatically restart the executed program if it ends.
49+
If "error", an exit code of 0 will still restart.
50+
If "exit", no restart regardless of exit code.
51+
If "success", no restart only if exit code is 0.
52+
--help, -h show help
53+
--version, -v print the version
54+
```
55+
56+
## Contributing
57+
58+
See the [Contributing guide](/CONTRIBUTING.md) for steps on how to contribute to this project.
59+
60+
## Reference
61+
62+
This package was heavily inspired by [gin](https://github.com/codegangsta/gin) and [node-supervisor](https://github.com/petruisfan/node-supervisor).
63+
64+
Basically, Gaper is a mixing of those projects above. It started from **gin** code base and I rewrote it aiming to get
65+
something similar to **node-supervisor** (but simpler). A big thanks for those projects and for the people behind it!
66+
:clap::clap:
67+
68+
### How is Gaper different of Gin
69+
70+
The main difference is that Gaper removes a layer of complexity from Gin which has a proxy running on top of
71+
the executed server. It allows to postpone a build and reload the server when the first call hits it. With Gaper
72+
we don't care about that feature, it just restarts your server whenever a change is made.

builder.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"path/filepath"
7+
"runtime"
8+
"strings"
9+
)
10+
11+
// Builder ...
12+
type Builder interface {
13+
Build() error
14+
Binary() string
15+
Errors() string
16+
}
17+
18+
type builder struct {
19+
dir string
20+
binary string
21+
errors string
22+
wd string
23+
buildArgs []string
24+
}
25+
26+
// NewBuilder ...
27+
func NewBuilder(dir string, bin string, wd string, buildArgs []string) Builder {
28+
if len(bin) == 0 {
29+
bin = "bin"
30+
}
31+
32+
// does not work on Windows without the ".exe" extension
33+
if runtime.GOOS == OSWindows {
34+
// check if it already has the .exe extension
35+
if !strings.HasSuffix(bin, ".exe") {
36+
bin += ".exe"
37+
}
38+
}
39+
40+
return &builder{dir: dir, binary: bin, wd: wd, buildArgs: buildArgs}
41+
}
42+
43+
// Binary ...
44+
func (b *builder) Binary() string {
45+
return b.binary
46+
}
47+
48+
// Errors ...
49+
func (b *builder) Errors() string {
50+
return b.errors
51+
}
52+
53+
// Build ...
54+
func (b *builder) Build() error {
55+
logger.Info("Building program")
56+
args := append([]string{"go", "build", "-o", filepath.Join(b.wd, b.binary)}, b.buildArgs...)
57+
logger.Debug("Build command", args)
58+
59+
command := exec.Command(args[0], args[1:]...) // nolint gas
60+
command.Dir = b.dir
61+
62+
output, err := command.CombinedOutput()
63+
if err != nil {
64+
return err
65+
}
66+
67+
if command.ProcessState.Success() {
68+
b.errors = ""
69+
} else {
70+
b.errors = string(output)
71+
}
72+
73+
if len(b.errors) > 0 {
74+
return fmt.Errorf(b.errors)
75+
}
76+
77+
return nil
78+
}

0 commit comments

Comments
 (0)