/
init.go
162 lines (142 loc) · 3.74 KB
/
init.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
package commands
import (
"appstax-cli/appstax/account"
"appstax-cli/appstax/config"
"appstax-cli/appstax/fail"
"appstax-cli/appstax/log"
"appstax-cli/appstax/template"
"appstax-cli/appstax/term"
"fmt"
"github.com/codegangsta/cli"
"os"
"strings"
)
func DoInit(c *cli.Context) {
useOptions(c)
loginIfNeeded()
app, err := selectApp()
if err != nil {
return
}
tpl := selectTemplate()
publicDir := "./public"
serverDir := "./server"
writeConfig(app, publicDir, serverDir)
if !strings.HasPrefix(tpl.Name, "ios/") {
createPublicDir()
}
createServerDir()
if tpl.Name != "none" {
term.Section()
term.Println("Setting up template... ")
template.Install(tpl)
term.Println("Done.")
}
if !strings.HasPrefix(tpl.Name, "ios/") {
term.Section()
selectSubdomain(app.AppID, false)
}
term.Section()
term.Println("All done!")
if !strings.HasPrefix(tpl.Name, "ios/") {
term.Println("Now run 'appstax deploy' when you are ready to upload your files.")
}
}
func selectApp() (account.App, error) {
apps, _ := account.GetUserApps()
selected := -1
if len(apps) == 0 {
term.Section()
term.Println("You have not created any apps yet!")
term.Println("Log in on https://appstax.com and create one before you proceed.")
} else {
term.Section()
term.Println("Choose which app to configure:")
for i, app := range apps {
term.Printf(" %d) %s\n", i+1, app.AppName)
}
term.Section()
for selected < 0 || selected >= len(apps) {
selected = -1 + term.GetInt(fmt.Sprintf("Please select (1-%d)", len(apps)))
}
}
return apps[selected], nil
}
func selectTemplate() template.Template {
templates := template.All()
term.Section()
term.Println("Choose a template for you app:")
for i, template := range templates {
term.Printf(" %d) %s\n", i+1, template.Label)
}
term.Section()
for {
selected := -1 + term.GetInt(fmt.Sprintf("Please select (1-%d)", len(templates)))
if selected >= 0 && selected < len(templates) {
return templates[selected]
}
}
}
func selectPublicDir() string {
dir := term.GetString("Select deployable directory [default: ./public]")
if dir == "" {
dir = "./public"
}
return dir
}
func selectSubdomainIfNeeded() {
app, err := account.GetCurrentApp()
fail.Handle(err)
if app.HostingSubdomain == "" {
selectSubdomain(app.AppID, true)
}
}
func selectSubdomain(appID string, required bool) {
app, _ := account.GetAppByID(appID)
log.Debugf("Subdomain app: %v", app)
for {
if required {
app.HostingSubdomain = term.GetString("Choose a *.appstax.io subdomain for hosting")
} else {
term.Println("Choose a *.appstax.io subdomain for hosting:")
term.Println("(Leave this blank if you wish to decide this later.)")
app.HostingSubdomain = term.GetString("Subdomain")
}
if app.HostingSubdomain != "" {
err1 := account.SaveApp(app)
if err1 != nil {
term.Println(err1.Error())
}
err2 := account.AddCorsOrigin(appID, fmt.Sprintf("http://%s.appstax.io", app.HostingSubdomain))
if err2 != nil {
term.Println(err2.Error())
}
if err1 == nil && err2 == nil {
term.Printf("Successfully configured %s.appstax.io\n", app.HostingSubdomain)
return
}
} else if !required {
return
}
}
}
func createPublicDir() {
dir := config.Read().PublicDir
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0700)
fail.Handle(err)
log.Debugf("Created public directory '%s'", dir)
} else {
log.Debugf("Not creating public directory. '%s' already exists.", dir)
}
}
func createServerDir() {
dir := config.Read().ServerDir
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0700)
fail.Handle(err)
log.Debugf("Created server directory '%s'", dir)
} else {
log.Debugf("Not creating server directory. '%s' already exists.", dir)
}
}