generate falgs based on struct options instead of defining them externally

This commit is contained in:
zlji 2017-01-09 22:06:46 +08:00 committed by Iwasaki Yudai
parent c4ed7bc415
commit d71e2fcfa8
6 changed files with 218 additions and 231 deletions

61
main.go
View file

@ -9,6 +9,7 @@ import (
"github.com/codegangsta/cli"
"github.com/yudai/gotty/app"
"github.com/yudai/gotty/utils"
)
func main() {
@ -18,41 +19,12 @@ func main() {
cmd.Usage = "Share your terminal as a web application"
cmd.HideHelp = true
flags := []flag{
flag{"address", "a", "IP address to listen"},
flag{"port", "p", "Port number to listen"},
flag{"permit-write", "w", "Permit clients to write to the TTY (BE CAREFUL)"},
flag{"credential", "c", "Credential for Basic Authentication (ex: user:pass, default disabled)"},
flag{"random-url", "r", "Add a random string to the URL"},
flag{"random-url-length", "", "Random URL length"},
flag{"tls", "t", "Enable TLS/SSL"},
flag{"tls-crt", "", "TLS/SSL certificate file path"},
flag{"tls-key", "", "TLS/SSL key file path"},
flag{"tls-ca-crt", "", "TLS/SSL CA certificate file for client certifications"},
flag{"index", "", "Custom index.html file"},
flag{"title-format", "", "Title format of browser window"},
flag{"reconnect", "", "Enable reconnection"},
flag{"reconnect-time", "", "Time to reconnect"},
flag{"timeout", "", "Timeout seconds for waiting a client (0 to disable)"},
flag{"max-connection", "", "Maximum connection to gotty, 0(default) means no limit"},
flag{"once", "", "Accept only one client and exit on disconnection"},
flag{"permit-arguments", "", "Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB)"},
flag{"close-signal", "", "Signal sent to the command process when gotty close it (default: SIGHUP)"},
flag{"width", "", "Static width of the screen, 0(default) means dynamically resize"},
flag{"height", "", "Static height of the screen, 0(default) means dynamically resize"},
options := &app.Options{}
if err := utils.ApplyDefaultValues(options); err != nil {
exit(err, 1)
}
mappingHint := map[string]string{
"index": "IndexFile",
"tls": "EnableTLS",
"tls-crt": "TLSCrtFile",
"tls-key": "TLSKeyFile",
"tls-ca-crt": "TLSCACrtFile",
"random-url": "EnableRandomUrl",
"reconnect": "EnableReconnect",
}
cliFlags, err := generateFlags(flags, mappingHint)
cliFlags, flagMappings, err := utils.GenerateFlags(options)
if err != nil {
exit(err, 3)
}
@ -69,35 +41,28 @@ func main() {
cmd.Action = func(c *cli.Context) {
if len(c.Args()) == 0 {
fmt.Println("Error: No command given.\n")
cli.ShowAppHelp(c)
exit(err, 1)
exit(fmt.Errorf("Error: No command given."), 1)
}
options := app.DefaultOptions
configFile := c.String("config")
_, err := os.Stat(app.ExpandHomeDir(configFile))
_, err := os.Stat(utils.ExpandHomeDir(configFile))
if configFile != "~/.gotty" || !os.IsNotExist(err) {
if err := app.ApplyConfigFile(&options, configFile); err != nil {
if err := utils.ApplyConfigFile(configFile, options); err != nil {
exit(err, 2)
}
}
applyFlags(&options, flags, mappingHint, c)
utils.ApplyFlags(cliFlags, flagMappings, c, options)
if c.IsSet("credential") {
options.EnableBasicAuth = true
}
if c.IsSet("tls-ca-crt") {
options.EnableTLSClientAuth = true
}
options.EnableBasicAuth = c.IsSet("credential")
options.EnableTLSClientAuth = c.IsSet("tls-ca-crt")
if err := app.CheckConfig(&options); err != nil {
if err := app.CheckConfig(options); err != nil {
exit(err, 6)
}
app, err := app.New(c.Args(), &options)
app, err := app.New(c.Args(), options)
if err != nil {
exit(err, 3)
}