Improve command interface.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-05-31 14:52:18 +08:00
parent 77fc6699bd
commit 4239980dc6
1 changed files with 48 additions and 37 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/appleboy/gorush/config" "github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/gorush" "github.com/appleboy/gorush/gorush"
"log" "log"
"os"
) )
func checkInput(token, message string) { func checkInput(token, message string) {
@ -20,26 +21,37 @@ func checkInput(token, message string) {
var Version = "No Version Provided" var Version = "No Version Provided"
func main() { func main() {
version := flag.Bool("v", false, "gorush version") opts := config.ConfYaml{}
confPath := flag.String("c", "", "yaml configuration file path for gorush")
certificateKeyPath := flag.String("i", "", "iOS certificate key file path for gorush") var showVersion bool
certificatePassword := flag.String("password", "", "iOS certificate password for gorush") var configFile string
apiKey := flag.String("k", "", "Android api key configuration for gorush") var topic string
port := flag.String("p", "", "port number for gorush") var message string
token := flag.String("t", "", "token string") var token string
message := flag.String("m", "", "notification message")
android := flag.Bool("android", false, "send android notification") flag.BoolVar(&showVersion, "version", false, "Print version information.")
ios := flag.Bool("ios", false, "send ios notification") flag.BoolVar(&showVersion, "v", false, "Print version information.")
production := flag.Bool("production", false, "production mode in iOS") flag.StringVar(&configFile, "c", "", "Configuration file.")
topic := flag.String("topic", "", "apns topic in iOS") flag.StringVar(&configFile, "config", "", "Configuration file.")
flag.StringVar(&opts.Ios.PemPath, "i", "", "iOS certificate key file path")
flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush")
flag.StringVar(&opts.Android.APIKey, "k", "", "Android api key configuration for gorush")
flag.StringVar(&opts.Core.Port, "p", "", "port number for gorush")
flag.StringVar(&token, "t", "", "token string")
flag.StringVar(&message, "m", "", "notification message")
flag.BoolVar(&opts.Android.Enabled, "android", false, "send android notification")
flag.BoolVar(&opts.Ios.Enabled, "ios", false, "send ios notification")
flag.BoolVar(&opts.Ios.Production, "production", false, "production mode in iOS")
flag.StringVar(&topic, "topic", "", "apns topic in iOS")
flag.Parse() flag.Parse()
gorush.SetVersion(Version) gorush.SetVersion(Version)
if *version { // Show version and exit
if showVersion {
gorush.PrintGoRushVersion() gorush.PrintGoRushVersion()
return os.Exit(0)
} }
var err error var err error
@ -48,8 +60,8 @@ func main() {
gorush.PushConf = config.BuildDefaultPushConf() gorush.PushConf = config.BuildDefaultPushConf()
// load user define config. // load user define config.
if *confPath != "" { if configFile != "" {
gorush.PushConf, err = config.LoadConfYaml(*confPath) gorush.PushConf, err = config.LoadConfYaml(configFile)
if err != nil { if err != nil {
log.Printf("Load yaml config file error: '%v'", err) log.Printf("Load yaml config file error: '%v'", err)
@ -58,22 +70,21 @@ func main() {
} }
} }
if *certificateKeyPath != "" { if opts.Ios.PemPath != "" {
gorush.PushConf.Ios.PemPath = *certificateKeyPath gorush.PushConf.Ios.PemPath = opts.Ios.PemPath
} }
if *certificatePassword != "" { if opts.Ios.Password != "" {
log.Printf("Load yaml config file error: '%v'", certificatePassword) gorush.PushConf.Ios.Password = opts.Ios.Password
gorush.PushConf.Ios.Password = *certificatePassword
} }
if *apiKey != "" { if opts.Android.APIKey != "" {
gorush.PushConf.Android.APIKey = *apiKey gorush.PushConf.Android.APIKey = opts.Android.APIKey
} }
// overwrite server port // overwrite server port
if *port != "" { if opts.Core.Port != "" {
gorush.PushConf.Core.Port = *port gorush.PushConf.Core.Port = opts.Core.Port
} }
if err = gorush.InitLog(); err != nil { if err = gorush.InitLog(); err != nil {
@ -83,12 +94,12 @@ func main() {
} }
// send android notification // send android notification
if *android { if opts.Android.Enabled {
gorush.PushConf.Android.Enabled = true gorush.PushConf.Android.Enabled = opts.Android.Enabled
req := gorush.PushNotification{ req := gorush.PushNotification{
Tokens: []string{*token}, Tokens: []string{token},
Platform: gorush.PlatFormAndroid, Platform: gorush.PlatFormAndroid,
Message: *message, Message: message,
} }
err := gorush.CheckMessage(req) err := gorush.CheckMessage(req)
@ -104,20 +115,20 @@ func main() {
} }
// send android notification // send android notification
if *ios { if opts.Ios.Enabled {
if *production { if opts.Ios.Production {
gorush.PushConf.Ios.Production = true gorush.PushConf.Ios.Production = opts.Ios.Production
} }
gorush.PushConf.Ios.Enabled = true gorush.PushConf.Ios.Enabled = opts.Ios.Enabled
req := gorush.PushNotification{ req := gorush.PushNotification{
Tokens: []string{*token}, Tokens: []string{token},
Platform: gorush.PlatFormIos, Platform: gorush.PlatFormIos,
Message: *message, Message: message,
} }
if *topic != "" { if topic != "" {
req.Topic = *topic req.Topic = topic
} }
err := gorush.CheckMessage(req) err := gorush.CheckMessage(req)