187 lines
5.5 KiB
Go
187 lines
5.5 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"strings"
|
|
|
|
"nadir/internal/oscmd"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
)
|
|
|
|
type LocaleStatusBody struct {
|
|
Lang string `json:"lang" example:"it_IT.UTF-8" doc:"System locale (LANG)"`
|
|
VCKeymap string `json:"vc_keymap" example:"it" doc:"Virtual console keymap"`
|
|
X11Layout string `json:"x11_layout" example:"it" doc:"X11 keyboard layout"`
|
|
}
|
|
|
|
type GetLocaleOutput struct{ Body LocaleStatusBody }
|
|
|
|
type LocalesOutput struct {
|
|
Body struct {
|
|
Locales []string `json:"locales" doc:"Available locales"`
|
|
}
|
|
}
|
|
|
|
type KeymapsOutput struct {
|
|
Body struct {
|
|
Keymaps []string `json:"keymaps" doc:"Available virtual console keymaps"`
|
|
}
|
|
}
|
|
|
|
type SetLocaleInput struct {
|
|
Body struct {
|
|
Lang string `json:"lang" example:"it_IT.UTF-8" doc:"Locale to set as LANG"`
|
|
}
|
|
}
|
|
|
|
type SetKeymapInput struct {
|
|
Body struct {
|
|
Keymap string `json:"keymap" example:"it" doc:"Virtual console keymap"`
|
|
}
|
|
}
|
|
|
|
func localeStatus() (LocaleStatusBody, error) {
|
|
lines, err := oscmd.RunLines("localectl", "status")
|
|
if err != nil {
|
|
return LocaleStatusBody{}, err
|
|
}
|
|
var b LocaleStatusBody
|
|
for _, line := range lines {
|
|
label, val, ok := strings.Cut(line, ":")
|
|
if !ok {
|
|
continue
|
|
}
|
|
switch strings.TrimSpace(label) {
|
|
case "System Locale":
|
|
for kv := range strings.FieldsSeq(val) {
|
|
if k, v, ok := strings.Cut(kv, "="); ok && k == "LANG" {
|
|
b.Lang = v
|
|
}
|
|
}
|
|
case "VC Keymap":
|
|
b.VCKeymap = strings.TrimSpace(val)
|
|
case "X11 Layout":
|
|
b.X11Layout = strings.TrimSpace(val)
|
|
}
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
func registerLocale(api huma.API) {
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "system-get-locale",
|
|
Method: "GET",
|
|
Path: "/api/system/locale",
|
|
Summary: "Get locale and keyboard layout",
|
|
Description: "Returns the system locale (LANG), virtual console keymap, " +
|
|
"and X11 layout from localectl.",
|
|
Tags: []string{tagSystem},
|
|
Metadata: op("read"),
|
|
Errors: readErrors,
|
|
}, func(ctx context.Context, _ *struct{}) (*GetLocaleOutput, error) {
|
|
b, err := localeStatus()
|
|
if err != nil {
|
|
return nil, huma.Error500InternalServerError("localectl failed", err)
|
|
}
|
|
return &GetLocaleOutput{Body: b}, nil
|
|
})
|
|
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "system-list-locales",
|
|
Method: "GET",
|
|
Path: "/api/system/locales",
|
|
Summary: "List available locales",
|
|
Description: "Returns every locale the host can be configured to use, " +
|
|
"suitable for populating a selector.",
|
|
Tags: []string{tagSystem},
|
|
Metadata: op("read"),
|
|
Errors: readErrors,
|
|
}, func(ctx context.Context, _ *struct{}) (*LocalesOutput, error) {
|
|
locales, err := oscmd.RunLines("localectl", "list-locales")
|
|
if err != nil {
|
|
return nil, huma.Error500InternalServerError("localectl failed", err)
|
|
}
|
|
out := &LocalesOutput{}
|
|
out.Body.Locales = locales
|
|
return out, nil
|
|
})
|
|
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "system-set-locale",
|
|
Method: "POST",
|
|
Path: "/api/system/locale",
|
|
Summary: "Set system locale (LANG)",
|
|
Description: "Sets LANG via localectl. The value is validated against the " +
|
|
"host's locale list, so an unknown locale returns 400.",
|
|
Tags: []string{tagSystem},
|
|
Metadata: op("write"),
|
|
Errors: writeErrors,
|
|
}, func(ctx context.Context, in *SetLocaleInput) (*oscmd.StatusOutput, error) {
|
|
lang := strings.TrimSpace(in.Body.Lang)
|
|
if lang == "" {
|
|
return nil, huma.Error400BadRequest("empty locale")
|
|
}
|
|
locales, err := oscmd.RunLines("localectl", "list-locales")
|
|
if err != nil {
|
|
return nil, huma.Error500InternalServerError("localectl failed", err)
|
|
}
|
|
if !slices.Contains(locales, lang) {
|
|
return nil, huma.Error400BadRequest("unknown locale: " + lang)
|
|
}
|
|
if _, err := oscmd.Run("localectl", "set-locale", "LANG="+lang); err != nil {
|
|
return nil, huma.Error500InternalServerError("set-locale failed", err)
|
|
}
|
|
return oscmd.OK(), nil
|
|
})
|
|
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "system-list-keymaps",
|
|
Method: "GET",
|
|
Path: "/api/system/keymaps",
|
|
Summary: "List available console keymaps",
|
|
Description: "Returns every virtual console keymap known to the host.",
|
|
Tags: []string{tagSystem},
|
|
Metadata: op("read"),
|
|
Errors: readErrors,
|
|
}, func(ctx context.Context, _ *struct{}) (*KeymapsOutput, error) {
|
|
keymaps, err := oscmd.RunLines("localectl", "list-keymaps")
|
|
if err != nil {
|
|
return nil, huma.Error500InternalServerError("localectl failed", err)
|
|
}
|
|
out := &KeymapsOutput{}
|
|
out.Body.Keymaps = keymaps
|
|
return out, nil
|
|
})
|
|
|
|
huma.Register(api, huma.Operation{
|
|
OperationID: "system-set-keymap",
|
|
Method: "POST",
|
|
Path: "/api/system/keymap",
|
|
Summary: "Set virtual console keymap",
|
|
Description: "Sets the virtual console keymap via localectl. The value is " +
|
|
"validated against the host's keymap list, so an unknown keymap " +
|
|
"returns 400.",
|
|
Tags: []string{tagSystem},
|
|
Metadata: op("write"),
|
|
Errors: writeErrors,
|
|
}, func(ctx context.Context, in *SetKeymapInput) (*oscmd.StatusOutput, error) {
|
|
km := strings.TrimSpace(in.Body.Keymap)
|
|
if km == "" {
|
|
return nil, huma.Error400BadRequest("empty keymap")
|
|
}
|
|
keymaps, err := oscmd.RunLines("localectl", "list-keymaps")
|
|
if err != nil {
|
|
return nil, huma.Error500InternalServerError("localectl failed", err)
|
|
}
|
|
if !slices.Contains(keymaps, km) {
|
|
return nil, huma.Error400BadRequest("unknown keymap: " + km)
|
|
}
|
|
if _, err := oscmd.Run("localectl", "set-keymap", km); err != nil {
|
|
return nil, huma.Error500InternalServerError("set-keymap failed", err)
|
|
}
|
|
return oscmd.OK(), nil
|
|
})
|
|
}
|