-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
150 lines (133 loc) · 4.04 KB
/
Copy pathconfig.go
File metadata and controls
150 lines (133 loc) · 4.04 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bufio"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/kless/datautil/valid"
c "github.com/wsxiaoys/terminal/color"
)
// Global variables used in the whole application
var (
GOPATH = os.Getenv("GOPATH")
SRCPATH = filepath.Join(GOPATH, "src")
HOME = os.Getenv("HOME")
GOBI_CONFIG = filepath.Join(HOME, ".gobi.json")
GITHUB = "github.com"
BITBUCKET = "bitbucket.org"
GOOGLE = "code.google.com"
GOBIPATH = filepath.Join(SRCPATH, GITHUB, "fern4lvarez", "gobi")
)
// setGobiPath where the templates and the version file will be located
func setGobiPath() {
if os.Getenv("GOBIPATH") != "" {
GOBIPATH = filepath.Join(SRCPATH, os.Getenv("GOBIPATH"))
}
}
// UserConfig contains all information about the current user
type UserConfig struct {
Name string `json:"name"`
Id string `json:"id"`
Host string `json:"host"`
Email string `json:"email"`
License string `json:"license"`
}
// NewConfig promps a form and returns a UserConfig object
// based on the answers
// A JSON file is stored at $HOME/.gobi.json with this content
func NewConfig() *UserConfig {
c.Println("@{!y}No configuration found! @bI'd like to know more about you.")
// Prompted user configuration form
var name, userName, host, email, license string
name = promptField(validateName,
promptForm["name"]["welcome"],
promptForm["name"]["error"],
promptForm["name"]["welcome2"])
userName = promptField(validateUserName,
promptForm["userName"]["welcome"],
promptForm["userName"]["error"],
promptForm["userName"]["welcome2"])
host = promptField(validateHost,
promptForm["host"]["welcome"],
promptForm["host"]["error"],
promptForm["host"]["welcome2"])
email = promptField(validateEmail,
promptForm["email"]["welcome"],
promptForm["email"]["error"],
promptForm["email"]["welcome2"])
license = promptField(validateLicense,
promptForm["license"]["welcome"],
promptForm["license"]["error"],
promptForm["license"]["welcome2"])
// User config creation
conf := &UserConfig{name, userName, host, email, license}
b, _ := json.Marshal(conf)
ioutil.WriteFile(GOBI_CONFIG, []byte(b), 0744)
return conf
}
// WhoAreYou pretty prints the UserConfig
func (uc UserConfig) WhoAreYou() {
c.Printf("@bYou are @{!g}%s @b(@{!g}%s@b)@b. Creating projects on @{!g}%s/%s @bunder @{!g}%s @blicense.\n",
uc.Name, uc.Email, uc.Host, uc.Id, uc.License)
}
// checkConfig if JSON config file exists and returns the UserConfig if so
func checkConfig() UserConfig {
var user UserConfig
b, errRead := ioutil.ReadFile(GOBI_CONFIG)
if errUnmarshal := json.Unmarshal(b, &user); errRead != nil || errUnmarshal != nil {
return *NewConfig()
}
return user
}
// promptField to validate and save input value
func promptField(validateFunc func(string) bool, welcomeMsg, errorMsg, welcome2Msg string) (resp string) {
reader := bufio.NewReader(os.Stdin)
c.Print(welcomeMsg)
resp, _ = reader.ReadString('\n')
resp = strings.TrimSpace(resp)
for !validateFunc(resp) {
c.Println(errorMsg)
c.Print(welcome2Msg)
resp, _ = reader.ReadString('\n')
resp = strings.TrimSpace(resp)
}
return
}
// validateName: Cannot be empty
func validateName(name string) bool {
return name != ""
}
// validateUserName: Cannot be empty, only one path level
func validateUserName(username string) bool {
return username != "" && !strings.Contains(username, "/") && !strings.Contains(username, " ")
}
// validateHost: Can only be GITHUB, BITBUCKET OR GOOGLE
func validateHost(host string) bool {
hosts := []string{GITHUB, BITBUCKET, GOOGLE}
for _, h := range hosts {
if strings.EqualFold(host, h) {
return true
}
}
return false
}
// validateEmail: Must have a correct email format
func validateEmail(email string) bool {
schema := valid.NewSchema(0)
_, err := valid.Email(schema, email)
if err != nil {
return false
}
return true
}
// validateLicense: Must be one of the supported licenses
func validateLicense(license string) bool {
for _, l := range licenses {
if strings.EqualFold(license, l) {
return true
}
}
return false
}