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"
)
// Builder ...
// Builder is a interface for the build process
type Builder interface {
Build() error
Binary() string
@ -22,7 +22,7 @@ type builder struct {
buildArgs []string
}
// NewBuilder ...
// NewBuilder creates a new builder
func NewBuilder(dir string, bin string, wd string, buildArgs []string) Builder {
// resolve bin name by current folder name
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}
}
// Binary ...
// Binary returns its build binary's path
func (b *builder) Binary() string {
return b.binary
}
// Build ...
// Build the Golang project set for this builder
func (b *builder) Build() error {
logger.Info("Building program")
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"
)
// Logger ..
// Logger used by gaper
type Logger struct {
verbose bool
logDebug *log.Logger
logWarn *log.Logger
logInfo *log.Logger
logError *log.Logger
}
// NewLogger ...
// NewLogger creates a new logger
func NewLogger(prefix string) *Logger {
prefix = "[" + prefix + "] "
return &Logger{
verbose: false,
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),
logError: log.New(os.Stdout, color.RedString(prefix), 0),
}
}
// Verbose ...
// Verbose toggle this logger verbosity
func (l *Logger) Verbose(verbose bool) {
l.verbose = verbose
}
// Debug ...
// Debug logs a debug message
func (l *Logger) Debug(v ...interface{}) {
if l.verbose {
l.logDebug.Println(v...)
}
}
// Debugf ...
// Debugf logs a debug message with format
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.verbose {
l.logDebug.Printf(format, v...)
}
}
// Warn ...
func (l *Logger) Warn(v ...interface{}) {
l.logWarn.Println(v...)
}
// Info ...
// Info logs a info message
func (l *Logger) Info(v ...interface{}) {
l.logInfo.Println(v...)
}
// Error ...
// Error logs an error message
func (l *Logger) Error(v ...interface{}) {
l.logError.Println(v...)
}
// Errorf ...
// Errorf logs and error message with format
func (l *Logger) Errorf(format string, v ...interface{}) {
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
import (
@ -19,7 +21,7 @@ var logger = NewLogger("gaper")
var exitStatusSuccess = 0
var exitStatusError = 1
// Config ...
// Config contains all settings supported by gaper
type Config struct {
BinName string
BuildPath string

View File

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

View File

@ -10,13 +10,13 @@ import (
"time"
)
// OSWindows ...
// OSWindows is used to check if current OS is a Windows
const OSWindows = "windows"
// os errors
var errFinished = errors.New("os: process already finished")
// Runner ...
// Runner is a interface for the run process
type Runner interface {
Run() (*exec.Cmd, error)
Kill() error
@ -35,7 +35,7 @@ type runner struct {
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 {
return &runner{
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) {
logger.Info("Starting program")
@ -63,7 +63,7 @@ func (r *runner) Run() (*exec.Cmd, error) {
return r.command, nil
}
// Kill ...
// Kill the current process running for the Golang project
func (r *runner) Kill() error {
if r.command == nil || r.command.Process == nil {
return nil
@ -97,12 +97,12 @@ func (r *runner) Kill() error {
return nil
}
// Exited ...
// Exited checks if the process has exited
func (r *runner) Exited() bool {
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 {
return r.errors
}

View File

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