-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
93 lines (80 loc) · 1.84 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/MaestroError/html-strings-affixer/app"
"github.com/MaestroError/html-strings-affixer/reporter"
)
// Entry point
func main() {
app.Bootstrap()
createConfigFileIfRequested()
app.Start()
}
// Checks config file existence and creates it if requested from user
func createConfigFileIfRequested() {
// Set main vars
configFileName := "affixer.json"
reporter := reporter.Reporter{}
// Get command run directly (pwd)
path := pwd() + "/" + configFileName
// check if config file exists
configExists, err := exists(path)
if err != nil {
panic(err)
}
if !configExists {
// get Executable location
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
// get config example path
exampleFile := exPath + "/affixer-example.json"
exampleExists, err := exists(exampleFile)
if err != nil {
panic(err)
}
// check if example config file exists
if exampleExists {
// Ask user to create
if reporter.AskForConfirmation("You have no affixer.json config file. Do you wanna create it? (Will not affect this run)", "yes") {
// Read example file and get content
var r []byte
var err error
r, err = ioutil.ReadFile(exampleFile)
if err != nil {
panic(err)
}
content := string(r)
// write content in new configFileName file in pwd
errWrite := ioutil.WriteFile(path, []byte(content), 0)
if errWrite != nil {
panic(err)
}
}
}
}
}
// Checks if file exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Get current working directory (where run the command)
func pwd() string {
path, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
return path
}