Skip to content

Commit 7e8ea60

Browse files
committed
update cli input
1 parent ac0de44 commit 7e8ea60

File tree

5 files changed

+318
-124
lines changed

5 files changed

+318
-124
lines changed

cli/cli.go

Lines changed: 84 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import (
1010
"github.com/larisgo/laravel-echo-server/errors"
1111
"github.com/larisgo/laravel-echo-server/log"
1212
"github.com/larisgo/laravel-echo-server/options"
13+
"github.com/larisgo/laravel-echo-server/std"
1314
"github.com/larisgo/laravel-echo-server/types"
14-
"github.com/tcnksm/go-input"
1515
"io/ioutil"
1616
"os"
1717
"os/signal"
1818
"path"
1919
"path/filepath"
20+
"regexp"
2021
"strings"
2122
"syscall"
2223
"time"
@@ -143,143 +144,105 @@ func (this *Cli) resolveEnvFileOptions(config options.Config, args *Args) option
143144
func (this *Cli) setupConfig(defaultFile string) (options.Config, string) {
144145
config := this.defaultOptions
145146

146-
ui := &input.UI{
147-
Writer: os.Stdout,
148-
Reader: os.Stdin,
149-
}
150-
if devMode, err := ui.Select("Do you want to run this server in development mode?", []string{"true", "false"}, &input.Options{
151-
Default: "false",
152-
Loop: true,
153-
}); err != nil {
154-
log.Fatal(err)
155-
} else {
156-
config.DevMode = map[string]bool{
157-
"true": true,
158-
"false": false,
159-
}[devMode]
160-
}
161-
if port, err := ui.Ask("Which port would you like to serve from?", &input.Options{
162-
Default: "6001",
163-
Loop: true,
164-
}); err != nil {
165-
log.Fatal(err)
166-
} else {
167-
config.Port = port
168-
}
169-
170-
if database, err := ui.Select("Which database would you like to use to store presence channel members?", []string{"redis", "sqlite"}, &input.Options{
171-
Default: "sqlite",
172-
Loop: true,
173-
}); err != nil {
174-
log.Fatal(err)
175-
} else {
176-
config.Database = database
177-
}
178-
179-
if authHost, err := ui.Ask("Enter the host of your Laravel authentication server.", &input.Options{
180-
Default: "http://localhost",
181-
Loop: true,
182-
}); err != nil {
183-
log.Fatal(err)
184-
} else {
185-
config.AuthHost = authHost
186-
}
147+
input := std.NewDefaultInput(os.Stdin, std.NewDefaultOutput(os.Stdout))
187148

188-
protocol, err := ui.Select("Will you be serving on http or https?", []string{"http", "https"}, &input.Options{
189-
Default: "http",
190-
Loop: true,
191-
})
192-
if err != nil {
193-
log.Fatal(err)
149+
if config.DevMode = input.Confirm("Do you want to run this server in development mode?", false); config.DevMode {
150+
log.Success("Yes")
194151
} else {
195-
config.Protocol = protocol
152+
log.Success("No")
196153
}
197154

198-
if protocol == "https" {
199-
if sslCertPath, err := ui.Ask("Enter the path to your SSL cert file.", &input.Options{
200-
Required: true,
201-
Loop: true,
202-
}); err != nil {
203-
log.Fatal(err)
155+
config.Port = input.Ask("Which port would you like to serve from:", func(v string) error {
156+
if (len(v) == 0) || regexp.MustCompile(`^([1-9]|[1-9]\d{1,3}|[1-6][0-5][0-5][0-3][0-5])$`).MatchString(v) {
157+
return nil
204158
} else {
205-
config.SslCertPath = sslCertPath
159+
return errors.NewError("Port numbers range from 1 to 65535")
206160
}
161+
}, "6001")
162+
log.Success(config.Port)
163+
164+
config.Database = input.Choose("Which database would you like to use to store presence channel members?", map[string]string{
165+
"redis": "Use redis to store.",
166+
"sqlite": "Use sqlite to store.",
167+
}, "sqlite")
168+
log.Success(config.Database)
169+
170+
config.AuthHost = input.Ask("Enter the host of your Laravel authentication server:", func(_ string) error {
171+
return nil
172+
}, "http://localhost")
173+
log.Success(config.AuthHost)
174+
175+
config.Protocol = input.Choose("Will you be serving on http or https?", map[string]string{
176+
"http": "Run the service using http.",
177+
"https": "Run the service using https.",
178+
}, "http")
179+
log.Success(config.Protocol)
180+
181+
if config.Protocol == "https" {
182+
config.SslCertPath = input.Ask("Enter the path to your SSL cert file:", func(v string) error {
183+
if len(v) > 0 {
184+
return nil
185+
} else {
186+
return errors.NewError("Please enter ssl Cert Path.")
187+
}
188+
})
189+
log.Success(config.SslCertPath)
190+
191+
config.SslKeyPath = input.Ask("Enter the path to your SSL key file:", func(v string) error {
192+
if len(v) > 0 {
193+
return nil
194+
} else {
195+
return errors.NewError("Please enter ssl Key Path.")
196+
}
197+
})
198+
log.Success(config.SslKeyPath)
199+
}
207200

208-
if sslKeyPath, err := ui.Ask("Enter the path to your SSL key file.", &input.Options{
209-
Required: true,
210-
Loop: true,
211-
}); err != nil {
212-
log.Fatal(err)
213-
} else {
214-
config.SslKeyPath = sslKeyPath
201+
if input.Confirm("Do you want to generate a client ID/Key for HTTP API?", false) {
202+
log.Success("Yes")
203+
client := options.Client{
204+
AppId: this.createAppId(),
205+
Key: this.createApiKey(),
215206
}
216-
}
207+
if len(config.Clients) == 0 {
208+
config.Clients = []options.Client{}
209+
}
210+
config.Clients = append(config.Clients, client)
217211

218-
if addClient, err := ui.Select("Do you want to generate a client ID/Key for HTTP API?", []string{"true", "false"}, &input.Options{
219-
Default: "false",
220-
Loop: true,
221-
}); err != nil {
222-
log.Fatal(err)
212+
log.Info(fmt.Sprintf("appId: %s", client.AppId))
213+
log.Info(fmt.Sprintf("key: %s", client.Key))
223214
} else {
224-
if map[string]bool{"true": true, "false": false}[addClient] {
225-
client := options.Client{
226-
AppId: this.createAppId(),
227-
Key: this.createApiKey(),
228-
}
229-
if len(config.Clients) == 0 {
230-
config.Clients = []options.Client{}
231-
}
232-
config.Clients = append(config.Clients, client)
233-
// log.Info(fmt.Sprintf("appId: %s", client.AppId))
234-
// log.Info(fmt.Sprintf("key: %s", client.Key))
235-
}
215+
log.Success("No")
236216
}
237217

238-
if corsAllow, err := ui.Select("Do you want to setup cross domain access to the API?", []string{"true", "false"}, &input.Options{
239-
Default: "false",
240-
Loop: true,
241-
}); err != nil {
242-
log.Fatal(err)
218+
if config.ApiOriginAllow.AllowCors = input.Confirm("Do you want to setup cross domain access to the API?", false); config.ApiOriginAllow.AllowCors {
219+
log.Success("Yes")
243220
} else {
244-
config.ApiOriginAllow.AllowCors = map[string]bool{
245-
"true": true,
246-
"false": false,
247-
}[corsAllow]
221+
log.Success("No")
248222
}
249223

250224
if config.ApiOriginAllow.AllowCors {
251-
if allowOrigin, err := ui.Ask("Specify the URI that may access the API:", &input.Options{
252-
Default: "http://localhost:80",
253-
Loop: true,
254-
}); err != nil {
255-
log.Fatal(err)
256-
} else {
257-
config.ApiOriginAllow.AllowOrigin = allowOrigin
258-
}
259-
if allowMethods, err := ui.Ask("Enter the HTTP methods that are allowed for CORS:", &input.Options{
260-
Default: "GET, POST",
261-
Loop: true,
262-
}); err != nil {
263-
log.Fatal(err)
264-
} else {
265-
config.ApiOriginAllow.AllowMethods = allowMethods
266-
}
267-
if allowHeaders, err := ui.Ask("Enter the HTTP headers that are allowed for CORS:", &input.Options{
268-
Default: "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id",
269-
Loop: true,
270-
}); err != nil {
271-
log.Fatal(err)
272-
} else {
273-
config.ApiOriginAllow.AllowHeaders = allowHeaders
274-
}
275-
}
276-
file, err := ui.Ask("What do you want this config to be saved as?", &input.Options{
277-
Default: defaultFile,
278-
Loop: true,
279-
})
280-
if err != nil {
281-
log.Fatal(err)
225+
config.ApiOriginAllow.AllowOrigin = input.Ask("Specify the URI that may access the API:", func(_ string) error {
226+
return nil
227+
}, "http://localhost:80")
228+
log.Success(config.ApiOriginAllow.AllowOrigin)
229+
230+
config.ApiOriginAllow.AllowMethods = input.Ask("Enter the HTTP methods that are allowed for CORS:", func(_ string) error {
231+
return nil
232+
}, "GET, POST")
233+
log.Success(config.ApiOriginAllow.AllowMethods)
234+
235+
config.ApiOriginAllow.AllowHeaders = input.Ask("Enter the HTTP headers that are allowed for CORS:", func(_ string) error {
236+
return nil
237+
}, "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id")
238+
log.Success(config.ApiOriginAllow.AllowHeaders)
282239
}
240+
241+
file := input.Ask("What do you want this config to be saved as:", func(_ string) error {
242+
return nil
243+
}, defaultFile)
244+
log.Success(file)
245+
283246
return config, file
284247
}
285248

go.mod

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ replace (
88
)
99

1010
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
1112
github.com/go-redis/redis v6.15.5+incompatible
1213
github.com/gookit/color v1.2.0
1314
github.com/gorilla/websocket v1.4.1 // indirect
1415
github.com/joho/godotenv v1.3.0
1516
github.com/julienschmidt/httprouter v1.2.0
1617
github.com/mattn/go-sqlite3 v1.11.0
18+
github.com/onsi/ginkgo v1.10.1 // indirect
19+
github.com/onsi/gomega v1.7.0 // indirect
1720
github.com/pschlump/json v1.12.0 // indirect
1821
github.com/pschlump/socketio v0.0.0-00010101000000-000000000000
19-
github.com/tcnksm/go-input v0.0.0-00010101000000-000000000000
20-
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7 // indirect
22+
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
23+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 // indirect
24+
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
2125
)

0 commit comments

Comments
 (0)