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 {
// loop projects
wg.Add(len(h.Projects))
for k := range h.Projects {
go h.Projects[k].watching()
for _,v := range h.Projects {
go v.watching()
}
wg.Wait()
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
func (h *Blueprint) Fast(params *cli.Context) error {
fast := h.Projects[0]
// Takes the values from config if wd path match with someone else
if params.Bool("config") {
if err := h.Read(); err == nil {
for _, val := range h.Projects {
if fast.Path == val.Path {
fast = val
wg.Add(1)
for i := 0; i < len(h.Projects); i++ {
v := &h.Projects[i]
if params.Bool("config") {
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()
return nil
}

View File

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

View File

@ -12,8 +12,8 @@ import (
"time"
)
// Wdir returns the name last element of the working directory path
func Wdir() string {
// WorkingDir returns the name last element of the working directory path
func WorkingDir() string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(Red(err))
@ -32,7 +32,6 @@ func read(file string) ([]byte, error) {
return content, err
}
return nil, err
}
// 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 {
var name string
if params.String("name") == "" && params.String("path") == "" {
return Wdir()
return WorkingDir()
} else if params.String("path") != "/" {
name = filepath.Base(params.String("path"))
} else {

View File

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

View File

@ -1,59 +1,18 @@
package main
import (
"fmt"
a "github.com/tockins/realize/app"
c "github.com/tockins/realize/cli"
s "github.com/tockins/realize/server"
"fmt"
"gopkg.in/urfave/cli.v2"
"log"
"os"
"syscall"
)
var App 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
}
var App *a.Realize
func main() {
App = &a.R
handle := func(err error) error {
if err != nil {
fmt.Println(c.Red(err.Error()))
@ -61,7 +20,6 @@ func main() {
}
return nil
}
header := func() error {
fmt.Println(c.Blue(App.Name) + " - " + c.Blue(App.Version))
fmt.Println(c.BlueS(App.Description) + "\n")
@ -71,8 +29,7 @@ func main() {
}
return nil
}
cli := &cli.App{
c := &cli.App{
Name: App.Name,
Version: App.Version,
Authors: []*cli.Author{
@ -126,7 +83,7 @@ func main() {
Aliases: []string{"a"},
Usage: "Add another project",
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.BoolFlag{Name: "build", Value: false, Usage: "Enable the build"},
&cli.BoolFlag{Name: "no-run", Usage: "Disables the run"},
@ -173,6 +130,5 @@ func main() {
},
},
}
cli.Run(os.Args)
c.Run(os.Args)
}

View File

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