Compare commits

...

2 Commits

Author SHA1 Message Date
Daniel Svitan
473ea2ba74 Adds changing name and exiting 2025-04-12 16:06:33 +02:00
Daniel Svitan
9547c72f5a Changes name change parameter source 2025-04-12 16:06:16 +02:00
2 changed files with 59 additions and 8 deletions

View File

@ -40,8 +40,8 @@ func load() error {
return nil
}
func makeReq(method string, url string, body io.Reader) error {
req, err := http.NewRequest(method, url, body)
func makeReq(method string, url string) error {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return err
}
@ -84,7 +84,7 @@ func main() {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "optional id of a client to fetch",
Usage: "optional id of the client to fetch",
},
},
Action: clients,
@ -95,7 +95,7 @@ func main() {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "optional id of a client to log",
Usage: "optional id of the client to log",
},
&cli.StringFlag{
Name: "date",
@ -112,6 +112,35 @@ func main() {
},
Action: logs,
},
{
Name: "name",
Usage: "rename a client",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "id of the client",
Required: true,
},
&cli.StringFlag{
Name: "new",
Usage: "new name of the client",
Required: true,
},
},
Action: name,
},
{
Name: "exit",
Usage: "request a client to disconnect",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "id of the client",
Required: true,
},
},
Action: exit,
},
},
}
@ -135,7 +164,7 @@ func clients(ctx context.Context, cmd *cli.Command) error {
url = fmt.Sprintf("%s/admin/clients", server)
}
return makeReq(http.MethodGet, url, nil)
return makeReq(http.MethodGet, url)
}
func logs(ctx context.Context, cmd *cli.Command) error {
@ -170,5 +199,28 @@ func logs(ctx context.Context, cmd *cli.Command) error {
url = fmt.Sprintf("%s/admin/logs", server)
}
return makeReq(http.MethodGet, url, nil)
return makeReq(http.MethodGet, url)
}
func name(ctx context.Context, cmd *cli.Command) error {
err := load()
if err != nil {
return err
}
id := cmd.String("id")
name := cmd.String("new")
url := fmt.Sprintf("%s/admin/name/%s?name=%s", server, id, name)
return makeReq(http.MethodPost, url)
}
func exit(ctx context.Context, cmd *cli.Command) error {
err := load()
if err != nil {
return err
}
id := cmd.String("id")
url := fmt.Sprintf("%s/admin/exit/%s", server, id)
return makeReq(http.MethodPost, url)
}

View File

@ -232,7 +232,7 @@ func main() {
})))
app.POST("/admin/name/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
name := c.Param("name")
name := c.QueryParam("name")
if name == "" {
return c.JSON(http.StatusBadRequest, "missing field 'name'")
}
@ -296,7 +296,6 @@ func keys(c echo.Context) error {
fmt.Printf("%s client %s crashed (couldn't open file)\n", time.Now().Format(TimeFormat), client.ID)
now = time.Now()
client.ExitWanted = true
client.Connected = false
client.DisconnectedAt = now
client.UpdatedAt = now