formatted
This commit is contained in:
parent
4baec825cf
commit
b859b29563
4
main.go
4
main.go
|
@ -10,14 +10,14 @@ func main() {
|
|||
|
||||
app := realize.Init()
|
||||
|
||||
handle := func(err error) error{
|
||||
handle := func(err error) error {
|
||||
if err != nil {
|
||||
return cli.Exit(err.Error(), 86)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
header := func(){
|
||||
header := func() {
|
||||
app.Information()
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
const(
|
||||
const (
|
||||
app_name = "Realize"
|
||||
app_version = "v1.0"
|
||||
app_email = "pracchia@hastega.it"
|
||||
|
@ -23,15 +23,15 @@ var red = color.New(color.FgRed).SprintFunc()
|
|||
var blue = color.New(color.FgBlue, color.Bold).SprintFunc()
|
||||
var bluel = color.New(color.FgBlue).SprintFunc()
|
||||
|
||||
var watcher_ignores = []string {"vendor","bin"}
|
||||
var watcher_exts = []string {".go"}
|
||||
var watcher_paths = []string {"/"}
|
||||
var watcher_ignores = []string{"vendor", "bin"}
|
||||
var watcher_exts = []string{".go"}
|
||||
var watcher_paths = []string{"/"}
|
||||
|
||||
type App struct{
|
||||
Name,Version,Description,Author,Email string
|
||||
type App struct {
|
||||
Name, Version, Description, Author, Email string
|
||||
}
|
||||
|
||||
func Init() *App{
|
||||
func Init() *App {
|
||||
return &App{
|
||||
Name: app_name,
|
||||
Version: app_version,
|
||||
|
@ -41,21 +41,20 @@ func Init() *App{
|
|||
}
|
||||
}
|
||||
|
||||
func Fail(msg string){
|
||||
func Fail(msg string) {
|
||||
fmt.Println(red(msg))
|
||||
}
|
||||
|
||||
func Success(msg string){
|
||||
func Success(msg string) {
|
||||
fmt.Println(green(msg))
|
||||
}
|
||||
|
||||
func LogSuccess(msg string){
|
||||
func LogSuccess(msg string) {
|
||||
log.Println(green(msg))
|
||||
}
|
||||
|
||||
|
||||
func (app *App) Information(){
|
||||
fmt.Println(blue(app.Name)+" - "+blue(app.Version))
|
||||
fmt.Println(bluel(app.Description)+"\n")
|
||||
func (app *App) Information() {
|
||||
fmt.Println(blue(app.Name) + " - " + blue(app.Version))
|
||||
fmt.Println(bluel(app.Description) + "\n")
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ type Config struct {
|
|||
}
|
||||
|
||||
// Default value
|
||||
func New(params *cli.Context) *Config{
|
||||
func New(params *cli.Context) *Config {
|
||||
return &Config{
|
||||
file: app_file,
|
||||
Version: "1.0",
|
||||
|
@ -39,9 +39,9 @@ func New(params *cli.Context) *Config{
|
|||
}
|
||||
|
||||
// check for duplicates
|
||||
func Duplicates(value Project, arr []Project) bool{
|
||||
for _, val := range arr{
|
||||
if value.Main == val.Main || value.Name == val.Name{
|
||||
func Duplicates(value Project, arr []Project) bool {
|
||||
for _, val := range arr {
|
||||
if value.Main == val.Main || value.Name == val.Name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -52,16 +52,16 @@ func Duplicates(value Project, arr []Project) bool{
|
|||
func (h *Config) Clean() {
|
||||
arr := h.Projects
|
||||
for key, val := range arr {
|
||||
if Duplicates(val, arr[key+1:]) {
|
||||
h.Projects = append(arr[:key], arr[key+1:]...)
|
||||
if Duplicates(val, arr[key + 1:]) {
|
||||
h.Projects = append(arr[:key], arr[key + 1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check, Read and remove duplicates from the config file
|
||||
func (h *Config) Read() error{
|
||||
if file, err := ioutil.ReadFile(h.file); err == nil{
|
||||
func (h *Config) Read() error {
|
||||
if file, err := ioutil.ReadFile(h.file); err == nil {
|
||||
if len(h.Projects) > 0 {
|
||||
err = yaml.Unmarshal(file, h)
|
||||
if err == nil {
|
||||
|
@ -70,13 +70,13 @@ func (h *Config) Read() error{
|
|||
return err
|
||||
}
|
||||
return errors.New("There are no projects")
|
||||
}else{
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// write and marshal
|
||||
func (h *Config) Write() error{
|
||||
func (h *Config) Write() error {
|
||||
y, err := yaml.Marshal(h)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -85,12 +85,12 @@ func (h *Config) Write() error{
|
|||
}
|
||||
|
||||
// Create config yaml file
|
||||
func (h *Config) Create(params *cli.Context) error{
|
||||
func (h *Config) Create(params *cli.Context) error {
|
||||
if h.Read() != nil {
|
||||
if err := h.Write(); err != nil {
|
||||
os.Remove(h.file)
|
||||
return err
|
||||
}else{
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ func (h *Config) Create(params *cli.Context) error{
|
|||
}
|
||||
|
||||
// Add another project
|
||||
func (h *Config) Add(params *cli.Context) error{
|
||||
func (h *Config) Add(params *cli.Context) error {
|
||||
if err := h.Read(); err == nil {
|
||||
new := Project{
|
||||
Name: params.String("name"),
|
||||
|
@ -117,45 +117,45 @@ func (h *Config) Add(params *cli.Context) error{
|
|||
}
|
||||
h.Projects = append(h.Projects, new)
|
||||
return h.Write()
|
||||
}else{
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// remove a project in list
|
||||
func (h *Config) Remove(params *cli.Context) error{
|
||||
func (h *Config) Remove(params *cli.Context) error {
|
||||
if err := h.Read(); err == nil {
|
||||
for key, val := range h.Projects {
|
||||
if params.String("name") == val.Name {
|
||||
h.Projects = append(h.Projects[:key], h.Projects[key+1:]...)
|
||||
h.Projects = append(h.Projects[:key], h.Projects[key + 1:]...)
|
||||
return h.Write()
|
||||
}
|
||||
}
|
||||
return errors.New("No project found")
|
||||
}else{
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// List of projects
|
||||
func (h *Config) List() error{
|
||||
func (h *Config) List() error {
|
||||
if err := h.Read(); err == nil {
|
||||
for _, val := range h.Projects {
|
||||
fmt.Println(green("|"), green(val.Name))
|
||||
fmt.Println(greenl("|"),"\t", green("Main File:"), red(val.Main))
|
||||
fmt.Println(greenl("|"),"\t", green("Base Path:"), red(val.Path))
|
||||
fmt.Println(greenl("|"),"\t", green("Run:"), red(val.Run))
|
||||
fmt.Println(greenl("|"),"\t", green("Build:"), red(val.Build))
|
||||
fmt.Println(greenl("|"),"\t", green("Watcher:"))
|
||||
fmt.Println(greenl("|"),"\t\t", green("After:"), red(val.Watcher.After))
|
||||
fmt.Println(greenl("|"),"\t\t", green("Before:"), red(val.Watcher.Before))
|
||||
fmt.Println(greenl("|"),"\t\t", green("Extensions:"), red(val.Watcher.Exts))
|
||||
fmt.Println(greenl("|"),"\t\t", green("Paths:"), red(val.Watcher.Paths))
|
||||
fmt.Println(greenl("|"),"\t\t", green("Paths ignored:"), red(val.Watcher.Ignore))
|
||||
fmt.Println(greenl("|"),"\t\t", green("Watch preview:"), red(val.Watcher.Preview))
|
||||
fmt.Println(greenl("|"), "\t", green("Main File:"), red(val.Main))
|
||||
fmt.Println(greenl("|"), "\t", green("Base Path:"), red(val.Path))
|
||||
fmt.Println(greenl("|"), "\t", green("Run:"), red(val.Run))
|
||||
fmt.Println(greenl("|"), "\t", green("Build:"), red(val.Build))
|
||||
fmt.Println(greenl("|"), "\t", green("Watcher:"))
|
||||
fmt.Println(greenl("|"), "\t\t", green("After:"), red(val.Watcher.After))
|
||||
fmt.Println(greenl("|"), "\t\t", green("Before:"), red(val.Watcher.Before))
|
||||
fmt.Println(greenl("|"), "\t\t", green("Extensions:"), red(val.Watcher.Exts))
|
||||
fmt.Println(greenl("|"), "\t\t", green("Paths:"), red(val.Watcher.Paths))
|
||||
fmt.Println(greenl("|"), "\t\t", green("Paths ignored:"), red(val.Watcher.Ignore))
|
||||
fmt.Println(greenl("|"), "\t\t", green("Watch preview:"), red(val.Watcher.Preview))
|
||||
}
|
||||
return nil
|
||||
}else{
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,13 +20,12 @@ type Project struct {
|
|||
Watcher Watcher `yaml:"app_watcher,omitempty"`
|
||||
}
|
||||
|
||||
func (p *Project) GoRun (channel chan bool) error{
|
||||
func (p *Project) GoRun(channel chan bool) error {
|
||||
base, _ := os.Getwd()
|
||||
build := exec.Command("go", "run", p.Main)
|
||||
path := base + p.Path
|
||||
build.Dir = path
|
||||
|
||||
|
||||
stdout, err := build.StdoutPipe()
|
||||
if err != nil {
|
||||
Fail(err.Error())
|
||||
|
@ -39,22 +38,22 @@ func (p *Project) GoRun (channel chan bool) error{
|
|||
for in.Scan() {
|
||||
select {
|
||||
default:
|
||||
log.Println(p.Name+":",in.Text())
|
||||
case <- channel:
|
||||
log.Println(p.Name + ":", in.Text())
|
||||
case <-channel:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Project) GoBuild() error{
|
||||
func (p *Project) GoBuild() error {
|
||||
var out bytes.Buffer
|
||||
base, _ := os.Getwd()
|
||||
path := base + p.Path
|
||||
|
||||
// create bin dir
|
||||
if _, err := os.Stat(path + "bin"); err != nil {
|
||||
if err = os.Mkdir(path + "bin", 0777); err != nil{
|
||||
if err = os.Mkdir(path + "bin", 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -68,7 +67,7 @@ func (p *Project) GoBuild() error{
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *Project) GoInstall() error{
|
||||
func (p *Project) GoInstall() error {
|
||||
var out bytes.Buffer
|
||||
base, _ := os.Getwd()
|
||||
path := base + p.Path
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type Watcher struct{
|
||||
type Watcher struct {
|
||||
// different before and after on re-run?
|
||||
Before []string `yaml:"before,omitempty"`
|
||||
After []string `yaml:"after,omitempty"`
|
||||
|
@ -20,36 +20,36 @@ type Watcher struct{
|
|||
Preview bool `yaml:"preview,omitempty"`
|
||||
}
|
||||
|
||||
func (h *Config) Watch() error{
|
||||
func (h *Config) Watch() error {
|
||||
if err := h.Read(); err == nil {
|
||||
// loop projects
|
||||
wg.Add(len(h.Projects))
|
||||
for k := range h.Projects {
|
||||
if string(h.Projects[k].Path[0]) != "/" {
|
||||
h.Projects[k].Path = "/"+h.Projects[k].Path
|
||||
h.Projects[k].Path = "/" + h.Projects[k].Path
|
||||
}
|
||||
go h.Projects[k].Watching()
|
||||
}
|
||||
wg.Wait()
|
||||
return nil
|
||||
}else{
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Project) Watching(){
|
||||
func (p *Project) Watching() {
|
||||
|
||||
var watcher *fsnotify.Watcher
|
||||
channel := make(chan bool)
|
||||
watcher, _ = fsnotify.NewWatcher()
|
||||
defer func(){
|
||||
defer func() {
|
||||
watcher.Close()
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
walk := func(path string, info os.FileInfo, err error) error{
|
||||
if !Ignore(path,p.Watcher.Ignore) {
|
||||
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.Contains(path, "/.")) || (InArray(filepath.Ext(path), p.Watcher.Exts)){
|
||||
walk := func(path string, info os.FileInfo, err error) error {
|
||||
if !Ignore(path, p.Watcher.Ignore) {
|
||||
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.Contains(path, "/.")) || (InArray(filepath.Ext(path), p.Watcher.Exts)) {
|
||||
if p.Watcher.Preview {
|
||||
fmt.Println(p.Name + ": \t" + path)
|
||||
}
|
||||
|
@ -65,24 +65,23 @@ func (p *Project) Watching(){
|
|||
base, _ := os.Getwd()
|
||||
// check main existence
|
||||
if _, err := os.Stat(base + p.Path + dir + p.Main); err != nil {
|
||||
Fail(p.Name + ": \t"+base + p.Path + dir + p.Main+ " doesn't exist. Main is required")
|
||||
Fail(p.Name + ": \t" + base + p.Path + dir + p.Main + " doesn't exist. Main is required")
|
||||
return
|
||||
}
|
||||
|
||||
// check paths existence
|
||||
if _, err := os.Stat(base + p.Path + dir); err == nil {
|
||||
if err := filepath.Walk(base + p.Path + dir, walk); err != nil {
|
||||
Fail(err.Error())
|
||||
}
|
||||
}else{
|
||||
Fail(p.Name + ": \t"+base + p.Path + dir +" path doesn't exist")
|
||||
} else {
|
||||
Fail(p.Name + ": \t" + base + p.Path + dir + " path doesn't exist")
|
||||
}
|
||||
}
|
||||
|
||||
// go build, install, run
|
||||
go p.build(); p.install(); p.run(channel);
|
||||
|
||||
fmt.Println(red("\n Watching: '"+ p.Name +"'\n"))
|
||||
fmt.Println(red("\n Watching: '" + p.Name + "'\n"))
|
||||
|
||||
p.reload = time.Now().Truncate(time.Second)
|
||||
|
||||
|
@ -95,7 +94,7 @@ func (p *Project) Watching(){
|
|||
}
|
||||
if _, err := os.Stat(event.Name); err == nil {
|
||||
i := strings.Index(event.Name, filepath.Ext(event.Name))
|
||||
log.Println(green(p.Name+":"), event.Name[:i])
|
||||
log.Println(green(p.Name + ":"), event.Name[:i])
|
||||
|
||||
// stop and run again
|
||||
close(channel)
|
||||
|
@ -111,13 +110,13 @@ func (p *Project) Watching(){
|
|||
}
|
||||
}
|
||||
|
||||
func (p *Project) install(){
|
||||
func (p *Project) install() {
|
||||
if p.Bin {
|
||||
LogSuccess(p.Name + ": Installing..")
|
||||
if err := p.GoInstall(); err != nil{
|
||||
if err := p.GoInstall(); err != nil {
|
||||
Fail(err.Error())
|
||||
return
|
||||
}else{
|
||||
} else {
|
||||
LogSuccess(p.Name + ": Installed")
|
||||
return
|
||||
}
|
||||
|
@ -125,13 +124,13 @@ func (p *Project) install(){
|
|||
return
|
||||
}
|
||||
|
||||
func (p *Project) build(){
|
||||
func (p *Project) build() {
|
||||
if p.Build {
|
||||
LogSuccess(p.Name + ": Building..")
|
||||
if err := p.GoBuild(); err != nil{
|
||||
if err := p.GoBuild(); err != nil {
|
||||
Fail(err.Error())
|
||||
return
|
||||
}else{
|
||||
} else {
|
||||
LogSuccess(p.Name + ": Builded")
|
||||
return
|
||||
}
|
||||
|
@ -139,7 +138,7 @@ func (p *Project) build(){
|
|||
return
|
||||
}
|
||||
|
||||
func (p *Project) run(channel chan bool){
|
||||
func (p *Project) run(channel chan bool) {
|
||||
if p.Run {
|
||||
LogSuccess(p.Name + ": Running..")
|
||||
go p.GoRun(channel)
|
||||
|
@ -148,7 +147,7 @@ func (p *Project) run(channel chan bool){
|
|||
return
|
||||
}
|
||||
|
||||
func InArray(str string, list []string) bool{
|
||||
func InArray(str string, list []string) bool {
|
||||
for _, v := range list {
|
||||
if v == str {
|
||||
return true
|
||||
|
@ -157,7 +156,7 @@ func InArray(str string, list []string) bool{
|
|||
return false
|
||||
}
|
||||
|
||||
func Ignore(str string, list []string) bool{
|
||||
func Ignore(str string, list []string) bool {
|
||||
for _, v := range list {
|
||||
if strings.Contains(str, v) {
|
||||
return true
|
||||
|
|
Loading…
Reference in New Issue