realize/app/main.go

146 lines
3.0 KiB
Go
Raw Normal View History

2016-09-17 23:04:36 +00:00
package app
import (
2016-09-18 11:50:15 +00:00
"fmt"
2016-09-17 23:04:36 +00:00
c "github.com/tockins/realize/cli"
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
Config = "r.config.yaml"
Output = "r.output.log"
)
var r realize
var R Realizer
// Realizer interface for wrap the cli, app and server functions
type Realizer interface {
Wdir() string
Before() error
Red(string) string
Blue(string) string
BlueS(string) string
Handle(error) error
Serve(*cli.Context)
Fast(*cli.Context) error
Run(p *cli.Context) error
Add(p *cli.Context) error
Remove(p *cli.Context) error
List(p *cli.Context) error
}
2016-09-17 23:04:36 +00:00
// Realize struct contains the general app informations
2016-09-18 11:50:15 +00:00
type realize struct {
2016-09-17 23:04:36 +00:00
Name, Description, Author, Email string
Version string
Limit uint64
Blueprint c.Blueprint
2016-09-18 11:50:15 +00:00
Server s.Server
Files map[string]string
2016-09-17 23:04:36 +00:00
}
// Application initialization
2016-09-18 11:50:15 +00:00
func init() {
r = realize{
Name: Name,
Version: Version,
Description: Description,
Limit: Limit,
Files: map[string]string{
"config": Config,
"output": Output,
2016-09-17 23:04:36 +00:00
},
}
2016-09-18 11:50:15 +00:00
r.Blueprint = c.Blueprint{Files: r.Files}
r.Server = s.Server{
Blueprint: &r.Blueprint,
Files: r.Files,
}
r.Increase()
R = &r
2016-09-17 23:04:36 +00:00
}
// Flimit defines the max number of watched files
2016-09-18 11:50:15 +00:00
func (r *realize) Increase() {
2016-09-17 23:04:36 +00:00
// increases the files limit
var rLimit syscall.Rlimit
rLimit.Max = r.Limit
rLimit.Cur = r.Limit
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
fmt.Println(c.Red("Error Setting Rlimit "), err)
}
}
2016-09-18 11:50:15 +00:00
func (r *realize) Red(s string) string {
return c.Red(s)
}
func (r *realize) Blue(s string) string {
return c.Blue(s)
}
func (r *realize) BlueS(s string) string {
return c.BlueS(s)
}
func (r *realize) Wdir() string {
return c.WorkingDir()
}
func (r *realize) Serve(p *cli.Context) {
if !p.Bool("no-server") {
r.Server.Start()
}
}
func (r *realize) Run(p *cli.Context) error {
r.Serve(p)
return r.Blueprint.Run()
}
func (r *realize) Fast(p *cli.Context) error {
r.Blueprint.Add(p)
r.Serve(p)
return r.Blueprint.Fast(p)
}
func (r *realize) Add(p *cli.Context) error {
return r.Blueprint.Insert(p)
}
func (r *realize) Remove(p *cli.Context) error {
return r.Blueprint.Insert(p)
}
func (r *realize) List(p *cli.Context) error {
return r.Blueprint.List()
}
func (r *realize) Before() error {
fmt.Println(r.Blue(r.Name) + " - " + r.Blue(r.Version))
fmt.Println(r.BlueS(r.Description) + "\n")
gopath := os.Getenv("GOPATH")
if gopath == "" {
log.Fatal(r.Red("$GOPATH isn't set up properly"))
}
return nil
}
func (r *realize) Handle(err error) error {
if err != nil {
fmt.Println(r.Red(err.Error()))
return nil
}
return nil
}