🔨 Adapts to 1.1.0 API
All checks were successful
Gitea Build Action / build (push) Successful in 23s
All checks were successful
Gitea Build Action / build (push) Successful in 23s
This commit is contained in:
parent
cb07efede6
commit
96abe93c3e
163
admin/main.go
163
admin/main.go
@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -20,6 +22,15 @@ var (
|
||||
token string
|
||||
)
|
||||
|
||||
type ChangeLockReq struct {
|
||||
Locked bool `json:"locked"`
|
||||
}
|
||||
|
||||
type ChangeAlertReq struct {
|
||||
Alert bool `json:"alert"`
|
||||
For int `json:"for"`
|
||||
}
|
||||
|
||||
func load() error {
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("toml")
|
||||
@ -42,33 +53,33 @@ func load() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func makeReqPrint(method string, url string) error {
|
||||
req, err := http.NewRequest(method, url, nil)
|
||||
func makeGetReq(url string) ([]byte, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", token)
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
defer func() { res.Body.Close() }()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
fmt.Printf("<-- %d\n", res.StatusCode)
|
||||
}
|
||||
|
||||
recv, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("%s", recv)
|
||||
return nil
|
||||
return io.ReadAll(res.Body)
|
||||
}
|
||||
|
||||
func makeReqRet(method string, url string) ([]byte, error) {
|
||||
req, err := http.NewRequest(method, url, nil)
|
||||
func makePostReq(url string, body any) ([]byte, error) {
|
||||
bodyRaw, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(bodyRaw))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -78,6 +89,7 @@ func makeReqRet(method string, url string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = res.Body.Close() }()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
fmt.Printf("<-- %d\n", res.StatusCode)
|
||||
@ -97,7 +109,7 @@ func main() {
|
||||
Name: "version",
|
||||
Usage: "print version and exit",
|
||||
Action: func(context.Context, *cli.Command) error {
|
||||
fmt.Println("version 1.0.0")
|
||||
fmt.Println("version 1.1.0")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
@ -115,31 +127,36 @@ func main() {
|
||||
Usage: "print raw server output",
|
||||
},
|
||||
},
|
||||
Action: get,
|
||||
Action: getOpened,
|
||||
},
|
||||
{
|
||||
Name: "lock",
|
||||
Usage: "change lock status",
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "do",
|
||||
Name: "on",
|
||||
Usage: "lock the door",
|
||||
Action: createManageLock(true),
|
||||
},
|
||||
{
|
||||
Name: "undo",
|
||||
Name: "off",
|
||||
Usage: "unlock the door",
|
||||
Action: createManageLock(false),
|
||||
},
|
||||
},
|
||||
Action: getLock,
|
||||
Action: getLocked,
|
||||
},
|
||||
{
|
||||
Name: "alerts",
|
||||
Usage: "change alerts status",
|
||||
Name: "alert",
|
||||
Usage: "change alert status",
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "pause",
|
||||
Name: "on",
|
||||
Usage: "resume alerts",
|
||||
Action: createManageAlert(true),
|
||||
},
|
||||
{
|
||||
Name: "off",
|
||||
Usage: "pause alerts",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
@ -147,15 +164,10 @@ func main() {
|
||||
Usage: "pause for x seconds",
|
||||
},
|
||||
},
|
||||
Action: createManageAlerts(true),
|
||||
},
|
||||
{
|
||||
Name: "resume",
|
||||
Usage: "resume alerts",
|
||||
Action: createManageAlerts(false),
|
||||
Action: createManageAlert(false),
|
||||
},
|
||||
},
|
||||
Action: getAlerts,
|
||||
Action: getAlert,
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -173,16 +185,22 @@ func test(context.Context, *cli.Command) error {
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/", server)
|
||||
return makeReqPrint(http.MethodGet, url)
|
||||
data, err := makeGetReq(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("%s", data)
|
||||
return nil
|
||||
}
|
||||
|
||||
func get(ctx context.Context, cmd *cli.Command) error {
|
||||
func getOpened(ctx context.Context, cmd *cli.Command) error {
|
||||
err := load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opened, err := makeReqRet(http.MethodGet, fmt.Sprintf("%s/read", server))
|
||||
opened, err := makeGetReq(fmt.Sprintf("%s/read", server))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -200,13 +218,13 @@ func get(ctx context.Context, cmd *cli.Command) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getLock(context.Context, *cli.Command) error {
|
||||
func getLocked(context.Context, *cli.Command) error {
|
||||
err := load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
locked, err := makeReqRet(http.MethodGet, fmt.Sprintf("%s/lock", server))
|
||||
locked, err := makeGetReq(fmt.Sprintf("%s/lock", server))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -220,40 +238,13 @@ func getLock(context.Context, *cli.Command) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func createManageLock(do bool) func(context.Context, *cli.Command) error {
|
||||
return func(ctx context.Context, cmd *cli.Command) error {
|
||||
err := load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var actionShort string
|
||||
var action string
|
||||
if do {
|
||||
actionShort = "do"
|
||||
action = "lock"
|
||||
} else {
|
||||
actionShort = "undo"
|
||||
action = "unlock"
|
||||
}
|
||||
|
||||
_, err = makeReqRet(http.MethodPost, fmt.Sprintf("%s/lock/%s", server, actionShort))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("door was %sed\n", action)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func getAlerts(context.Context, *cli.Command) error {
|
||||
func getAlert(context.Context, *cli.Command) error {
|
||||
err := load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
alert, err := makeReqRet(http.MethodGet, fmt.Sprintf("%s/alerts", server))
|
||||
alert, err := makeGetReq(fmt.Sprintf("%s/alerts", server))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -267,21 +258,43 @@ func getAlerts(context.Context, *cli.Command) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func createManageAlerts(pause bool) func(context.Context, *cli.Command) error {
|
||||
func createManageLock(locked bool) func(context.Context, *cli.Command) error {
|
||||
return func(ctx context.Context, cmd *cli.Command) error {
|
||||
err := load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var action string
|
||||
if pause {
|
||||
action = "pause"
|
||||
} else {
|
||||
action = "resume"
|
||||
var data ChangeLockReq
|
||||
data.Locked = locked
|
||||
|
||||
_, err = makePostReq(fmt.Sprintf("%s/lock", server), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rest string
|
||||
var action string
|
||||
if locked {
|
||||
action = "locked"
|
||||
} else {
|
||||
action = "unlocked"
|
||||
}
|
||||
|
||||
fmt.Printf("door was %s\n", action)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func createManageAlert(alert bool) func(context.Context, *cli.Command) error {
|
||||
return func(ctx context.Context, cmd *cli.Command) error {
|
||||
err := load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var data ChangeAlertReq
|
||||
data.Alert = alert
|
||||
|
||||
var secs int
|
||||
secsRaw := cmd.String("for")
|
||||
if secsRaw != "" {
|
||||
@ -289,17 +302,25 @@ func createManageAlerts(pause bool) func(context.Context, *cli.Command) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rest = fmt.Sprintf("?for=%d", secs)
|
||||
data.For = secs
|
||||
}
|
||||
|
||||
_, err = makeReqRet(http.MethodPost, fmt.Sprintf("%s/alerts/%s%s", server, action, rest))
|
||||
_, err = makePostReq(fmt.Sprintf("%s/alerts", server), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rest string
|
||||
if secsRaw != "" {
|
||||
rest = fmt.Sprintf(" for %d seconds", secs)
|
||||
}
|
||||
var action string
|
||||
if alert {
|
||||
action = "resumed"
|
||||
} else {
|
||||
action = "paused"
|
||||
}
|
||||
|
||||
fmt.Printf("alerts were %sd%s\n", action, rest)
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user