code cleaned, comments fixed

This commit is contained in:
alessio 2016-12-17 11:43:27 +01:00
parent 33081bb309
commit 9fd616f552
9 changed files with 48 additions and 22 deletions

View File

@ -12,13 +12,14 @@ import (
"strconv" "strconv"
) )
// Server struct contains server informations // Server settings
type Server struct { type Server struct {
*c.Settings `yaml:"-"` *c.Settings `yaml:"-"`
*w.Blueprint `yaml:"-"` *w.Blueprint `yaml:"-"`
Sync chan string `yaml:"-"` Sync chan string `yaml:"-"`
} }
// Render return a web pages defined in bindata
func render(c echo.Context, path string, mime int) error { func render(c echo.Context, path string, mime int) error {
data, err := Asset(path) data, err := Asset(path)
if err != nil { if err != nil {
@ -45,7 +46,7 @@ func render(c echo.Context, path string, mime int) error {
return nil return nil
} }
// Server starting // Start the web server
func (s *Server) Start(p *cli.Context) (err error) { func (s *Server) Start(p *cli.Context) (err error) {
if !p.Bool("no-server") && s.Enabled { if !p.Bool("no-server") && s.Enabled {
e := echo.New() e := echo.New()
@ -82,8 +83,7 @@ func (s *Server) Start(p *cli.Context) (err error) {
}) })
//websocket //websocket
//e.GET("/ws", echo.WrapHandler(s.projects())) e.GET("/ws", s.projects)
e.GET("/ws", s.hello)
go e.Start(string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port)) go e.Start(string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port))
if s.Open || p.Bool("open") { if s.Open || p.Bool("open") {
@ -98,7 +98,7 @@ func (s *Server) Start(p *cli.Context) (err error) {
return nil return nil
} }
func (s *Server) hello(c echo.Context) error { func (s *Server) projects(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) { websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close() defer ws.Close()
msg, _ := json.Marshal(s.Blueprint.Projects) msg, _ := json.Marshal(s.Blueprint.Projects)
@ -109,7 +109,6 @@ func (s *Server) hello(c echo.Context) error {
msg, _ := json.Marshal(s.Blueprint.Projects) msg, _ := json.Marshal(s.Blueprint.Projects)
err = websocket.Message.Send(ws, string(msg)) err = websocket.Message.Send(ws, string(msg))
if err != nil { if err != nil {
//log.Println(err)
break break
} }
} }
@ -123,7 +122,6 @@ func (s *Server) hello(c echo.Context) error {
} else { } else {
err := json.Unmarshal([]byte(text), &s.Blueprint.Projects) err := json.Unmarshal([]byte(text), &s.Blueprint.Projects)
if err != nil { if err != nil {
//log.Println(err)
break break
} }
} }

View File

@ -11,6 +11,7 @@ import (
var cmd map[string]string var cmd map[string]string
var stderr bytes.Buffer var stderr bytes.Buffer
// Init an associative array with the os supported
func init() { func init() {
cmd = map[string]string{ cmd = map[string]string{
"windows": "start", "windows": "start",
@ -19,6 +20,7 @@ func init() {
} }
} }
// Open a url in the default browser
func Open(url string) (io.Writer, error) { func Open(url string) (io.Writer, error) {
if open, err := cmd[runtime.GOOS]; !err { if open, err := cmd[runtime.GOOS]; !err {
return nil, errors.New("This operating system is not supported.") return nil, errors.New("This operating system is not supported.")

View File

@ -4,6 +4,7 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
) )
// Colors allowed
type Colors struct { type Colors struct {
Red Red
Blue Blue
@ -11,57 +12,72 @@ type Colors struct {
Magenta Magenta
Green Green
} }
// Red color
type Red struct{} type Red struct{}
// Blue color
type Blue struct{} type Blue struct{}
// Yellow color
type Yellow struct{} type Yellow struct{}
// Magenta color
type Magenta struct{} type Magenta struct{}
// Green color
type Green struct{} type Green struct{}
// Regular font in red
func (c Red) Regular(t ...interface{}) string { func (c Red) Regular(t ...interface{}) string {
r := color.New(color.FgRed).SprintFunc() r := color.New(color.FgRed).SprintFunc()
return r(t...) return r(t...)
} }
// Bold font in red
func (c Red) Bold(t ...interface{}) string { func (c Red) Bold(t ...interface{}) string {
r := color.New(color.FgRed, color.Bold).SprintFunc() r := color.New(color.FgRed, color.Bold).SprintFunc()
return r(t...) return r(t...)
} }
// Regular font in blue
func (c Blue) Regular(t ...interface{}) string { func (c Blue) Regular(t ...interface{}) string {
r := color.New(color.FgBlue).SprintFunc() r := color.New(color.FgBlue).SprintFunc()
return r(t...) return r(t...)
} }
// Bold font in blue
func (c Blue) Bold(t ...interface{}) string { func (c Blue) Bold(t ...interface{}) string {
r := color.New(color.FgBlue, color.Bold).SprintFunc() r := color.New(color.FgBlue, color.Bold).SprintFunc()
return r(t...) return r(t...)
} }
// Regular font in yellow
func (c Yellow) Regular(t ...interface{}) string { func (c Yellow) Regular(t ...interface{}) string {
r := color.New(color.FgYellow).SprintFunc() r := color.New(color.FgYellow).SprintFunc()
return r(t...) return r(t...)
} }
// Bold font in red
func (c Yellow) Bold(t ...interface{}) string { func (c Yellow) Bold(t ...interface{}) string {
r := color.New(color.FgYellow, color.Bold).SprintFunc() r := color.New(color.FgYellow, color.Bold).SprintFunc()
return r(t...) return r(t...)
} }
// Regular font in magenta
func (c Magenta) Regular(t ...interface{}) string { func (c Magenta) Regular(t ...interface{}) string {
r := color.New(color.FgMagenta).SprintFunc() r := color.New(color.FgMagenta).SprintFunc()
return r(t...) return r(t...)
} }
// Bold font in magenta
func (c Magenta) Bold(t ...interface{}) string { func (c Magenta) Bold(t ...interface{}) string {
r := color.New(color.FgMagenta, color.Bold).SprintFunc() r := color.New(color.FgMagenta, color.Bold).SprintFunc()
return r(t...) return r(t...)
} }
// Regular font in green
func (c Green) Regular(t ...interface{}) string { func (c Green) Regular(t ...interface{}) string {
r := color.New(color.FgGreen).SprintFunc() r := color.New(color.FgGreen).SprintFunc()
return r(t...) return r(t...)
} }
// Bold font in red
func (c Green) Bold(t ...interface{}) string { func (c Green) Bold(t ...interface{}) string {
r := color.New(color.FgGreen, color.Bold).SprintFunc() r := color.New(color.FgGreen, color.Bold).SprintFunc()
return r(t...) return r(t...)

View File

@ -6,7 +6,7 @@ import (
"path/filepath" "path/filepath"
) )
// Scan return a byte stream of a given file // Stream return a byte stream of a given file
func (s Settings) Stream(file string) ([]byte, error) { func (s Settings) Stream(file string) ([]byte, error) {
_, err := os.Stat(file) _, err := os.Stat(file)
if err == nil { if err == nil {

View File

@ -1,10 +1,11 @@
package settings package settings
import ( import (
"gopkg.in/yaml.v2"
"os" "os"
"gopkg.in/yaml.v2"
) )
// Settings defines a group of general settings
type Settings struct { type Settings struct {
Colors `yaml:"-"` Colors `yaml:"-"`
Resources `yaml:"resources" json:"resources"` Resources `yaml:"resources" json:"resources"`
@ -12,10 +13,12 @@ type Settings struct {
Config `yaml:"config" json:"config"` Config `yaml:"config" json:"config"`
} }
// Config defines structural options
type Config struct { type Config struct {
Flimit uint64 `yaml:"flimit" json:"flimit"` Flimit uint64 `yaml:"flimit" json:"flimit"`
} }
// Server settings, used for the web panel
type Server struct { type Server struct {
Enabled bool `yaml:"enable" json:"enable"` Enabled bool `yaml:"enable" json:"enable"`
Open bool `yaml:"open" json:"open"` Open bool `yaml:"open" json:"open"`
@ -23,6 +26,7 @@ type Server struct {
Port int `yaml:"port" json:"port"` Port int `yaml:"port" json:"port"`
} }
// Resources defines the files generated by realize
type Resources struct { type Resources struct {
Config string `yaml:"-" json:"-"` Config string `yaml:"-" json:"-"`
Output string `yaml:"output" json:"output"` Output string `yaml:"output" json:"output"`

View File

@ -6,12 +6,14 @@ import (
"path/filepath" "path/filepath"
) )
// Wdir return the current working directory
func (s Settings) Wdir() string { func (s Settings) Wdir() string {
dir, err := os.Getwd() dir, err := os.Getwd()
s.Validate(err) s.Validate(err)
return filepath.Base(dir) return filepath.Base(dir)
} }
// Validate checks a fatal error
func (s Settings) Validate(err error) error { func (s Settings) Validate(err error) error {
if err != nil { if err != nil {
s.Fatal(err, "") s.Fatal(err, "")
@ -19,6 +21,7 @@ func (s Settings) Validate(err error) error {
return nil return nil
} }
// Fatal prints a fatal error with its additional messages
func (s Settings) Fatal(err error, msg ...interface{}) { func (s Settings) Fatal(err error, msg ...interface{}) {
if len(msg) > 0 { if len(msg) > 0 {
log.Fatalln(s.Red.Regular(msg...), err.Error()) log.Fatalln(s.Red.Regular(msg...), err.Error())

View File

@ -8,7 +8,7 @@ import (
"strings" "strings"
) )
// Watch method adds the given paths on the Watcher // Run launches the toolchain for each project
func (h *Blueprint) Run() error { func (h *Blueprint) Run() error {
err := h.check() err := h.check()
if err == nil { if err == nil {
@ -68,7 +68,7 @@ func (h *Blueprint) Clean() {
} }
} }
// Inserts a new project in the list // Insert a new project in projects list
func (h *Blueprint) Insert(p *cli.Context) error { func (h *Blueprint) Insert(p *cli.Context) error {
err := h.Add(p) err := h.Add(p)
return err return err
@ -134,9 +134,8 @@ func (h *Blueprint) check() error {
if len(h.Projects) > 0 { if len(h.Projects) > 0 {
h.Clean() h.Clean()
return nil return nil
} else {
return errors.New("There are no projects. The config file is empty.")
} }
return errors.New("There are no projects. The config file is empty.")
} }
// NameParam check the project name presence. If empty takes the working directory name // NameParam check the project name presence. If empty takes the working directory name

View File

@ -14,7 +14,7 @@ type logWriter struct {
c.Colors c.Colors
} }
// Projects struct contains a projects list // Blueprint struct contains a projects list
type Blueprint struct { type Blueprint struct {
*c.Settings `yaml:"-"` *c.Settings `yaml:"-"`
Projects []Project `yaml:"projects,omitempty" json:"projects,omitempty"` Projects []Project `yaml:"projects,omitempty" json:"projects,omitempty"`
@ -28,12 +28,12 @@ type Project struct {
base string base string
Name string `yaml:"name" json:"name"` Name string `yaml:"name" json:"name"`
Path string `yaml:"path" json:"path"` Path string `yaml:"path" json:"path"`
Run bool `yaml:"run" json:"run"`
Bin bool `yaml:"bin" json:"bin"`
Generate bool `yaml:"generate" json:"generate"`
Build bool `yaml:"build" json:"build"`
Fmt bool `yaml:"fmt" json:"fmt"` Fmt bool `yaml:"fmt" json:"fmt"`
Test bool `yaml:"test" json:"test"` Test bool `yaml:"test" json:"test"`
Generate bool `yaml:"generate" json:"generate"`
Bin bool `yaml:"bin" json:"bin"`
Build bool `yaml:"build" json:"build"`
Run bool `yaml:"run" json:"run"`
Params []string `yaml:"params" json:"params"` Params []string `yaml:"params" json:"params"`
Watcher Watcher `yaml:"watcher" json:"watcher"` Watcher Watcher `yaml:"watcher" json:"watcher"`
Cli Cli `yaml:"cli" json:"cli"` Cli Cli `yaml:"cli" json:"cli"`
@ -54,23 +54,26 @@ type Watcher struct {
Preview bool `yaml:"preview" json:"preview"` Preview bool `yaml:"preview" json:"preview"`
} }
// Cli output status, enables or disables
type Cli struct { type Cli struct {
Streams bool `yaml:"streams" json:"streams"` Streams bool `yaml:"streams" json:"streams"`
} }
// File determinates the status of each log files (streams, logs, errors)
type File struct { type File struct {
Streams bool `yaml:"streams" json:"streams"` Streams bool `yaml:"streams" json:"streams"`
Logs bool `yaml:"logs" json:"logs"` Logs bool `yaml:"logs" json:"logs"`
Errors bool `yaml:"errors" json:"errors"` Errors bool `yaml:"errors" json:"errors"`
} }
// Buffer struct for buffering outputs // Buffer define an array buffer for each log files
type Buffer struct { type Buffer struct {
StdOut []BufferOut `json:"stdOut"` StdOut []BufferOut `json:"stdOut"`
StdLog []BufferOut `json:"stdLog"` StdLog []BufferOut `json:"stdLog"`
StdErr []BufferOut `json:"stdErr"` StdErr []BufferOut `json:"stdErr"`
} }
// BufferOut is used for exchange information between "realize cli" and "web realize"
type BufferOut struct { type BufferOut struct {
Time time.Time `json:"time"` Time time.Time `json:"time"`
Text string `json:"text"` Text string `json:"text"`

View File

@ -82,7 +82,7 @@ func (p *Project) watching() {
} }
} }
// Install calls an implementation of the "go install" // Install calls an implementation of "go install"
func (p *Project) install() { func (p *Project) install() {
if p.Bin { if p.Bin {
start := time.Now() start := time.Now()
@ -101,6 +101,7 @@ func (p *Project) install() {
return return
} }
// Install calls an implementation of "go run"
func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { func (p *Project) run(channel chan bool, wr *sync.WaitGroup) {
if p.Run { if p.Run {
start := time.Now() start := time.Now()
@ -265,7 +266,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error {
return nil return nil
} }
// Ignore validates a path // Ignore and validate a path
func (p *Project) ignore(str string) bool { func (p *Project) ignore(str string) bool {
for _, v := range p.Watcher.Ignore { for _, v := range p.Watcher.Ignore {
if strings.Contains(str, filepath.Join(p.base, v)) { if strings.Contains(str, filepath.Join(p.base, v)) {
@ -275,7 +276,7 @@ func (p *Project) ignore(str string) bool {
return false return false
} }
// Routines launches the following methods: run, build, install // Routines launches the toolchain run, build, install
func (p *Project) routines(channel chan bool, wr *sync.WaitGroup) { func (p *Project) routines(channel chan bool, wr *sync.WaitGroup) {
p.install() p.install()
p.build() p.build()