/
init.go
103 lines (74 loc) · 1.8 KB
/
init.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
94
95
96
97
98
99
100
101
102
103
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"github.com/AnthonyCapirchio/golem/internal/config"
httpService "github.com/AnthonyCapirchio/golem/pkg/server/http"
)
type initOpts struct {
configFile string
}
var successMessage string = `
The configuration has successfully initialized.
Configuration file location: %s
Configured port: %s
`
// This command create a new configuration file
// golem init
func initCmd() command {
fs := flag.NewFlagSet("golem init", flag.ExitOnError)
opts := &initOpts{}
// fs.StringVar(&opts.configFile, "config", "./.golem/golem.yaml", "Config File")
return command{fs, func(args []string) error {
fs.Parse(args)
return initGolem(opts)
}}
}
func initGolem(opts *initOpts) (err error) {
createFolders()
port := askForDefaultPort()
cfg := config.InitConfig(ConfigPath)
cfg.SetPort(port)
cfg.Services = []config.Service{
config.Service{
Name: "Ping",
HTTPConfig: httpService.ServerConfig{
Routes: map[string]httpService.HTTPHandler{
"/ping": httpService.HTTPHandler{
Body: "pong!!",
},
},
},
},
}
cfg.Save()
fmt.Printf(successMessage, ConfigPath, port)
return nil
}
func askForDefaultPort() string {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Which port do you want to use (default %s) ? ", DefaultPort)
port, err := reader.ReadString('\n')
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
port = strings.TrimSuffix(port, "\n")
if port == "" {
port = DefaultPort
}
return port
}
func createFolders() {
if _, err := os.Stat(BasePath); os.IsNotExist(err) {
os.Mkdir(BasePath, os.ModePerm)
}
if _, err := os.Stat(TemplatePath); os.IsNotExist(err) {
os.Mkdir(TemplatePath, os.ModePerm)
}
if _, err := os.Stat(DatabasePath); os.IsNotExist(err) {
os.Mkdir(DatabasePath, os.ModePerm)
}
}