Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Remove build step #1

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[constraint]]
name = "github.com/devlocker/devproxy"
version = "0.2.0"
version = "0.2.1"

[[constraint]]
name = "github.com/fatih/color"
Expand Down
116 changes: 56 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ will watch your filesystem for changes and automatically recompile and restart
code on change.

`tychus` is language agnostic - it can be configured to work with just about
anything: Go, Rust, Ruby, Python, etc. Should you desire you can use `tychus`
as a proxy to your application. The proxy is pretty smart - it will pause
requests while your app rebuilds and won't let you run into that super annoying
case where you refresh your webpage after your app boots, but before it can
serve a request.
anything: Go, Rust, Ruby, Python, etc.

Should you desire you can use `tychus` as a proxy to your application. It will
pause requests while your application rebuilds & restarts.


## Installation
Expand All @@ -29,82 +28,79 @@ Assuming you have a working Go environment and `GOPATH/bin` is in your `PATH`
go get github.com/devlocker/tychus
```

### Windows
Currently isn't supported :(

## Getting Started
You will need to create a `.tychus.yml` file configuration file. Easiest way is
to generate one with:
to generate one is with:

```
$ tychus init
```

Double check your generated `.tychus.yml` config to make sure it knows which
file extensions to watch.

## Usage

Usage is simple, `tychus run` and then your command. On a filesystem change that
command will be rerun.

```
tychus run
// Go
tychus run go run main.go

// Rust
tychus run cargo run

// Ruby
tychus ruby myapp.rb

// Shell Commands
tychus run ls
```

Want to pass additional arguments?
Need to pass flags? Stick the command in quotes

```
tychus run bundle exec ruby myapp.rb
tychus run "ruby myapp.rb -e development"
```

Need to pass flags? The following are equivalent:
Complicated command? Stick it in quotes

```
# Yep, that will just run "ls -al" on any file change with the build step
# disabled. You can really run anything you want.
tychus run "ls -al"
tychus run ls --with=-al
tychus run ls -w -al
tychus run "go build -o my-bin && echo 'Built Binary' && ./my-bin"
```


## Configuration

```yaml
# Settings for the file watcher
watch:
# List of extentions to watch. A change to a file with one of these extensions
# will trigger a fresh of your application.
extensions:
- .go
# List of folders to not watch.
ignore:
- node_modules
- tmp
- log
- vendor

# Build settings.
build:
# Disable this if you don't have a compile step (Ruby, Python, etc.).
enabled: false
# Command to run to rebuild your binary. Tychus will automatically tack on a
# -o bin_name so it can be omitted.
build_command: go build -i
# Name of binary that gets built.
bin_name: tychus-bin
# Where to put your built binary.
target_path: tmp

# Proxy settings.
proxy:
# If not enabled, proxy will not start.
enabled: true
# Port your application runs on. NOTE: a PORT environment will take overwrite
# whatever you put here.
app_port: 3000
# Port to run the proxy on.
proxy_port: 4000
# In seconds, how long the proxy will attempt to proxy a request until it
# gives up and returns a 502.
timeout: 10
# List of extentions to watch. A change to a file with one of these extensions
# will trigger a fresh of your application.
extensions:
- .go

# List of folders to not watch. Too many watched files / folders can slow things
down, so try and ignore as much as possible.
ignore:
- node_modules
- tmp
- log
- vendor

# If not enabled, proxy will not start.
proxy_enabled: true

# Port proxy runs on.
proxy_port: 4000

# Port your application runs on. NOTE: a PORT environment will take overwrite
# whatever you put here.
app_port: 3000

# In seconds, how long the proxy will attempt to proxy a request until it
# gives up and returns a 502.
timeout: 10
```

### Sample Configurations + Instructions:

* [Go](https://github.com/devlocker/tychus/wiki/Example:-Go)
* [Ruby + Sinatra](https://github.com/devlocker/tychus/wiki/Example:-Ruby---Sinatra)
* [Rust](https://github.com/devlocker/tychus/wiki/Example:-Rust)
* [Rust with Cargo](https://github.com/devlocker/tychus/wiki/Example:-Rust-with-Cargo)

40 changes: 10 additions & 30 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,12 @@ func detectLangauge(dir string) (*tychus.Configuration, error) {
}

c := &tychus.Configuration{
Build: tychus.BuildConfig{
BuildCommand: "go build -i",
BinName: "tychus-bin",
Enabled: true,
TargetPath: "tmp/",
},
Watch: tychus.WatchConfig{
Extensions: []string{".go"},
Ignore: []string{"node_modules", "tmp", "log", "vendor"},
},
Proxy: tychus.ProxyConfig{
Enabled: true,
AppPort: 3000,
ProxyPort: 4000,
Timeout: 10,
},
Extensions: []string{".go"},
Ignore: []string{"node_modules", "tmp", "log", "vendor"},
ProxyEnabled: true,
ProxyPort: 4000,
AppPort: 3000,
Timeout: 10,
}

// Go Project?
Expand All @@ -103,10 +93,7 @@ func detectLangauge(dir string) (*tychus.Configuration, error) {
for _, f := range files {
ext := filepath.Ext(f.Name())
if f.Name() == "Gemfile" || ext == ".rb" {
c.Build.Enabled = false
c.Build.BuildCommand = ""
c.Build.BinName = ""
c.Watch.Extensions = []string{".rb"}
c.Extensions = []string{".rb"}
return c, nil
}
}
Expand All @@ -115,10 +102,7 @@ func detectLangauge(dir string) (*tychus.Configuration, error) {
for _, f := range files {
ext := filepath.Ext(f.Name())
if ext == ".py" {
c.Build.Enabled = false
c.Build.BuildCommand = ""
c.Build.BinName = ""
c.Watch.Extensions = []string{".py"}
c.Extensions = []string{".py"}
return c, nil
}
}
Expand All @@ -127,8 +111,7 @@ func detectLangauge(dir string) (*tychus.Configuration, error) {
for _, f := range files {
ext := filepath.Ext(f.Name())
if f.Name() == "Cargo.toml" || ext == ".rs" {
c.Build.BuildCommand = "rustc main.rs"
c.Watch.Extensions = []string{".rs"}
c.Extensions = []string{".rs"}
return c, nil
}
}
Expand All @@ -137,10 +120,7 @@ func detectLangauge(dir string) (*tychus.Configuration, error) {
for _, f := range files {
ext := filepath.Ext(f.Name())
if f.Name() == "package.json" || ext == ".js" {
c.Build.Enabled = false
c.Build.BuildCommand = ""
c.Build.BinName = ""
c.Watch.Extensions = []string{".js"}
c.Extensions = []string{".js"}
return c, nil
}
}
Expand Down
56 changes: 32 additions & 24 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,70 @@ package cmd

import (
"os"
"os/signal"
"strconv"
"strings"
"syscall"

"github.com/devlocker/tychus/tychus"
"github.com/fatih/color"
"github.com/spf13/cobra"
)

var with string

func init() {
rootCmd.AddCommand(runCmd)
runCmd.Flags().StringVarP(&with, "with", "w", "", "extra options to run with")
}

var runCmd = &cobra.Command{
Use: "run",
Short: "Reloads your application as you make changes to source files.",
Run: func(cmd *cobra.Command, args []string) {
if len(with) > 1 {
args = append(args, strings.Fields(with)...)
}

start(args)
},
}

func start(args []string) {
c := &tychus.Configuration{}
c.Logger = tychus.NewLogger(debug)
stop := make(chan os.Signal, 1)
signal.Notify(
stop,
os.Interrupt,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)

// Load configuration and use default logger
c := &tychus.Configuration{}
err := c.Load(configFile)
if err != nil {
c.Logger.Fatal(err.Error())
}

c.Logger.Printf(
"Starting: build [%v], proxy [%v]",
isEnabledStr(c.Build.Enabled),
isEnabledStr(c.Proxy.Enabled),
)
c.Logger = tychus.NewLogger(debug)

// If PORT is set, use that instead of AppPort. For things like foreman
// where ports are automatically assigned.
port := os.Getenv("PORT")
if len(port) > 0 {
appPort, err := strconv.Atoi(port)
if err == nil {
c.Proxy.AppPort = appPort
port, ok := os.LookupEnv("PORT")
if ok {
if appPort, err := strconv.Atoi(port); err == nil {
c.AppPort = appPort
}
}

err = tychus.Start(args, c)
if err != nil {
c.Logger.Fatal(err.Error())
}
o := tychus.New(args, c)

// Run tychus
go func() {
err = o.Start()
if err != nil {
o.Stop()
c.Logger.Fatal(err.Error())
}
}()

<-stop
// Have to call `Stop`
o.Stop()
}

func isEnabledStr(b bool) string {
Expand Down
Loading