-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.go
More file actions
66 lines (59 loc) · 1.46 KB
/
generate.go
File metadata and controls
66 lines (59 loc) · 1.46 KB
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
package main
import (
"bufio"
"os"
"strings"
"fmt"
"log"
"encoding/json"
"path/filepath"
)
func generate () {
jsonfile := filepath.Dir(os.Args[0]) + "\\conf.json"
reader := bufio.NewReader(os.Stdin)
if fStat, err := os.Stat(jsonfile); err == nil && !fStat.IsDir() {
fmt.Printf("Configuration file (%s) exists, overwrite? (y/n): ", jsonfile)
if r, _ := reader.ReadString('\n'); strings.ToLower(strings.TrimSpace(r)) != "y" {
return
}
err := os.Remove(jsonfile)
if err != nil {
log.Fatalln(err)
}
}
f, err := os.OpenFile(jsonfile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
w := bufio.NewWriter(f)
c := &Config{
From: EmailStruct{
Name: ask("Enter \"From\" name:\t\t"),
Email: ask("Enter \"From\" email:\t\t"),
},
To: EmailStruct{
Name: ask("Enter \"To\" name:\t\t"),
Email: ask("Enter \"To\" email:\t\t"),
},
Server: ask("Enter SMTP server:\t\t"),
Port: ask("Port:\t\t\t\t"),
ScanningInterval: ask("Scan interval:\t\t\t"),
BlacklistPath: ask("Path to blacklist file:\t"),
LogFilesPath: ask("Path to folder with log files:\t"),
}
j, err := json.MarshalIndent(c, "", "\t")
if err != nil {
log.Fatalln(err)
}
fmt.Fprintf(w, "%s", j)
w.Flush()
fmt.Printf("Configuration file generated: %s.\n", jsonfile)
return
}
func ask(s string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(s)
r, _, _ := reader.ReadLine()
return string(r)
}