Add Go documents

This commit is contained in:
Max Claus Nunes 2018-06-20 23:04:16 -03:00
parent 2847f41c96
commit f4157b5069
6 changed files with 26 additions and 32 deletions

View File

@ -8,7 +8,7 @@ import (
"strings" "strings"
) )
// Builder ... // Builder is a interface for the build process
type Builder interface { type Builder interface {
Build() error Build() error
Binary() string Binary() string
@ -22,7 +22,7 @@ type builder struct {
buildArgs []string buildArgs []string
} }
// NewBuilder ... // NewBuilder creates a new builder
func NewBuilder(dir string, bin string, wd string, buildArgs []string) Builder { func NewBuilder(dir string, bin string, wd string, buildArgs []string) Builder {
// resolve bin name by current folder name // resolve bin name by current folder name
if bin == "" { if bin == "" {
@ -40,12 +40,12 @@ func NewBuilder(dir string, bin string, wd string, buildArgs []string) Builder {
return &builder{dir: dir, binary: bin, wd: wd, buildArgs: buildArgs} return &builder{dir: dir, binary: bin, wd: wd, buildArgs: buildArgs}
} }
// Binary ... // Binary returns its build binary's path
func (b *builder) Binary() string { func (b *builder) Binary() string {
return b.binary return b.binary
} }
// Build ... // Build the Golang project set for this builder
func (b *builder) Build() error { func (b *builder) Build() error {
logger.Info("Building program") logger.Info("Building program")
args := append([]string{"go", "build", "-o", filepath.Join(b.wd, b.binary)}, b.buildArgs...) args := append([]string{"go", "build", "-o", filepath.Join(b.wd, b.binary)}, b.buildArgs...)

View File

@ -7,62 +7,55 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
) )
// Logger .. // Logger used by gaper
type Logger struct { type Logger struct {
verbose bool verbose bool
logDebug *log.Logger logDebug *log.Logger
logWarn *log.Logger
logInfo *log.Logger logInfo *log.Logger
logError *log.Logger logError *log.Logger
} }
// NewLogger ... // NewLogger creates a new logger
func NewLogger(prefix string) *Logger { func NewLogger(prefix string) *Logger {
prefix = "[" + prefix + "] " prefix = "[" + prefix + "] "
return &Logger{ return &Logger{
verbose: false, verbose: false,
logDebug: log.New(os.Stdout, prefix, 0), logDebug: log.New(os.Stdout, prefix, 0),
logWarn: log.New(os.Stdout, color.YellowString(prefix), 0),
logInfo: log.New(os.Stdout, color.CyanString(prefix), 0), logInfo: log.New(os.Stdout, color.CyanString(prefix), 0),
logError: log.New(os.Stdout, color.RedString(prefix), 0), logError: log.New(os.Stdout, color.RedString(prefix), 0),
} }
} }
// Verbose ... // Verbose toggle this logger verbosity
func (l *Logger) Verbose(verbose bool) { func (l *Logger) Verbose(verbose bool) {
l.verbose = verbose l.verbose = verbose
} }
// Debug ... // Debug logs a debug message
func (l *Logger) Debug(v ...interface{}) { func (l *Logger) Debug(v ...interface{}) {
if l.verbose { if l.verbose {
l.logDebug.Println(v...) l.logDebug.Println(v...)
} }
} }
// Debugf ... // Debugf logs a debug message with format
func (l *Logger) Debugf(format string, v ...interface{}) { func (l *Logger) Debugf(format string, v ...interface{}) {
if l.verbose { if l.verbose {
l.logDebug.Printf(format, v...) l.logDebug.Printf(format, v...)
} }
} }
// Warn ... // Info logs a info message
func (l *Logger) Warn(v ...interface{}) {
l.logWarn.Println(v...)
}
// Info ...
func (l *Logger) Info(v ...interface{}) { func (l *Logger) Info(v ...interface{}) {
l.logInfo.Println(v...) l.logInfo.Println(v...)
} }
// Error ... // Error logs an error message
func (l *Logger) Error(v ...interface{}) { func (l *Logger) Error(v ...interface{}) {
l.logError.Println(v...) l.logError.Println(v...)
} }
// Errorf ... // Errorf logs and error message with format
func (l *Logger) Errorf(format string, v ...interface{}) { func (l *Logger) Errorf(format string, v ...interface{}) {
l.logError.Printf(format, v...) l.logError.Printf(format, v...)
} }

View File

@ -1,3 +1,5 @@
// Package gaper implements a supervisor restarts a go project
// when it crashes or a watched file changes
package main package main
import ( import (
@ -19,7 +21,7 @@ var logger = NewLogger("gaper")
var exitStatusSuccess = 0 var exitStatusSuccess = 0
var exitStatusError = 1 var exitStatusError = 1
// Config ... // Config contains all settings supported by gaper
type Config struct { type Config struct {
BinName string BinName string
BuildPath string BuildPath string

View File

@ -1,10 +1,9 @@
package main package main
import ( import (
"fmt"
"testing" "testing"
) )
func TestGaper(t *testing.T) { func TestGaper(t *testing.T) {
fmt.Println("Sample test") // TODO: add test to main file
} }

View File

@ -10,13 +10,13 @@ import (
"time" "time"
) )
// OSWindows ... // OSWindows is used to check if current OS is a Windows
const OSWindows = "windows" const OSWindows = "windows"
// os errors // os errors
var errFinished = errors.New("os: process already finished") var errFinished = errors.New("os: process already finished")
// Runner ... // Runner is a interface for the run process
type Runner interface { type Runner interface {
Run() (*exec.Cmd, error) Run() (*exec.Cmd, error)
Kill() error Kill() error
@ -35,7 +35,7 @@ type runner struct {
end chan bool // used internally by Kill to wait a process die end chan bool // used internally by Kill to wait a process die
} }
// NewRunner ... // NewRunner creates a new runner
func NewRunner(wStdout io.Writer, wStderr io.Writer, bin string, args []string) Runner { func NewRunner(wStdout io.Writer, wStderr io.Writer, bin string, args []string) Runner {
return &runner{ return &runner{
bin: bin, bin: bin,
@ -48,7 +48,7 @@ func NewRunner(wStdout io.Writer, wStderr io.Writer, bin string, args []string)
} }
} }
// Run ... // Run executes the project binary
func (r *runner) Run() (*exec.Cmd, error) { func (r *runner) Run() (*exec.Cmd, error) {
logger.Info("Starting program") logger.Info("Starting program")
@ -63,7 +63,7 @@ func (r *runner) Run() (*exec.Cmd, error) {
return r.command, nil return r.command, nil
} }
// Kill ... // Kill the current process running for the Golang project
func (r *runner) Kill() error { func (r *runner) Kill() error {
if r.command == nil || r.command.Process == nil { if r.command == nil || r.command.Process == nil {
return nil return nil
@ -97,12 +97,12 @@ func (r *runner) Kill() error {
return nil return nil
} }
// Exited ... // Exited checks if the process has exited
func (r *runner) Exited() bool { func (r *runner) Exited() bool {
return r.command != nil && r.command.ProcessState != nil && r.command.ProcessState.Exited() return r.command != nil && r.command.ProcessState != nil && r.command.ProcessState.Exited()
} }
// Errors ... // Errors get errors occurred during the build
func (r *runner) Errors() chan error { func (r *runner) Errors() chan error {
return r.errors return r.errors
} }

View File

@ -16,7 +16,7 @@ var DefaultExtensions = []string{"go"}
// DefaultPoolInterval used by the watcher // DefaultPoolInterval used by the watcher
var DefaultPoolInterval = 500 var DefaultPoolInterval = 500
// Watcher ... // Watcher is a interface for the watch process
type Watcher struct { type Watcher struct {
PollInterval int PollInterval int
WatchItems []string WatchItems []string
@ -26,7 +26,7 @@ type Watcher struct {
Errors chan error Errors chan error
} }
// NewWatcher ... // NewWatcher creates a new watcher
func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, extensions []string) (*Watcher, error) { func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, extensions []string) (*Watcher, error) {
if pollInterval == 0 { if pollInterval == 0 {
pollInterval = DefaultPoolInterval pollInterval = DefaultPoolInterval
@ -64,7 +64,7 @@ func NewWatcher(pollInterval int, watchItems []string, ignoreItems []string, ext
var startTime = time.Now() var startTime = time.Now()
var errDetectedChange = errors.New("done") var errDetectedChange = errors.New("done")
// Watch ... // Watch starts watching for file changes
func (w *Watcher) Watch() { func (w *Watcher) Watch() {
for { for {
for i := range w.WatchItems { for i := range w.WatchItems {