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