-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinitCommand.go
126 lines (103 loc) · 3.23 KB
/
initCommand.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"github.com/mkideal/cli"
"gopkg.in/yaml.v2"
)
type initT struct {
cli.Helper
Config string `cli:"config" usage:"Your configuration file" dft:"pomdok.yaml"`
}
var initCommand = &cli.Command{
Name: "init",
Desc: "init your local symfony binary environment to work with a given project",
Argv: func() interface{} { return new(initT) },
Fn: func(ctx *cli.Context) error {
printHeader()
argv := ctx.Argv().(*initT)
config, baseDirectory, err := loadPomdokConfig(argv.Config)
if nil != err {
fmt.Println(err)
return nil
}
if config.Pomdok.Tld == "" {
fmt.Printf("Configuration file error 🙊. Maybe you should give a %s to your domains 🧐\n", yellow("tld"))
return nil
}
if config.Pomdok.Projects == nil {
fmt.Printf("Configuration file error 🙊. Maybe you should add %s 🧐\n", yellow("projects"))
return nil
}
fileDomains := make(map[string]string)
filePorts := make(map[string]int)
for _, element := range config.Pomdok.Projects {
if element.Domain == "" {
fmt.Printf("Configuration file error 🙊. One of the project has empty/no %s 🧐\n", yellow("domain"))
return nil
}
if element.Path == "" {
fmt.Printf("Configuration file error 🙊. One of the project has empty/no %s 🧐\n", yellow("path"))
return nil
}
fullPath := baseDirectory + element.Path
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
fmt.Printf("Configuration file error 🙊. %s path is not found 🧐\n", bold(fullPath))
return nil
}
if _, ok := fileDomains[element.Domain]; ok {
fmt.Printf("Configuration file error 🙊. Domain %s is used more than one time 🧐\n", yellow(element.Domain))
return nil
}
fileDomains[element.Domain] = fullPath
filePorts[element.Domain] = element.Port
}
symfonyJSONData := SymfonyJSONProxy{
Tld: config.Pomdok.Tld,
Port: 7080,
Host: "localhost",
Domains: fileDomains,
Ports: filePorts,
}
symfonyJSON, _ := json.MarshalIndent(symfonyJSONData, "", " ")
configPath := getSymfonyCliConfigPath()
if configPath == "" {
fmt.Printf("Symfony Binary not installed 🙊. Please use %s to see what you should do 🧐\n", yellow("symfony check:requirements"))
return nil
}
ioutil.WriteFile(fmt.Sprintf("%s/proxy.json", configPath), symfonyJSON, 0644)
fmt.Printf("Project setup done ✔\n")
return nil
},
}
func loadPomdokConfig(fileName string) (PomdokYamlConfig, string, error) {
config := PomdokYamlConfig{}
file := findFileUp(fileName, 0)
if file == "" {
return config, "", errors.New("No file found")
}
data, _ := ioutil.ReadFile(file)
yaml.Unmarshal([]byte(data), &config)
return config, path.Dir(file), nil
}
func findFileUp(file string, level int) string {
temp := file
if level > 0 {
temp = strings.Repeat("../", level) + file
}
currentDirectory, _ := os.Getwd()
temp = path.Clean(currentDirectory + "/" + temp)
if temp == "/" {
fmt.Print("Configuration file does not exists 🙊. Maybe you should create or rename your configuration file ? 🧐\n")
return ""
}
if _, err := os.Stat(temp); os.IsNotExist(err) {
return findFileUp(file, level+1) // not found, go up
}
return temp
}