shared projects between server/cli

This commit is contained in:
alessio 2016-09-18 01:04:36 +02:00
parent 608a1a64d0
commit a4b0bcaeca
7 changed files with 87 additions and 77 deletions

51
app/main.go Normal file
View File

@ -0,0 +1,51 @@
package app
import (
c "github.com/tockins/realize/cli"
s "github.com/tockins/realize/server"
"syscall"
"fmt"
)
var R Realize
// Realize struct contains the general app informations
type Realize struct {
Name, Description, Author, Email string
Version string
Limit uint64
Blueprint c.Blueprint
Server s.Server
}
// Application initialization
func init(){
R = Realize{
Name: "Realize",
Version: "1.0",
Description: "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths",
Limit: 10000,
Blueprint: c.Blueprint{
Files: map[string]string{
"config": "r.config.yaml",
"output": "r.output.log",
},
},
Server: s.Server{
Blueprint: &R.Blueprint,
},
}
R.Increases()
}
// Flimit defines the max number of watched files
func (r *Realize) Increases() {
// 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)
}
}

View File

@ -15,8 +15,8 @@ func (h *Blueprint) Run() error {
if err == nil { if err == nil {
// loop projects // loop projects
wg.Add(len(h.Projects)) wg.Add(len(h.Projects))
for k := range h.Projects { for _,v := range h.Projects {
go h.Projects[k].watching() go v.watching()
} }
wg.Wait() wg.Wait()
return nil return nil
@ -26,19 +26,23 @@ func (h *Blueprint) Run() error {
// Fast method run a project from his working directory without makes a config file // Fast method run a project from his working directory without makes a config file
func (h *Blueprint) Fast(params *cli.Context) error { func (h *Blueprint) Fast(params *cli.Context) error {
fast := h.Projects[0]
// Takes the values from config if wd path match with someone else // Takes the values from config if wd path match with someone else
if params.Bool("config") { wg.Add(1)
if err := h.Read(); err == nil { for i := 0; i < len(h.Projects); i++ {
for _, val := range h.Projects { v := &h.Projects[i]
if fast.Path == val.Path { if params.Bool("config") {
fast = val if err := h.Read(); err == nil {
for l := 0; l < len(h.Projects); l++ {
l := &h.Projects[l]
if v.Path == l.Path {
v = l
}
} }
} }
} }
go v.watching()
} }
wg.Add(1)
go fast.watching()
wg.Wait() wg.Wait()
return nil return nil
} }

View File

@ -1,18 +1,18 @@
package cli package cli
import ( import (
"github.com/fatih/color"
"log" "log"
"sync" "sync"
"time" "time"
"github.com/fatih/color"
) )
var Bp *Blueprint var B *Blueprint
var wg sync.WaitGroup var wg sync.WaitGroup
// Green, Red Bold, Red, Blue, Blue Bold, Yellow, Yellow Bold, Magenta, Magenta Bold colors var Green, Red, RedS, Blue, BlueS, Yellow, YellowS, Magenta, MagentaS =
var Green, Red, RedS, Blue, BlueS, Yellow, YellowS, Magenta, MagentaS = color.New(color.FgGreen, color.Bold).SprintFunc(), color.New(color.FgGreen, color.Bold).SprintFunc(),
color.New(color.FgRed, color.Bold).SprintFunc(), color.New(color.FgRed, color.Bold).SprintFunc(),
color.New(color.FgRed).SprintFunc(), color.New(color.FgRed).SprintFunc(),
color.New(color.FgBlue, color.Bold).SprintFunc(), color.New(color.FgBlue, color.Bold).SprintFunc(),

View File

@ -12,8 +12,8 @@ import (
"time" "time"
) )
// Wdir returns the name last element of the working directory path // WorkingDir returns the name last element of the working directory path
func Wdir() string { func WorkingDir() string {
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { if err != nil {
log.Fatal(Red(err)) log.Fatal(Red(err))
@ -32,7 +32,6 @@ func read(file string) ([]byte, error) {
return content, err return content, err
} }
return nil, err return nil, err
} }
// Write a file given a name and a byte stream // Write a file given a name and a byte stream
@ -71,7 +70,7 @@ func argsParam(params *cli.Context) []string {
func nameFlag(params *cli.Context) string { func nameFlag(params *cli.Context) string {
var name string var name string
if params.String("name") == "" && params.String("path") == "" { if params.String("name") == "" && params.String("path") == "" {
return Wdir() return WorkingDir()
} else if params.String("path") != "/" { } else if params.String("path") != "/" {
name = filepath.Base(params.String("path")) name = filepath.Base(params.String("path"))
} else { } else {

View File

@ -218,7 +218,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error {
if p.Path == "." || p.Path == "/" { if p.Path == "." || p.Path == "/" {
p.base = wd p.base = wd
p.Path = Wdir() p.Path = WorkingDir()
} else if filepath.IsAbs(p.Path) { } else if filepath.IsAbs(p.Path) {
p.base = p.Path p.base = p.Path
} else { } else {

View File

@ -1,59 +1,18 @@
package main package main
import ( import (
"fmt" a "github.com/tockins/realize/app"
c "github.com/tockins/realize/cli" c "github.com/tockins/realize/cli"
s "github.com/tockins/realize/server" "fmt"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"log" "log"
"os" "os"
"syscall"
) )
var App Realize var App *a.Realize
// Realize struct contains the general app informations
type Realize struct {
Name, Description, Author, Email string
Version string
Limit uint64
Blueprint c.Blueprint
Server s.Server
}
// Flimit defines the max number of watched files
func (r *Realize) Increases() {
// 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)
}
}
func init() {
App = Realize{
Name: "Realize",
Version: "1.0",
Description: "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths",
Limit: 10000,
Blueprint: c.Blueprint{
Files: map[string]string{
"config": "r.config.yaml",
"output": "r.output.log",
},
},
}
App.Increases()
c.Bp = &App.Blueprint
s.Bp = &App.Blueprint
}
func main() { func main() {
App = &a.R
handle := func(err error) error { handle := func(err error) error {
if err != nil { if err != nil {
fmt.Println(c.Red(err.Error())) fmt.Println(c.Red(err.Error()))
@ -61,7 +20,6 @@ func main() {
} }
return nil return nil
} }
header := func() error { header := func() error {
fmt.Println(c.Blue(App.Name) + " - " + c.Blue(App.Version)) fmt.Println(c.Blue(App.Name) + " - " + c.Blue(App.Version))
fmt.Println(c.BlueS(App.Description) + "\n") fmt.Println(c.BlueS(App.Description) + "\n")
@ -71,8 +29,7 @@ func main() {
} }
return nil return nil
} }
c := &cli.App{
cli := &cli.App{
Name: App.Name, Name: App.Name,
Version: App.Version, Version: App.Version,
Authors: []*cli.Author{ Authors: []*cli.Author{
@ -126,7 +83,7 @@ func main() {
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "Add another project", Usage: "Add another project",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: c.Wdir(), Usage: "Project name"}, &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: c.WorkingDir(), Usage: "Project name"},
&cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "/", Usage: "Project base path"}, &cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "/", Usage: "Project base path"},
&cli.BoolFlag{Name: "build", Value: false, Usage: "Enable the build"}, &cli.BoolFlag{Name: "build", Value: false, Usage: "Enable the build"},
&cli.BoolFlag{Name: "no-run", Usage: "Disables the run"}, &cli.BoolFlag{Name: "no-run", Usage: "Disables the run"},
@ -173,6 +130,5 @@ func main() {
}, },
}, },
} }
c.Run(os.Args)
cli.Run(os.Args)
} }

View File

@ -1,21 +1,20 @@
package server package server
import ( import (
c "github.com/tockins/realize/cli"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/engine/standard" "github.com/labstack/echo/engine/standard"
"github.com/labstack/echo/middleware" "github.com/labstack/echo/middleware"
c "github.com/tockins/realize/cli"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
"log" "log"
"net/http" "net/http"
) )
var Bp *c.Blueprint
// Server struct contains server informations // Server struct contains server informations
type Server struct { type Server struct{
Blueprint *c.Blueprint
} }
func render(c echo.Context, path string) error { func render(c echo.Context, path string) error {
@ -35,7 +34,8 @@ func (s *Server) Start() {
e := echo.New() e := echo.New()
e.Use(middleware.Gzip()) e.Use(middleware.Gzip())
e.GET("/", func(c echo.Context) error { e.GET("/", func(c echo.Context) error {
return render(c, "server/assets/index.html") return c.JSON(200, s.Blueprint)
//return render(c, "server/assets/index.html")
}) })
e.GET("/projects", standard.WrapHandler(projects())) e.GET("/projects", standard.WrapHandler(projects()))
@ -46,9 +46,9 @@ func (s *Server) Start() {
func projects() websocket.Handler { func projects() websocket.Handler {
return websocket.Handler(func(ws *websocket.Conn) { return websocket.Handler(func(ws *websocket.Conn) {
for { for {
message, _ := json.Marshal(Bp) message, _ := json.Marshal("")
err := websocket.Message.Send(ws, string(message)) err := websocket.Message.Send(ws, string(message))
fmt.Println(Bp) fmt.Println("")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }