Tweaks to the CLI to ensure consistent behavior with previous versions

This commit is contained in:
Toby 2021-05-08 16:07:04 -07:00
parent 3f5bd76480
commit 060948188e

View file

@ -22,66 +22,24 @@ var (
func main() { func main() {
app := &cli.App{ app := &cli.App{
Name: "hysteria", Name: "Hysteria",
Usage: "A TCP/UDP relay & SOCKS5/HTTP proxy tool", Usage: "a TCP/UDP relay & SOCKS5/HTTP proxy tool optimized for poor network environments",
Version: fmt.Sprintf("%s %s %s", appVersion, appDate, appCommit), Version: fmt.Sprintf("%s %s %s", appVersion, appDate, appCommit),
Authors: []*cli.Author{{Name: "HyNetwork <https://github.com/HyNetwork>"}}, Authors: []*cli.Author{{Name: "HyNetwork <https://github.com/HyNetwork>"}},
EnableBashCompletion: true, EnableBashCompletion: true,
Action: func(c *cli.Context) error { Action: clientAction,
return cli.ShowAppHelp(c) Flags: commonFlags(),
}, Before: initApp,
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "server", Name: "server",
Usage: "Run as server mode", Usage: "Run as server mode",
Before: initApp, Action: serverAction,
Flags: commonFlags(),
Action: func(c *cli.Context) error {
cbs, err := ioutil.ReadFile(c.String("config"))
if err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err,
}).Fatal("Failed to read configuration")
}
// server mode
sc, err := parseServerConfig(cbs)
if err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err,
}).Fatal("Failed to parse server configuration")
}
server(sc)
return nil
},
}, },
{ {
Name: "client", Name: "client",
Usage: "Run as client mode", Usage: "Run as client mode",
Before: initApp, Action: clientAction,
Flags: commonFlags(),
Action: func(c *cli.Context) error {
cbs, err := ioutil.ReadFile(c.String("config"))
if err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err,
}).Fatal("Failed to read configuration")
}
// client mode
cc, err := parseClientConfig(cbs)
if err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err,
}).Fatal("Failed to parse client configuration")
}
client(cc)
return nil
},
}, },
}, },
} }
@ -93,6 +51,46 @@ func main() {
} }
func clientAction(c *cli.Context) error {
cbs, err := ioutil.ReadFile(c.String("config"))
if err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err,
}).Fatal("Failed to read configuration")
}
// client mode
cc, err := parseClientConfig(cbs)
if err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err,
}).Fatal("Failed to parse client configuration")
}
client(cc)
return nil
}
func serverAction(c *cli.Context) error {
cbs, err := ioutil.ReadFile(c.String("config"))
if err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err,
}).Fatal("Failed to read configuration")
}
// server mode
sc, err := parseServerConfig(cbs)
if err != nil {
logrus.WithFields(logrus.Fields{
"file": c.String("config"),
"error": err,
}).Fatal("Failed to parse server configuration")
}
server(sc)
return nil
}
func parseServerConfig(cb []byte) (*serverConfig, error) { func parseServerConfig(cb []byte) (*serverConfig, error) {
var c serverConfig var c serverConfig
err := json5.Unmarshal(cb, &c) err := json5.Unmarshal(cb, &c)