Skip to content

Commit b3ebd11

Browse files
committed
code cleaned up
1 parent ed7715b commit b3ebd11

File tree

4 files changed

+58
-30
lines changed

4 files changed

+58
-30
lines changed

controllers/errorHandling.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package controllers
2+
3+
import "fmt"
4+
5+
func ErrorHandle(err error) {
6+
if err != nil {
7+
fmt.Println(err)
8+
}
9+
}

controllers/functions.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package controllers
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"godemon/models"
7+
"io/ioutil"
48
"os"
59
"os/exec"
610
"time"
@@ -28,3 +32,28 @@ func TimeLog() {
2832
cmd.Stderr = os.Stderr
2933
cmd.Run()
3034
}
35+
36+
func ProgramStarting(cnf string, filepath string, modOrFile string) (string, string) {
37+
if cnf == "cmd" {
38+
filepath = os.Args[2]
39+
modOrFile = os.Args[3]
40+
} else if cnf == "cnf" {
41+
command := os.Args[2]
42+
jsonFile, err := os.Open("godemon-cnf.json")
43+
ErrorHandle(err)
44+
defer jsonFile.Close()
45+
byteValue, err := ioutil.ReadAll(jsonFile)
46+
ErrorHandle(err)
47+
var commands models.Commands
48+
json.Unmarshal(byteValue, &commands)
49+
for i := 0; i < len(commands.Commands); i++ {
50+
if command == commands.Commands[i].Name {
51+
fmt.Println(commands.Commands[i].Path)
52+
fmt.Println(commands.Commands[i].Option)
53+
filepath = commands.Commands[i].Path
54+
modOrFile = commands.Commands[i].Option
55+
}
56+
}
57+
}
58+
return filepath, modOrFile
59+
}

main.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"godemon/controllers"
7-
"godemon/models"
8-
"io/ioutil"
96
"os"
107
"os/exec"
118
"time"
@@ -16,24 +13,20 @@ func watch(fileordirPath string) error {
1613
if err != nil {
1714
return err
1815
}
19-
2016
for {
2117
stat, err := os.Stat(fileordirPath)
2218
if err != nil {
2319
return err
2420
}
25-
2621
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {
2722
cmd := exec.Command("killall", "-9", "app-godemon-app-godemon-tmp-generated")
2823
cmd.Stdout = os.Stdout
2924
cmd.Stderr = os.Stderr
3025
cmd.Run()
3126
break
3227
}
33-
3428
time.Sleep(1 * time.Second)
3529
}
36-
3730
return nil
3831
}
3932

@@ -42,37 +35,17 @@ func main() {
4235
cnf := os.Args[1]
4336
var filepath string
4437
var modOrFile string
45-
if cnf == "cmd" {
46-
filepath = os.Args[2]
47-
modOrFile = os.Args[3]
48-
} else if cnf == "cnf" {
49-
command := os.Args[2]
50-
jsonFile, _ := os.Open("godemon-cnf.json")
51-
byteValue, _ := ioutil.ReadAll(jsonFile)
52-
var commands models.Commands
53-
json.Unmarshal(byteValue, &commands)
54-
for i := 0; i < len(commands.Commands); i++ {
55-
if command == commands.Commands[i].Name {
56-
fmt.Println(commands.Commands[i].Path)
57-
fmt.Println(commands.Commands[i].Option)
58-
filepath = commands.Commands[i].Path
59-
modOrFile = commands.Commands[i].Option
60-
}
61-
}
62-
jsonFile.Close()
63-
}
38+
filepath, modOrFile = controllers.ProgramStarting(cnf, filepath, modOrFile)
6439
fmt.Println(filepath)
6540
for true {
6641
go func(doneChan chan bool) {
6742
defer func() {
6843
doneChan <- true
6944
}()
70-
7145
err := watch(filepath)
7246
if err != nil {
7347
fmt.Println(err)
7448
}
75-
7649
fmt.Println("File has been changed")
7750
if modOrFile == "mod" {
7851
os.Chdir(filepath)

readme.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ First of all you need to build your version:
1212

1313
### Using default `./godemon`
1414
Now you can move file `godemon` to folder with your go project and
15-
start it using command: `./godemon <fileOrDir> <fileOrModule>`.
15+
start it using command: `./godemon cnf <command>` or `./godemon cmd <fileOrDir> <fileOrModule>`.
1616
In first argument you're giving godemon the path to project, and in second
1717
argument you're choosing is it simple go file or go module - if module pass
1818
argument - `mod` , if file pass argument - `file`
@@ -30,4 +30,21 @@ argument - `mod` , if file pass argument - `file`
3030

3131
### Installing `godemon` from `godemon.exe`
3232
1. Download `godemon.exe`
33-
2. Add path to `godemon.exe` to system PATH
33+
2. Add path to `godemon.exe` to system PATH
34+
35+
### How to work with configuration json file
36+
1. Create in project file `godemon-cnf.json`
37+
2. Create configuration of commands - sample code:
38+
39+
```
40+
{
41+
"commands": [
42+
{
43+
"name": "run",
44+
"path": "/home/nwagner/Desktop/godemon/api/",
45+
"file": "mod"
46+
}
47+
]
48+
}
49+
```
50+
3. Now use command: `godemon cnf <command-name>`

0 commit comments

Comments
 (0)