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