forked from parse-community/parse-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (109 loc) · 2.59 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
package main
import (
"fmt"
"io"
"os"
"os/signal"
"runtime"
"github.com/facebookgo/clock"
"github.com/spf13/cobra"
)
const (
version = "2.2.7"
cloudDir = "cloud"
hostingDir = "public"
defaultBaseURL = "https://api.parse.com/1/"
)
var userAgent = fmt.Sprintf("parse-cli-%s-%s", runtime.GOOS, version)
type env struct {
Root string // project root
Server string // parse api server
Type int // project type
ParserEmail string // email associated with developer parse account
ErrorStack bool
Out io.Writer
Err io.Writer
In io.Reader
Exit func(int)
Clock clock.Clock
ParseAPIClient *ParseAPIClient
}
type client struct {
Config config
AppName string
AppConfig appConfig
}
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 := 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 := getProjectRoot(&e, cur)
if isProjectDir(root) {
e.Root = root
config, err := 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 = legacyParseFormat
e.Root = getLegacyProjectRoot(&e, cur)
}
}
if e.Type != legacyParseFormat && e.Type != parseFormat {
fmt.Fprintf(e.Err, "Unknown project type %d.\n", e.Type)
os.Exit(1)
}
if e.Server == "" {
e.Server = defaultBaseURL
}
apiClient, err := newParseAPIClient(&e)
if err != nil {
fmt.Fprintln(e.Err, err)
os.Exit(1)
}
e.ParseAPIClient = apiClient
var rootCmd *cobra.Command
switch e.Type {
case legacyParseFormat, parseFormat:
rootCmd = parseRootCmd(&e)
}
message, err := checkIfSupported(&e, version, os.Args[1:]...)
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)
}
}