Skip to content

Commit 7ae73b6

Browse files
committed
code cleaned up
1 parent 6d747bd commit 7ae73b6

File tree

3 files changed

+101
-65
lines changed

3 files changed

+101
-65
lines changed

controllers/functions.go

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package controllers
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"godemon/models"
7-
"io/ioutil"
85
"os"
96
"os/exec"
107
"time"
@@ -37,66 +34,16 @@ func ProgramStarting(cnf *string, filepath string, modOrFile string, command str
3734
if *cnf == "cmd" {
3835

3936
} else if *cnf == "cnf" {
40-
jsonFile, err := os.Open("godemon-cnf.json")
41-
ErrorHandle(err)
42-
defer jsonFile.Close()
43-
byteValue, err := ioutil.ReadAll(jsonFile)
44-
ErrorHandle(err)
45-
var commands models.Commands
46-
json.Unmarshal(byteValue, &commands)
47-
for i := 0; i < len(commands.Commands); i++ {
48-
if command == commands.Commands[i].Name {
49-
fmt.Println(commands.Commands[i].Path)
50-
fmt.Println(commands.Commands[i].Option)
51-
filepath = commands.Commands[i].Path
52-
modOrFile = commands.Commands[i].Option
53-
}
54-
}
37+
cnfFunc(command, "", "")
5538
} else if *cnf == "deploy" {
56-
jsonFile, err := os.Open("project.json")
57-
ErrorHandle(err)
58-
defer jsonFile.Close()
59-
byteValue, err := ioutil.ReadAll(jsonFile)
60-
ErrorHandle(err)
61-
var pr models.Project
62-
json.Unmarshal(byteValue, &pr)
63-
goos := "GOOS=" + pr.OS
64-
arch := "GOARCH=" + pr.Arch
65-
name := pr.Name
66-
os.Chdir(pr.Path)
67-
fmt.Println(pr)
68-
cmd := exec.Command("env", goos, arch, "go", "build", "-o", name)
69-
cmd.Stdout = os.Stdout
70-
cmd.Stderr = os.Stderr
71-
cmd.Run()
72-
os.Exit(1)
39+
deploy()
7340
} else if init == true {
74-
os.Mkdir(name, 0777)
75-
os.Chdir(name)
76-
var project models.Project
77-
path, _ := os.Getwd()
78-
project.Path = path
79-
project.Name = name
80-
project.Arch = arch
81-
project.OS = oso
82-
var commands models.Commands
83-
var command models.Command
84-
command.Name = "run"
85-
command.Path = path
86-
command.Option = "mod"
87-
commands.Commands = append(commands.Commands, command)
88-
file, _ := json.MarshalIndent(project, "", " ")
89-
_ = ioutil.WriteFile("project.json", file, 0644)
90-
file, _ = json.MarshalIndent(commands, "", " ")
91-
_ = ioutil.WriteFile("godemon-cnf.json", file, 0644)
92-
cmd := exec.Command("go", "mod", "init", name)
93-
cmd.Stdout = os.Stdout
94-
cmd.Stderr = os.Stderr
95-
cmd.Run()
96-
os.Exit(1)
97-
} else if *help == true {
98-
fmt.Printf("Godemon %v: \n 1. -cnf <- in this flag put info about what do you want to do - if use cmd option use -cnf=cmd, if config file use -cnf=cnf \n 2. -path <- path to file/directory \n 3. -modOrFile <- are you using modules or one file \n 4. -command <- binded command in config file \n", version)
99-
os.Exit(1)
41+
initialize(name, arch, oso)
42+
} else if *help == true ||
43+
(*cnf == "" && filepath == "" && modOrFile == "" &&
44+
command == "" && *help == false && init == false &&
45+
name == "" && oso == "" && arch == "") {
46+
models.HelpCLI(version)
10047
}
10148
return filepath, modOrFile
10249
}
@@ -112,10 +59,7 @@ func WatchFiles(fileordirPath string) error {
11259
return err
11360
}
11461
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {
115-
cmd := exec.Command("killall", "-9", "app-godemon-app-godemon-tmp-generated")
116-
cmd.Stdout = os.Stdout
117-
cmd.Stderr = os.Stderr
118-
cmd.Run()
62+
killProcess()
11963
break
12064
}
12165
time.Sleep(1 * time.Second)

controllers/pv.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package controllers
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"godemon/models"
7+
"io/ioutil"
8+
"os"
9+
"os/exec"
10+
)
11+
12+
func deploy() {
13+
jsonFile, err := os.Open("project.json")
14+
ErrorHandle(err)
15+
defer jsonFile.Close()
16+
byteValue, err := ioutil.ReadAll(jsonFile)
17+
ErrorHandle(err)
18+
var pr models.Project
19+
json.Unmarshal(byteValue, &pr)
20+
goos := "GOOS=" + pr.OS
21+
arch := "GOARCH=" + pr.Arch
22+
name := pr.Name
23+
os.Chdir(pr.Path)
24+
fmt.Println(pr)
25+
cmd := exec.Command("env", goos, arch, "go", "build", "-o", name)
26+
cmd.Stdout = os.Stdout
27+
cmd.Stderr = os.Stderr
28+
cmd.Run()
29+
os.Exit(1)
30+
}
31+
32+
func initialize(name string, arch string, oso string) {
33+
os.Mkdir(name, 0777)
34+
os.Chdir(name)
35+
var project models.Project
36+
path, _ := os.Getwd()
37+
project.Path = path
38+
project.Name = name
39+
project.Arch = arch
40+
project.OS = oso
41+
var commands models.Commands
42+
var command models.Command
43+
command.Name = "run"
44+
command.Path = path
45+
command.Option = "mod"
46+
commands.Commands = append(commands.Commands, command)
47+
file, _ := json.MarshalIndent(project, "", " ")
48+
_ = ioutil.WriteFile("project.json", file, 0644)
49+
file, _ = json.MarshalIndent(commands, "", " ")
50+
_ = ioutil.WriteFile("godemon-cnf.json", file, 0644)
51+
cmd := exec.Command("go", "mod", "init", name)
52+
cmd.Stdout = os.Stdout
53+
cmd.Stderr = os.Stderr
54+
cmd.Run()
55+
os.Exit(1)
56+
}
57+
58+
func cnfFunc(command string, filepath string, modOrFile string) {
59+
jsonFile, err := os.Open("godemon-cnf.json")
60+
ErrorHandle(err)
61+
defer jsonFile.Close()
62+
byteValue, err := ioutil.ReadAll(jsonFile)
63+
ErrorHandle(err)
64+
var commands models.Commands
65+
json.Unmarshal(byteValue, &commands)
66+
for i := 0; i < len(commands.Commands); i++ {
67+
if command == commands.Commands[i].Name {
68+
fmt.Println(commands.Commands[i].Path)
69+
fmt.Println(commands.Commands[i].Option)
70+
filepath = commands.Commands[i].Path
71+
modOrFile = commands.Commands[i].Option
72+
}
73+
}
74+
}
75+
76+
func killProcess() {
77+
cmd := exec.Command("killall", "-9", "app-godemon-app-godemon-tmp-generated")
78+
cmd.Stdout = os.Stdout
79+
cmd.Stderr = os.Stderr
80+
cmd.Run()
81+
}

models/staticCLI.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package models
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func HelpCLI(version string) {
9+
fmt.Printf("Godemon %v: \n 1. -cnf <- in this flag put info about what do you want to do - if use cmd option use -cnf=cmd, if config file use -cnf=cnf \n 2. -path <- path to file/directory \n 3. -modOrFile <- are you using modules or one file \n 4. -command <- binded command in config file \n", version)
10+
os.Exit(1)
11+
}

0 commit comments

Comments
 (0)