forked from parse-community/parse-cli
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
128 lines (115 loc) · 2.82 KB
/
main.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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"time"
"github.com/back4app/parse-cli/parsecli"
"github.com/facebookgo/clock"
"github.com/facebookgo/stackerr"
)
func main() {
// some parts of apps.go are unable to handle
// interrupts, this logic ensures we exit on system interrupts
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
go func() {
<-interrupt
os.Exit(1)
}()
e := parsecli.Env{
Root: os.Getenv("PARSE_ROOT"),
Server: os.Getenv("PARSE_SERVER"),
ErrorStack: os.Getenv("PARSE_ERROR_STACK") == "1",
ParserEmail: os.Getenv("PARSER_EMAIL"),
Out: os.Stdout,
Err: os.Stderr,
In: os.Stdin,
Exit: os.Exit,
Clock: clock.New(),
}
if e.Root == "" {
cur, err := os.Getwd()
if err != nil {
fmt.Fprintf(e.Err, "Failed to get current directory:\n%s\n", err)
os.Exit(1)
}
root := parsecli.GetProjectRoot(&e, cur)
if parsecli.IsProjectDir(root) {
e.Root = root
config, err := parsecli.ConfigFromDir(root)
if err != nil {
fmt.Fprintln(e.Err, err)
os.Exit(1)
}
e.Type = config.GetProjectConfig().Type
if e.ParserEmail == "" {
e.ParserEmail = config.GetProjectConfig().ParserEmail
}
} else {
e.Type = parsecli.LegacyParseFormat
e.Root = parsecli.GetLegacyProjectRoot(&e, cur)
}
}
if e.Type != parsecli.LegacyParseFormat && e.Type != parsecli.ParseFormat {
fmt.Fprintf(e.Err, "Unknown project type %d.\n", e.Type)
os.Exit(1)
}
if e.Server == "" {
e.Server = parsecli.DefaultBaseURL
}
apiClient, err := parsecli.NewParseAPIClient(&e)
if err != nil {
fmt.Fprintln(e.Err, err)
os.Exit(1)
}
e.ParseAPIClient = apiClient
mode := "parse"
command, rootCmd := parseRootCmd(&e)
if len(command) == 0 || command[0] != "update" {
message, err := checkIfSupported(&e, parsecli.Version, mode, command...)
if err != nil {
fmt.Fprintln(e.Err, err)
os.Exit(1)
}
if message != "" {
fmt.Fprintln(e.Err, message)
}
}
if err := rootCmd.Execute(); err != nil {
// Error is already printed in Execute()
os.Exit(1)
}
}
func checkIfSupported(e *parsecli.Env, version string, mode string, args ...string) (string, error) {
v := make(url.Values)
v.Set("version", version)
v.Set("mode", mode)
v.Set("other", strings.Join(args, " "))
req := &http.Request{
Method: "GET",
URL: &url.URL{Path: "supported", RawQuery: v.Encode()},
}
type result struct {
warning string
err error
}
timeout := make(chan *result, 1)
go func() {
var res struct {
Warning string `json:"warning"`
}
_, err := e.ParseAPIClient.Do(req, nil, &res)
timeout <- &result{warning: res.Warning, err: err}
}()
select {
case res := <-timeout:
return res.warning, stackerr.Wrap(res.err)
case <-time.After(time.Second):
return "", nil
}
return "", nil
}