Add ability to set a default priority for cli client, fix #2

This commit is contained in:
Avner Cohen
2020-05-21 10:47:13 +03:00
committed by Jannis Mattheis
parent facfb9b517
commit a38bf37dcd
5 changed files with 31 additions and 6 deletions

View File

@@ -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` | | token | an application token (a client token will not work) | `A4ZudDRdLT40L5X` |
| url | the URL to your [gotify/server][gotify/server] | `https://gotify.example.com` | | 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 ### Config example
```json ```json
{ {
"token": "A4ZudDRdLT40L5X", "token": "A4ZudDRdLT40L5X",
"url": "https://gotify.example.com" "url": "https://gotify.example.com",
"defaultPriority": 6
} }
``` ```

View File

@@ -21,6 +21,7 @@ func Config() cli.Command {
} }
fmt.Println("Used Config:", conf.FromLocation) fmt.Println("Used Config:", conf.FromLocation)
fmt.Println("URL:", conf.URL) fmt.Println("URL:", conf.URL)
fmt.Println("Default Priority:", conf.DefaultPriority)
fmt.Println("Token:", conf.Token) fmt.Println("Token:", conf.Token)
}, },
} }

View File

@@ -35,10 +35,13 @@ func doInit(ctx *cli.Context) {
hr() hr()
token := inputToken(gotify.NewClient(serverURL, utils.CreateHTTPClient())) token := inputToken(gotify.NewClient(serverURL, utils.CreateHTTPClient()))
hr() hr()
defaultPriority := inputDefaultPriority()
hr()
conf := &config.Config{ conf := &config.Config{
URL: serverURL.String(), URL: serverURL.String(),
Token: token, Token: token,
DefaultPriority: defaultPriority,
} }
pathToWrite, err := config.ExistingConfig(config.GetLocations()) 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 { func inputServerURL() *url.URL {
for { for {
rawURL := inputString("Gotify URL: ") rawURL := inputString("Gotify URL: ")

View File

@@ -64,6 +64,10 @@ func doPush(ctx *cli.Context) {
stringURL = conf.URL stringURL = conf.URL
} }
if !ctx.IsSet("priority") {
priority = conf.DefaultPriority
}
msg := models.MessageExternal{ msg := models.MessageExternal{
Message: msgText, Message: msgText,
Title: title, Title: title,

View File

@@ -1,7 +1,8 @@
package config package config
type Config struct { type Config struct {
Token string `json:"token"` Token string `json:"token"`
URL string `json:"url"` URL string `json:"url"`
FromLocation string `json:"-"` DefaultPriority int `json:"defaultPriority"`
FromLocation string `json:"-"`
} }