This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathutils.go
208 lines (187 loc) · 4.62 KB
/
utils.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package parsecli
import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/facebookgo/errgroup"
"github.com/facebookgo/parse"
"github.com/facebookgo/stackerr"
)
const (
SampleSource = `
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("hello", function(request, response) {
response.success("Hello world!");
});
`
SampleHTML = `
<html>
<head>
<title>My ParseApp site</title>
<style>
body { font-family: Helvetica, Arial, sans-serif; }
div { width: 800px; height: 400px; margin: 40px auto; padding: 20px; border: 2px solid #5298fc; }
h1 { font-size: 30px; margin: 0; }
p { margin: 40px 0; }
em { font-family: monospace; }
a { color: #5298fc; text-decoration: none; }
</style>
</head>
<body>
<div>
<h1>Congratulations! You're already hosting with Parse.</h1>
<p>To get started, edit this file at <em>public/index.html</em> and start adding static content.</p>
<p>If you want something a bit more dynamic, delete this file and check out <a href="https://parse.com/docs/hosting_guide#webapp">our hosting docs</a>.</p>
</div>
</body>
</html>
`
)
func getHostFromURL(urlStr, email string) (string, error) {
netURL, err := url.Parse(urlStr)
if err != nil {
return "", stackerr.Wrap(err)
}
server := regexp.MustCompile(`(.*):\d+$`).ReplaceAllString(netURL.Host, "$1")
if server == "" {
return "", stackerr.Newf("%s is not a valid url", urlStr)
}
if email != "" {
return fmt.Sprintf("%s#%s", server, email), nil
}
return server, nil
}
func Last4(str string) string {
l := len(str)
if l > 4 {
return fmt.Sprintf("%s%s", strings.Repeat("*", l-4), str[l-4:l])
}
return str
}
// errorString returns the error string with our without the stack trace
// depending on the Environment variable. this exists because we want plain
// messages for end users, but when we're working on the CLI we want the stack
// trace for debugging.
func ErrorString(e *Env, err error) string {
type hasUnderlying interface {
HasUnderlying() error
}
parseErr := func(err error) error {
if apiErr, ok := err.(*parse.Error); ok {
return errors.New(apiErr.Message)
}
return err
}
lastErr := func(err error) error {
if serr, ok := err.(*stackerr.Error); ok {
if errs := stackerr.Underlying(serr); len(errs) != 0 {
err = errs[len(errs)-1]
}
} else {
if eu, ok := err.(hasUnderlying); ok {
err = eu.HasUnderlying()
}
}
return parseErr(err)
}
if !e.ErrorStack {
if merr, ok := err.(errgroup.MultiError); ok {
var multiError []error
for _, ierr := range []error(merr) {
multiError = append(multiError, lastErr(ierr))
}
err = errgroup.MultiError(multiError)
} else {
err = lastErr(err)
}
return parseErr(err).Error()
}
return err.Error()
}
func CreateConfigWithContent(path, content string) error {
file, err := os.OpenFile(
path,
os.O_RDWR|os.O_CREATE|os.O_TRUNC,
0600,
)
if err != nil && !os.IsExist(err) {
return stackerr.Wrap(err)
}
defer file.Close()
if _, err := file.WriteString(content); err != nil {
return stackerr.Wrap(err)
}
if err := file.Close(); err != nil {
return stackerr.Wrap(err)
}
return nil
}
var NewProjectFiles = []struct {
Dirname, Filename, Content string
}{
{"cloud", "main.js", SampleSource},
{"public", "index.html", SampleHTML},
}
func CloneSampleCloudCode(e *Env, dumpTemplate bool) error {
err := os.MkdirAll(e.Root, 0755)
if err != nil {
return stackerr.Wrap(err)
}
err = CreateConfigWithContent(
filepath.Join(e.Root, ParseProject),
fmt.Sprintf(
`{
"project_type" : %d,
"parse": {"jssdk":""}
}`,
ParseFormat,
),
)
if err != nil {
return err
}
err = CreateConfigWithContent(
filepath.Join(e.Root, ParseLocal),
"{}",
)
if err != nil {
return err
}
// no need to set up the template code
if !dumpTemplate {
return nil
}
for _, info := range NewProjectFiles {
sampleDir := filepath.Join(e.Root, info.Dirname)
if _, err := os.Stat(sampleDir); err != nil {
if !os.IsNotExist(err) {
return stackerr.Wrap(err)
}
if err := os.Mkdir(sampleDir, 0755); err != nil {
return stackerr.Wrap(err)
}
}
sampleFile := filepath.Join(sampleDir, info.Filename)
if _, err := os.Stat(sampleFile); err != nil {
if os.IsNotExist(err) {
file, err := os.Create(sampleFile)
if err != nil && !os.IsExist(err) {
return stackerr.Wrap(err)
}
defer file.Close()
if _, err := file.WriteString(info.Content); err != nil {
return stackerr.Wrap(err)
}
if err := file.Close(); err != nil {
return stackerr.Wrap(err)
}
}
}
}
return nil
}