|
|
@@ -16,6 +16,7 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
type LocaleStatusBody struct {
|
|
|
|
type LocaleStatusBody struct {
|
|
|
|
Lang string `json:"lang" example:"it_IT.UTF-8" doc:"System locale (LANG)"`
|
|
|
|
Lang string `json:"lang" example:"it_IT.UTF-8" doc:"System locale (LANG)"`
|
|
|
|
|
|
|
|
Language string `json:"language" example:"en_US:" doc:"Fallback language list (LANGUAGE)"`
|
|
|
|
VCKeymap string `json:"vc_keymap" example:"it" doc:"Virtual console keymap"`
|
|
|
|
VCKeymap string `json:"vc_keymap" example:"it" doc:"Virtual console keymap"`
|
|
|
|
X11Layout string `json:"x11_layout" example:"it" doc:"X11 keyboard layout"`
|
|
|
|
X11Layout string `json:"x11_layout" example:"it" doc:"X11 keyboard layout"`
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -31,12 +32,14 @@ type LocalesOutput struct {
|
|
|
|
type KeymapsOutput struct {
|
|
|
|
type KeymapsOutput struct {
|
|
|
|
Body struct {
|
|
|
|
Body struct {
|
|
|
|
Keymaps []string `json:"keymaps" doc:"Available virtual console keymaps"`
|
|
|
|
Keymaps []string `json:"keymaps" doc:"Available virtual console keymaps"`
|
|
|
|
|
|
|
|
Reason string `json:"reason,omitempty" doc:"When keymaps is empty, why: e.g. \"kbd not installed on this server\""`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type SetLocaleInput struct {
|
|
|
|
type SetLocaleInput struct {
|
|
|
|
Body struct {
|
|
|
|
Body struct {
|
|
|
|
Lang string `json:"lang" example:"it_IT.UTF-8" doc:"Locale to set as LANG"`
|
|
|
|
Lang string `json:"lang" example:"it_IT.UTF-8" doc:"Locale to set as LANG"`
|
|
|
|
|
|
|
|
Language *string `json:"language,omitempty" example:"en_US:" doc:"Fallback language list (LANGUAGE)"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@@ -56,6 +59,10 @@ type GenerateLocaleInput struct {
|
|
|
|
// .charmap suffix (e.g. fr_FR, fr_FR.UTF-8, en_US.ISO-8859-1).
|
|
|
|
// .charmap suffix (e.g. fr_FR, fr_FR.UTF-8, en_US.ISO-8859-1).
|
|
|
|
var localeRe = regexp.MustCompile(`^[a-z]{2,3}_[A-Z]{2}(\.[A-Za-z0-9_-]+)?$`)
|
|
|
|
var localeRe = regexp.MustCompile(`^[a-z]{2,3}_[A-Z]{2}(\.[A-Za-z0-9_-]+)?$`)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// languageRe validates the LANGUAGE fallback list: colon-separated locale names
|
|
|
|
|
|
|
|
// like "en_US:de_DE". Anchored so a leading "-" can't survive into argv.
|
|
|
|
|
|
|
|
var languageRe = regexp.MustCompile(`^[a-zA-Z0-9_.:-]*$`)
|
|
|
|
|
|
|
|
|
|
|
|
func localeStatus() (LocaleStatusBody, error) {
|
|
|
|
func localeStatus() (LocaleStatusBody, error) {
|
|
|
|
lines, err := oscmd.RunLines("localectl", "status")
|
|
|
|
lines, err := oscmd.RunLines("localectl", "status")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@@ -63,21 +70,28 @@ func localeStatus() (LocaleStatusBody, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var b LocaleStatusBody
|
|
|
|
var b LocaleStatusBody
|
|
|
|
for _, line := range lines {
|
|
|
|
for _, line := range lines {
|
|
|
|
label, val, ok := strings.Cut(line, ":")
|
|
|
|
trimmed := strings.TrimSpace(line)
|
|
|
|
if !ok {
|
|
|
|
if strings.HasPrefix(trimmed, "VC Keymap:") {
|
|
|
|
|
|
|
|
b.VCKeymap = strings.TrimSpace(strings.TrimPrefix(trimmed, "VC Keymap:"))
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch strings.TrimSpace(label) {
|
|
|
|
if strings.HasPrefix(trimmed, "X11 Layout:") {
|
|
|
|
case "System Locale":
|
|
|
|
b.X11Layout = strings.TrimSpace(strings.TrimPrefix(trimmed, "X11 Layout:"))
|
|
|
|
for kv := range strings.FieldsSeq(val) {
|
|
|
|
continue
|
|
|
|
if k, v, ok := strings.Cut(kv, "="); ok && k == "LANG" {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse k=v fields on any other line (like System Locale block).
|
|
|
|
|
|
|
|
parts := strings.Fields(trimmed)
|
|
|
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
|
|
|
part = strings.TrimPrefix(part, "System Locale:")
|
|
|
|
|
|
|
|
part = strings.TrimSpace(part)
|
|
|
|
|
|
|
|
if k, v, ok := strings.Cut(part, "="); ok {
|
|
|
|
|
|
|
|
switch k {
|
|
|
|
|
|
|
|
case "LANG":
|
|
|
|
b.Lang = v
|
|
|
|
b.Lang = v
|
|
|
|
|
|
|
|
case "LANGUAGE":
|
|
|
|
|
|
|
|
b.Language = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "VC Keymap":
|
|
|
|
|
|
|
|
b.VCKeymap = strings.TrimSpace(val)
|
|
|
|
|
|
|
|
case "X11 Layout":
|
|
|
|
|
|
|
|
b.X11Layout = strings.TrimSpace(val)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
return b, nil
|
|
|
@@ -144,7 +158,17 @@ func registerLocale(api huma.API) {
|
|
|
|
if !slices.Contains(locales, lang) {
|
|
|
|
if !slices.Contains(locales, lang) {
|
|
|
|
return nil, huma.Error400BadRequest("unknown locale: " + lang)
|
|
|
|
return nil, huma.Error400BadRequest("unknown locale: " + lang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := oscmd.Run("localectl", "set-locale", "LANG="+lang); err != nil {
|
|
|
|
|
|
|
|
|
|
|
|
args := []string{"set-locale", "LANG=" + lang}
|
|
|
|
|
|
|
|
if in.Body.Language != nil {
|
|
|
|
|
|
|
|
langVal := strings.TrimSpace(*in.Body.Language)
|
|
|
|
|
|
|
|
if !languageRe.MatchString(langVal) {
|
|
|
|
|
|
|
|
return nil, huma.Error400BadRequest("invalid language format: " + langVal)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, "LANGUAGE="+langVal)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := oscmd.Run("localectl", args...); err != nil {
|
|
|
|
return nil, huma.Error500InternalServerError("set-locale failed", err)
|
|
|
|
return nil, huma.Error500InternalServerError("set-locale failed", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oscmd.OK(), nil
|
|
|
|
return oscmd.OK(), nil
|
|
|
@@ -161,12 +185,14 @@ func registerLocale(api huma.API) {
|
|
|
|
Errors: readErrors,
|
|
|
|
Errors: readErrors,
|
|
|
|
}, func(ctx context.Context, _ *struct{}) (*KeymapsOutput, error) {
|
|
|
|
}, func(ctx context.Context, _ *struct{}) (*KeymapsOutput, error) {
|
|
|
|
// ponytail: minimal servers ship without kbd / /usr/share/keymaps, so
|
|
|
|
// ponytail: minimal servers ship without kbd / /usr/share/keymaps, so
|
|
|
|
// localectl errors instead of returning empty. Treat that as "no keymaps
|
|
|
|
// localectl errors instead of returning empty. Surface that as a `reason`
|
|
|
|
// available" rather than a server fault — set-keymap stays unreachable
|
|
|
|
// the frontend can display ("kbd not installed") instead of an opaque N/A.
|
|
|
|
// because nothing will be in the allowlist.
|
|
|
|
keymaps, err := oscmd.RunLines("localectl", "list-keymaps")
|
|
|
|
keymaps, _ := oscmd.RunLines("localectl", "list-keymaps")
|
|
|
|
|
|
|
|
out := &KeymapsOutput{}
|
|
|
|
out := &KeymapsOutput{}
|
|
|
|
out.Body.Keymaps = keymaps
|
|
|
|
out.Body.Keymaps = keymaps
|
|
|
|
|
|
|
|
if err != nil || len(keymaps) == 0 {
|
|
|
|
|
|
|
|
out.Body.Reason = "kbd is not installed on this server (install the kbd / console-data package to enable keymap selection)"
|
|
|
|
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
return out, nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
@@ -209,7 +235,7 @@ func registerLocale(api huma.API) {
|
|
|
|
"already generated, returns 200 immediately.",
|
|
|
|
"already generated, returns 200 immediately.",
|
|
|
|
Tags: []string{tagSystem},
|
|
|
|
Tags: []string{tagSystem},
|
|
|
|
Metadata: op("write"),
|
|
|
|
Metadata: op("write"),
|
|
|
|
Errors: append(writeErrors, 501),
|
|
|
|
Errors: []int{400, 401, 403, 500, 501},
|
|
|
|
}, func(ctx context.Context, in *GenerateLocaleInput) (*oscmd.StatusOutput, error) {
|
|
|
|
}, func(ctx context.Context, in *GenerateLocaleInput) (*oscmd.StatusOutput, error) {
|
|
|
|
locale := strings.TrimSpace(in.Body.Locale)
|
|
|
|
locale := strings.TrimSpace(in.Body.Locale)
|
|
|
|
if locale == "" {
|
|
|
|
if locale == "" {
|
|
|
@@ -263,9 +289,16 @@ func enableLocaleGen(path, locale string) error {
|
|
|
|
if !found {
|
|
|
|
if !found {
|
|
|
|
return huma.Error400BadRequest("locale not available for generation: " + locale)
|
|
|
|
return huma.Error400BadRequest("locale not available for generation: " + locale)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(path, []byte(newContent), 0644); err != nil {
|
|
|
|
// Write atomically: a crash mid-write would leave /etc/locale.gen truncated
|
|
|
|
|
|
|
|
// and break every subsequent locale-gen on the host.
|
|
|
|
|
|
|
|
tmp := path + ".nadir.tmp"
|
|
|
|
|
|
|
|
if err := os.WriteFile(tmp, []byte(newContent), 0644); err != nil {
|
|
|
|
return huma.Error500InternalServerError("writing locale.gen failed", err)
|
|
|
|
return huma.Error500InternalServerError("writing locale.gen failed", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(tmp, path); err != nil {
|
|
|
|
|
|
|
|
os.Remove(tmp)
|
|
|
|
|
|
|
|
return huma.Error500InternalServerError("replacing locale.gen failed", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
if _, err := oscmd.Run("locale-gen"); err != nil {
|
|
|
|
if _, err := oscmd.Run("locale-gen"); err != nil {
|
|
|
|
return huma.Error500InternalServerError("locale-gen failed", err)
|
|
|
|
return huma.Error500InternalServerError("locale-gen failed", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|