Compare commits

4 Commits

Author SHA1 Message Date
Daniel Svitan
3c6d7e027a Adds a unique name rule 2025-04-13 17:58:05 +02:00
Daniel Svitan
50420bc0a5 Adds selecting client by name 2025-04-13 17:46:43 +02:00
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 76 additions and 15 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

@@ -159,7 +159,7 @@ func main() {
app.GET("/admin/clients/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
for _, client := range clients {
if client.ID == id {
if client.ID == id || client.Name == id.String() {
return c.JSONPretty(http.StatusOK, client, indent)
}
}
@@ -228,21 +228,31 @@ func main() {
return c.NoContent(http.StatusInternalServerError)
}
return c.JSONPretty(http.StatusOK, string(bytes[:n]), indent)
return c.String(http.StatusOK, string(bytes[:n]))
})))
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'")
}
var target *Client = nil
for _, client := range clients {
if client.ID == id {
client.Name = name
client.UpdatedAt = time.Now()
return c.JSON(http.StatusOK, echo.Map{"message": "ok"})
if client.ID == id || client.Name == id.String() {
target = client
}
if client.Name == name {
return c.JSON(http.StatusBadRequest, echo.Map{"message": "name already used"})
}
}
if target != nil {
target.Name = name
target.UpdatedAt = time.Now()
return c.JSON(http.StatusOK, echo.Map{"message": "ok"})
}
return c.JSON(http.StatusNotFound, echo.Map{"message": "not found"})
@@ -250,7 +260,7 @@ func main() {
app.POST("/admin/exit/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
for _, client := range clients {
if client.ID == id {
if client.ID == id || client.Name == id.String() {
client.ExitWanted = true
return c.JSON(http.StatusOK, echo.Map{"message": "ok"})
}
@@ -296,7 +306,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