From 9fd616f55233e03cf21f4cb78d1bc964e8b43e89 Mon Sep 17 00:00:00 2001 From: alessio Date: Sat, 17 Dec 2016 11:43:27 +0100 Subject: [PATCH] code cleaned, comments fixed --- server/main.go | 12 +++++------- server/open.go | 2 ++ settings/colors.go | 16 ++++++++++++++++ settings/io.go | 2 +- settings/settings.go | 6 +++++- settings/utils.go | 3 +++ watcher/cmd.go | 7 +++---- watcher/main.go | 15 +++++++++------ watcher/watcher.go | 7 ++++--- 9 files changed, 48 insertions(+), 22 deletions(-) diff --git a/server/main.go b/server/main.go index b0c31b7..55867b1 100644 --- a/server/main.go +++ b/server/main.go @@ -12,13 +12,14 @@ import ( "strconv" ) -// Server struct contains server informations +// Server settings type Server struct { *c.Settings `yaml:"-"` *w.Blueprint `yaml:"-"` Sync chan string `yaml:"-"` } +// Render return a web pages defined in bindata func render(c echo.Context, path string, mime int) error { data, err := Asset(path) if err != nil { @@ -45,7 +46,7 @@ func render(c echo.Context, path string, mime int) error { return nil } -// Server starting +// Start the web server func (s *Server) Start(p *cli.Context) (err error) { if !p.Bool("no-server") && s.Enabled { e := echo.New() @@ -82,8 +83,7 @@ func (s *Server) Start(p *cli.Context) (err error) { }) //websocket - //e.GET("/ws", echo.WrapHandler(s.projects())) - e.GET("/ws", s.hello) + e.GET("/ws", s.projects) go e.Start(string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port)) if s.Open || p.Bool("open") { @@ -98,7 +98,7 @@ func (s *Server) Start(p *cli.Context) (err error) { return nil } -func (s *Server) hello(c echo.Context) error { +func (s *Server) projects(c echo.Context) error { websocket.Handler(func(ws *websocket.Conn) { defer ws.Close() msg, _ := json.Marshal(s.Blueprint.Projects) @@ -109,7 +109,6 @@ func (s *Server) hello(c echo.Context) error { msg, _ := json.Marshal(s.Blueprint.Projects) err = websocket.Message.Send(ws, string(msg)) if err != nil { - //log.Println(err) break } } @@ -123,7 +122,6 @@ func (s *Server) hello(c echo.Context) error { } else { err := json.Unmarshal([]byte(text), &s.Blueprint.Projects) if err != nil { - //log.Println(err) break } } diff --git a/server/open.go b/server/open.go index 9d1f48d..2bdc697 100644 --- a/server/open.go +++ b/server/open.go @@ -11,6 +11,7 @@ import ( var cmd map[string]string var stderr bytes.Buffer +// Init an associative array with the os supported func init() { cmd = map[string]string{ "windows": "start", @@ -19,6 +20,7 @@ func init() { } } +// Open a url in the default browser func Open(url string) (io.Writer, error) { if open, err := cmd[runtime.GOOS]; !err { return nil, errors.New("This operating system is not supported.") diff --git a/settings/colors.go b/settings/colors.go index 8f65462..e9d12e0 100644 --- a/settings/colors.go +++ b/settings/colors.go @@ -4,6 +4,7 @@ import ( "github.com/fatih/color" ) +// Colors allowed type Colors struct { Red Blue @@ -11,57 +12,72 @@ type Colors struct { Magenta Green } +// Red color type Red struct{} +// Blue color type Blue struct{} +// Yellow color type Yellow struct{} +// Magenta color type Magenta struct{} +// Green color type Green struct{} +// Regular font in red func (c Red) Regular(t ...interface{}) string { r := color.New(color.FgRed).SprintFunc() return r(t...) } +// Bold font in red func (c Red) Bold(t ...interface{}) string { r := color.New(color.FgRed, color.Bold).SprintFunc() return r(t...) } +// Regular font in blue func (c Blue) Regular(t ...interface{}) string { r := color.New(color.FgBlue).SprintFunc() return r(t...) } +// Bold font in blue func (c Blue) Bold(t ...interface{}) string { r := color.New(color.FgBlue, color.Bold).SprintFunc() return r(t...) } +// Regular font in yellow func (c Yellow) Regular(t ...interface{}) string { r := color.New(color.FgYellow).SprintFunc() return r(t...) } +// Bold font in red func (c Yellow) Bold(t ...interface{}) string { r := color.New(color.FgYellow, color.Bold).SprintFunc() return r(t...) } +// Regular font in magenta func (c Magenta) Regular(t ...interface{}) string { r := color.New(color.FgMagenta).SprintFunc() return r(t...) } +// Bold font in magenta func (c Magenta) Bold(t ...interface{}) string { r := color.New(color.FgMagenta, color.Bold).SprintFunc() return r(t...) } +// Regular font in green func (c Green) Regular(t ...interface{}) string { r := color.New(color.FgGreen).SprintFunc() return r(t...) } +// Bold font in red func (c Green) Bold(t ...interface{}) string { r := color.New(color.FgGreen, color.Bold).SprintFunc() return r(t...) diff --git a/settings/io.go b/settings/io.go index 9619aea..c479928 100644 --- a/settings/io.go +++ b/settings/io.go @@ -6,7 +6,7 @@ import ( "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) { _, err := os.Stat(file) if err == nil { diff --git a/settings/settings.go b/settings/settings.go index 57f671e..c4114bb 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -1,10 +1,11 @@ package settings import ( - "gopkg.in/yaml.v2" "os" + "gopkg.in/yaml.v2" ) +// Settings defines a group of general settings type Settings struct { Colors `yaml:"-"` Resources `yaml:"resources" json:"resources"` @@ -12,10 +13,12 @@ type Settings struct { Config `yaml:"config" json:"config"` } +// Config defines structural options type Config struct { Flimit uint64 `yaml:"flimit" json:"flimit"` } +// Server settings, used for the web panel type Server struct { Enabled bool `yaml:"enable" json:"enable"` Open bool `yaml:"open" json:"open"` @@ -23,6 +26,7 @@ type Server struct { Port int `yaml:"port" json:"port"` } +// Resources defines the files generated by realize type Resources struct { Config string `yaml:"-" json:"-"` Output string `yaml:"output" json:"output"` diff --git a/settings/utils.go b/settings/utils.go index e67c502..38804c9 100644 --- a/settings/utils.go +++ b/settings/utils.go @@ -6,12 +6,14 @@ import ( "path/filepath" ) +// Wdir return the current working directory func (s Settings) Wdir() string { dir, err := os.Getwd() s.Validate(err) return filepath.Base(dir) } +// Validate checks a fatal error func (s Settings) Validate(err error) error { if err != nil { s.Fatal(err, "") @@ -19,6 +21,7 @@ func (s Settings) Validate(err error) error { return nil } +// Fatal prints a fatal error with its additional messages func (s Settings) Fatal(err error, msg ...interface{}) { if len(msg) > 0 { log.Fatalln(s.Red.Regular(msg...), err.Error()) diff --git a/watcher/cmd.go b/watcher/cmd.go index d4e2a2c..da54349 100644 --- a/watcher/cmd.go +++ b/watcher/cmd.go @@ -8,7 +8,7 @@ import ( "strings" ) -// Watch method adds the given paths on the Watcher +// Run launches the toolchain for each project func (h *Blueprint) Run() error { err := h.check() 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 { err := h.Add(p) return err @@ -134,9 +134,8 @@ func (h *Blueprint) check() error { if len(h.Projects) > 0 { h.Clean() 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 diff --git a/watcher/main.go b/watcher/main.go index 50fd52e..79710e1 100644 --- a/watcher/main.go +++ b/watcher/main.go @@ -14,7 +14,7 @@ type logWriter struct { c.Colors } -// Projects struct contains a projects list +// Blueprint struct contains a projects list type Blueprint struct { *c.Settings `yaml:"-"` Projects []Project `yaml:"projects,omitempty" json:"projects,omitempty"` @@ -28,12 +28,12 @@ type Project struct { base string Name string `yaml:"name" json:"name"` 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"` 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"` Watcher Watcher `yaml:"watcher" json:"watcher"` Cli Cli `yaml:"cli" json:"cli"` @@ -54,23 +54,26 @@ type Watcher struct { Preview bool `yaml:"preview" json:"preview"` } +// Cli output status, enables or disables type Cli struct { Streams bool `yaml:"streams" json:"streams"` } +// File determinates the status of each log files (streams, logs, errors) type File struct { Streams bool `yaml:"streams" json:"streams"` Logs bool `yaml:"logs" json:"logs"` Errors bool `yaml:"errors" json:"errors"` } -// Buffer struct for buffering outputs +// Buffer define an array buffer for each log files type Buffer struct { StdOut []BufferOut `json:"stdOut"` StdLog []BufferOut `json:"stdLog"` StdErr []BufferOut `json:"stdErr"` } +// BufferOut is used for exchange information between "realize cli" and "web realize" type BufferOut struct { Time time.Time `json:"time"` Text string `json:"text"` diff --git a/watcher/watcher.go b/watcher/watcher.go index b9d3103..a977713 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -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() { if p.Bin { start := time.Now() @@ -101,6 +101,7 @@ func (p *Project) install() { return } +// Install calls an implementation of "go run" func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { if p.Run { start := time.Now() @@ -265,7 +266,7 @@ func (p *Project) walks(watcher *fsnotify.Watcher) error { return nil } -// Ignore validates a path +// Ignore and validate a path func (p *Project) ignore(str string) bool { for _, v := range p.Watcher.Ignore { if strings.Contains(str, filepath.Join(p.base, v)) { @@ -275,7 +276,7 @@ func (p *Project) ignore(str string) bool { 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) { p.install() p.build()