2016-09-17 23:04:36 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2016-09-18 11:50:15 +00:00
|
|
|
"fmt"
|
2016-10-21 13:59:11 +00:00
|
|
|
w "github.com/tockins/realize/cli"
|
|
|
|
c "github.com/tockins/realize/config"
|
2016-09-17 23:04:36 +00:00
|
|
|
s "github.com/tockins/realize/server"
|
2016-09-18 11:50:15 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
"log"
|
|
|
|
"os"
|
2016-09-17 23:04:36 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2016-09-18 11:50:15 +00:00
|
|
|
const (
|
|
|
|
Name = "Realize"
|
|
|
|
Version = "1.1"
|
|
|
|
Description = "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths"
|
|
|
|
Limit = 10000
|
2016-10-21 13:59:11 +00:00
|
|
|
Config = "R.config.yaml"
|
|
|
|
Output = "R.output.log"
|
2016-10-17 09:58:22 +00:00
|
|
|
Host = "Web server listening on localhost:5000"
|
2016-09-18 11:50:15 +00:00
|
|
|
)
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
var R Realize
|
2016-09-17 23:04:36 +00:00
|
|
|
|
|
|
|
// Realize struct contains the general app informations
|
2016-10-21 13:59:11 +00:00
|
|
|
type Realize struct {
|
2016-10-21 14:14:39 +00:00
|
|
|
c.Utils
|
2016-10-17 09:58:22 +00:00
|
|
|
Name, Description, Author, Email, Host string
|
|
|
|
Version string
|
|
|
|
Limit uint64
|
2016-10-21 13:59:11 +00:00
|
|
|
Blueprint w.Blueprint
|
2016-10-17 09:58:22 +00:00
|
|
|
Server s.Server
|
|
|
|
Files map[string]string
|
|
|
|
Sync chan string
|
2016-09-17 23:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Application initialization
|
2016-09-18 11:50:15 +00:00
|
|
|
func init() {
|
2016-10-21 13:59:11 +00:00
|
|
|
R = Realize{
|
2016-09-18 11:50:15 +00:00
|
|
|
Name: Name,
|
|
|
|
Version: Version,
|
|
|
|
Description: Description,
|
2016-10-17 09:58:22 +00:00
|
|
|
Host: Host,
|
2016-09-18 11:50:15 +00:00
|
|
|
Limit: Limit,
|
|
|
|
Files: map[string]string{
|
|
|
|
"config": Config,
|
|
|
|
"output": Output,
|
2016-09-17 23:04:36 +00:00
|
|
|
},
|
2016-09-18 13:35:44 +00:00
|
|
|
Sync: make(chan string),
|
|
|
|
}
|
2016-10-21 13:59:11 +00:00
|
|
|
R.Blueprint = w.Blueprint{
|
2016-10-21 14:14:39 +00:00
|
|
|
Utils: R.Utils,
|
2016-10-21 13:59:11 +00:00
|
|
|
Files: R.Files,
|
|
|
|
Sync: R.Sync,
|
2016-09-17 23:04:36 +00:00
|
|
|
}
|
2016-10-21 13:59:11 +00:00
|
|
|
R.Server = s.Server{
|
|
|
|
Blueprint: &R.Blueprint,
|
|
|
|
Files: R.Files,
|
|
|
|
Sync: R.Sync,
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
2016-10-21 13:59:11 +00:00
|
|
|
R.limit()
|
2016-09-17 23:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flimit defines the max number of watched files
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) limit() {
|
2016-09-17 23:04:36 +00:00
|
|
|
var rLimit syscall.Rlimit
|
2016-10-21 13:59:11 +00:00
|
|
|
rLimit.Max = R.Limit
|
|
|
|
rLimit.Cur = R.Limit
|
2016-09-17 23:04:36 +00:00
|
|
|
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err != nil {
|
2016-10-21 13:59:11 +00:00
|
|
|
log.Fatal(w.Red("Error Setting Rlimit "), err)
|
2016-09-17 23:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-18 11:50:15 +00:00
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Red(s string) string {
|
|
|
|
return w.Red(s)
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Blue(s string) string {
|
|
|
|
return w.Blue(s)
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) BlueS(s string) string {
|
|
|
|
return w.BlueS(s)
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Dir() string {
|
2016-10-21 14:14:39 +00:00
|
|
|
return r.Wdir()
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Serve(p *cli.Context) {
|
2016-09-18 11:50:15 +00:00
|
|
|
if !p.Bool("no-server") {
|
2016-10-21 13:59:11 +00:00
|
|
|
fmt.Println(R.Red(R.Host) + "\n")
|
|
|
|
R.Server.Open = p.Bool("open")
|
|
|
|
R.Server.Start()
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Run(p *cli.Context) error {
|
|
|
|
R.Serve(p)
|
|
|
|
return R.Blueprint.Run()
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Fast(p *cli.Context) error {
|
|
|
|
R.Blueprint.Add(p)
|
|
|
|
R.Serve(p)
|
|
|
|
return R.Blueprint.Fast(p)
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Add(p *cli.Context) error {
|
|
|
|
return R.Blueprint.Insert(p)
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Remove(p *cli.Context) error {
|
|
|
|
return R.Blueprint.Insert(p)
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) List(p *cli.Context) error {
|
|
|
|
return R.Blueprint.List()
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Before(p *cli.Context) error {
|
|
|
|
fmt.Println(R.Blue(R.Name) + " - " + R.Blue(R.Version))
|
|
|
|
fmt.Println(R.BlueS(R.Description) + "\n")
|
2016-09-18 11:50:15 +00:00
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
if gopath == "" {
|
2016-10-21 13:59:11 +00:00
|
|
|
log.Fatal(R.Red("$GOPATH isn't set up properly"))
|
2016-09-18 11:50:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:59:11 +00:00
|
|
|
func (r *Realize) Handle(err error) error {
|
2016-09-18 11:50:15 +00:00
|
|
|
if err != nil {
|
2016-10-21 13:59:11 +00:00
|
|
|
fmt.Println(R.Red(err.Error()))
|
2016-09-18 11:50:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|