From a38bf37dcd7db6ba594afc16114922a1b35e0320 Mon Sep 17 00:00:00 2001 From: Avner Cohen Date: Thu, 21 May 2020 10:47:13 +0300 Subject: [PATCH] Add ability to set a default priority for cli client, fix #2 --- README.md | 4 +++- command/config.go | 1 + command/initialize.go | 21 +++++++++++++++++++-- command/push.go | 4 ++++ config/config.go | 7 ++++--- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fd3e9e6..53ea818 100644 --- a/README.md +++ b/README.md @@ -151,13 +151,15 @@ Gotify-CLI will search the following paths for a config file: | ----- | ----------- | ------- | | token | an application token (a client token will not work) | `A4ZudDRdLT40L5X` | | url | the URL to your [gotify/server][gotify/server] | `https://gotify.example.com` | +| defaultPriority | Default priority ( set to 0 if not present) | `6` | ### Config example ```json { "token": "A4ZudDRdLT40L5X", - "url": "https://gotify.example.com" + "url": "https://gotify.example.com", + "defaultPriority": 6 } ``` diff --git a/command/config.go b/command/config.go index 3ff349d..0fa4be7 100644 --- a/command/config.go +++ b/command/config.go @@ -21,6 +21,7 @@ func Config() cli.Command { } fmt.Println("Used Config:", conf.FromLocation) fmt.Println("URL:", conf.URL) + fmt.Println("Default Priority:", conf.DefaultPriority) fmt.Println("Token:", conf.Token) }, } diff --git a/command/initialize.go b/command/initialize.go index 24c2777..eca19f8 100644 --- a/command/initialize.go +++ b/command/initialize.go @@ -35,10 +35,13 @@ func doInit(ctx *cli.Context) { hr() token := inputToken(gotify.NewClient(serverURL, utils.CreateHTTPClient())) hr() + defaultPriority := inputDefaultPriority() + hr() conf := &config.Config{ - URL: serverURL.String(), - Token: token, + URL: serverURL.String(), + Token: token, + DefaultPriority: defaultPriority, } pathToWrite, err := config.ExistingConfig(config.GetLocations()) @@ -204,6 +207,20 @@ func inputRawToken(gotify *api.GotifyREST) string { } +func inputDefaultPriority() int { + for { + defaultPriorityStr := inputString("Default Priority [0-10]: ") + defaultPriority, err := strconv.Atoi(defaultPriorityStr) + if err != nil || (defaultPriority > 10 || defaultPriority < 0) { + erred("Priority needs to be a number between 0 and 10.") + continue + } else { + return defaultPriority + } + hr() + } +} + func inputServerURL() *url.URL { for { rawURL := inputString("Gotify URL: ") diff --git a/command/push.go b/command/push.go index a49486b..164bab7 100644 --- a/command/push.go +++ b/command/push.go @@ -64,6 +64,10 @@ func doPush(ctx *cli.Context) { stringURL = conf.URL } + if !ctx.IsSet("priority") { + priority = conf.DefaultPriority + } + msg := models.MessageExternal{ Message: msgText, Title: title, diff --git a/config/config.go b/config/config.go index 9e71f45..ac47b24 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,8 @@ package config type Config struct { - Token string `json:"token"` - URL string `json:"url"` - FromLocation string `json:"-"` + Token string `json:"token"` + URL string `json:"url"` + DefaultPriority int `json:"defaultPriority"` + FromLocation string `json:"-"` }