/
cli.go
116 lines (105 loc) · 2.59 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package realize
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/go-siris/siris/core/errors"
"go/build"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"time"
)
var (
// RPrefix tool name
RPrefix = "realize"
// RVersion current version
RVersion = "2.1"
// RExt file extension
RExt = ".yaml"
// RFile config file name
RFile = "." + RPrefix + RExt
//RExtWin windows extension
RExtWin = ".exe"
)
type (
// LogWriter used for all log
LogWriter struct{}
// Realize main struct
Realize struct {
Settings Settings `yaml:"settings" json:"settings"`
Server Server `yaml:"server,omitempty" json:"server,omitempty"`
Schema `yaml:",inline" json:",inline"`
Sync chan string `yaml:"-" json:"-"`
Err Func `yaml:"-" json:"-"`
After Func `yaml:"-" json:"-"`
Before Func `yaml:"-" json:"-"`
Change Func `yaml:"-" json:"-"`
Reload Func `yaml:"-" json:"-"`
}
// Context is used as argument for func
Context struct {
Path string
Project *Project
Stop <-chan bool
Watcher FileWatcher
Event fsnotify.Event
}
// Func is used instead realize func
Func func(Context)
)
// init check
func init() {
// custom log
log.SetFlags(0)
log.SetOutput(LogWriter{})
if build.Default.GOPATH == "" {
log.Fatal("$GOPATH isn't set properly")
}
path := filepath.SplitList(build.Default.GOPATH)
if err := os.Setenv("GOBIN", filepath.Join(path[len(path)-1], "bin")); err != nil {
log.Fatal(err)
}
}
// Stop realize workflow
func (r *Realize) Stop() error {
for k := range r.Schema.Projects {
if r.Schema.Projects[k].exit != nil {
close(r.Schema.Projects[k].exit)
}
}
return nil
}
// Start realize workflow
func (r *Realize) Start() error {
if len(r.Schema.Projects) > 0 {
var wg sync.WaitGroup
wg.Add(len(r.Schema.Projects))
for k := range r.Schema.Projects {
r.Schema.Projects[k].exit = make(chan os.Signal, 1)
signal.Notify(r.Schema.Projects[k].exit, os.Interrupt)
r.Schema.Projects[k].parent = r
go r.Schema.Projects[k].Watch(&wg)
}
wg.Wait()
} else {
return errors.New("there are no projects")
}
return nil
}
// Prefix a given string with tool name
func (r *Realize) Prefix(input string) string {
if len(input) > 0 {
return fmt.Sprint(Yellow.Bold("["), strings.ToUpper(RPrefix), Yellow.Bold("]"), " : ", input)
}
return input
}
// Rewrite the layout of the log timestamp
func (w LogWriter) Write(bytes []byte) (int, error) {
if len(bytes) > 0 {
return fmt.Fprint(Output, Yellow.Regular("["), time.Now().Format("15:04:05"), Yellow.Regular("]"), string(bytes))
}
return 0, nil
}