update distro profiles

- add hyprmodules
parent 7b95545e
package create package create
import ( import (
"bytes"
"ximperconf/config" "ximperconf/config"
"ximperconf/hyprland" "ximperconf/hyprland"
...@@ -128,6 +129,47 @@ func processProfile(prof Profile, dryRun bool, res *Result) { ...@@ -128,6 +129,47 @@ func processProfile(prof Profile, dryRun bool, res *Result) {
} }
// ----- hyprmodules -----
for _, m := range prof.HyprModules {
if m.Module == "" {
res.HyprModules = append(res.HyprModules, "Пропущен модуль без имени")
continue
}
// Если есть if-exec
if m.IfExec != "" {
cmd := exec.Command("sh", "-c", m.IfExec)
output, err := cmd.Output()
if err != nil {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("Модуль '%s' пропущен: команда '%s' завершилась с ошибкой: %v", m.Module, m.IfExec, err))
continue
}
// Если команда ничего не вывела
if len(bytes.TrimSpace(output)) == 0 {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("Модуль '%s' пропущен: команда '%s' не вернула данных", m.Module, m.IfExec))
continue
}
}
// Dry-run
if dryRun {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("[Dry-run] Подключён модуль: %s", m.Module))
continue
}
if _, err := hyprland.HyprlandSetModule("enable", m.Module, m.User, m.NewOnly); err != nil {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("Ошибка подключения модуля %s: %v", m.Module, err))
} else {
res.HyprModules = append(res.HyprModules,
fmt.Sprintf("Подключён модуль: %s", m.Module))
}
}
// ----- синхронизация раскладок Hyprland----- // ----- синхронизация раскладок Hyprland-----
if prof.SyncSystemLayouts && !dryRun { if prof.SyncSystemLayouts && !dryRun {
sysLayouts, err := hyprland.HyprlandGetKeyboardLayouts() sysLayouts, err := hyprland.HyprlandGetKeyboardLayouts()
...@@ -385,6 +427,7 @@ func CreateConfCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -385,6 +427,7 @@ func CreateConfCommand(ctx context.Context, cmd *cli.Command) error {
printTree("Создаём ссылки", res.Links) printTree("Создаём ссылки", res.Links)
printTree("Обновляем", res.Replaced) printTree("Обновляем", res.Replaced)
printTree("Создаём переменные Hyprland", res.HyprVars) printTree("Создаём переменные Hyprland", res.HyprVars)
printTree("Подключаем модули Hyprland", res.HyprModules)
printTree("Синхронизируем раскладку Hyprland", res.SyncSystemLayouts) printTree("Синхронизируем раскладку Hyprland", res.SyncSystemLayouts)
return nil return nil
......
...@@ -22,6 +22,13 @@ type HyprVar struct { ...@@ -22,6 +22,13 @@ type HyprVar struct {
Value string `json:"value"` Value string `json:"value"`
} }
type HyprModule struct {
Module string `json:"module"`
User bool `json:"user,omitempty"`
NewOnly bool `json:"new-only,omitempty"`
IfExec string `json:"if-exec,omitempty"`
}
type Profile struct { type Profile struct {
Binary string `json:"binary,omitempty"` Binary string `json:"binary,omitempty"`
Copy []CopyEntry `json:"copy,omitempty"` Copy []CopyEntry `json:"copy,omitempty"`
...@@ -29,8 +36,9 @@ type Profile struct { ...@@ -29,8 +36,9 @@ type Profile struct {
ReleaseReplace []ReleaseReplaceEntry `json:"release-replace,omitempty"` ReleaseReplace []ReleaseReplaceEntry `json:"release-replace,omitempty"`
// ----- hyprland ----- // ----- hyprland -----
SyncSystemLayouts bool `json:"sync-system-layouts,omitempty"` SyncSystemLayouts bool `json:"sync-system-layouts,omitempty"`
HyprVars []HyprVar `json:"hyprvars,omitempty"` HyprVars []HyprVar `json:"hyprvars,omitempty"`
HyprModules []HyprModule `json:"hyprmodules,omitempty"`
} }
type Config struct { type Config struct {
...@@ -42,5 +50,6 @@ type Result struct { ...@@ -42,5 +50,6 @@ type Result struct {
Copies []string Copies []string
Replaced []string Replaced []string
HyprVars []string HyprVars []string
HyprModules []string
SyncSystemLayouts []string SyncSystemLayouts []string
} }
...@@ -102,50 +102,71 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -102,50 +102,71 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
return nil return nil
} }
func hyprlandSetModule(action string, module string, user bool) { func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (string, error) {
sysFile := hyprlandGetModuleFile(module, user) sysFile := hyprlandGetModuleFile(module, user)
lineFull := "source = " + sysFile lineFull := "source = " + sysFile
lineTilde := "source = ~" + strings.TrimPrefix(sysFile, HomeDir) lineTilde := "source = ~" + strings.TrimPrefix(sysFile, HomeDir)
var out string
if !fileExists(sysFile) { if !fileExists(sysFile) {
fmt.Printf("Модуль '%s' не найден: %s", module, sysFile) return "", fmt.Errorf("модуль '%s' не найден: %s", module, sysFile)
return
} }
data, _ := os.ReadFile(config.Env.Hyprland.Config) data, _ := os.ReadFile(config.Env.Hyprland.Config)
lines := strings.Split(string(data), "\n") lines := strings.Split(string(data), "\n")
changed := false changed := false
found := false
switch action { switch action {
case "enable": case "enable":
for i, l := range lines { for i, l := range lines {
if strings.TrimSpace(l) == lineFull || strings.TrimSpace(l) == lineTilde { trim := strings.TrimSpace(l)
fmt.Printf("Модуль '%s' уже включён", module)
return // Уже включён
if trim == lineFull || trim == lineTilde {
found = true
if onlyNew {
return "", fmt.Errorf("модуль '%s' уже присутствует в конфиге — пропущено", module)
}
return "", fmt.Errorf("модуль '%s' уже включён", module)
} }
re := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineFull) + `$`)
if re.MatchString(l) { // Был закомментирован
reFull := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineFull) + `$`)
reTilde := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineTilde) + `$`)
if reFull.MatchString(l) || reTilde.MatchString(l) {
found = true
if onlyNew {
return "", fmt.Errorf("модуль '%s' уже присутствует в конфиге (закомментирован) — пропущено", module)
}
lines[i] = lineFull lines[i] = lineFull
changed = true changed = true
} }
re = regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineTilde) + `$`) }
if re.MatchString(l) {
lines[i] = lineFull // Если модуль нигде не найден — добавляем новую строку
changed = true if !found {
if strings.HasPrefix(sysFile, HomeDir) {
lines = append(lines, lineTilde)
} else {
lines = append(lines, lineFull)
} }
changed = true
} }
if !changed { if !changed {
lines = append(lines, lineTilde) return "", fmt.Errorf("модуль '%s' не изменён", module)
} }
color.Green("Модуль '%s' включён", module)
out = fmt.Sprintf("Модуль '%s' включён", module)
case "disable": case "disable":
for i, l := range lines { for i, l := range lines {
reFull := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineFull) + `$`) reFull := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineFull) + `$`)
reTilde := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineTilde) + `$`) reTilde := regexp.MustCompile(`^\s*#\s*` + regexp.QuoteMeta(lineTilde) + `$`)
if reFull.MatchString(l) || reTilde.MatchString(l) { if reFull.MatchString(l) || reTilde.MatchString(l) {
color.Yellow("Модуль '%s' уже отключён", module) return "", fmt.Errorf("модуль '%s' уже отключён", module)
return
} }
if strings.TrimSpace(l) == lineFull || strings.TrimSpace(l) == lineTilde { if strings.TrimSpace(l) == lineFull || strings.TrimSpace(l) == lineTilde {
...@@ -155,22 +176,33 @@ func hyprlandSetModule(action string, module string, user bool) { ...@@ -155,22 +176,33 @@ func hyprlandSetModule(action string, module string, user bool) {
} }
if !changed { if !changed {
color.Red("Модуль '%s' не найден в конфигурации", module) return "", fmt.Errorf("модуль '%s' не найден в конфигурации", module)
return
} }
out = fmt.Sprintf("Модуль '%s' отключён", module)
color.Yellow("Модуль '%s' отключён", module)
} }
_ = os.WriteFile(config.Env.Hyprland.Config, []byte(strings.Join(lines, "\n")), 0644) _ = os.WriteFile(config.Env.Hyprland.Config, []byte(strings.Join(lines, "\n")), 0644)
return out, nil
} }
func HyprlandModuleEnableCommand(ctx context.Context, cmd *cli.Command) error { func HyprlandModuleEnableCommand(ctx context.Context, cmd *cli.Command) error {
hyprlandSetModule("enable", cmd.Args().Get(0), cmd.Bool("user")) msg, err := HyprlandSetModule("enable", cmd.Args().Get(0), cmd.Bool("user"), false)
if err != nil {
color.Red(err.Error())
return err
}
color.Green(msg)
return nil return nil
} }
func HyprlandModuleDisableCommand(ctx context.Context, cmd *cli.Command) error { func HyprlandModuleDisableCommand(ctx context.Context, cmd *cli.Command) error {
hyprlandSetModule("disable", cmd.Args().Get(0), cmd.Bool("user")) msg, err := HyprlandSetModule("disable", cmd.Args().Get(0), cmd.Bool("user"), false)
if err != nil {
color.Red(err.Error())
return err
}
color.Green(msg)
return nil return nil
} }
func HyprlandToggleModuleCommand(ctx context.Context, cmd *cli.Command) error { func HyprlandToggleModuleCommand(ctx context.Context, cmd *cli.Command) error {
...@@ -178,12 +210,23 @@ func HyprlandToggleModuleCommand(ctx context.Context, cmd *cli.Command) error { ...@@ -178,12 +210,23 @@ func HyprlandToggleModuleCommand(ctx context.Context, cmd *cli.Command) error {
user := cmd.Bool("user") user := cmd.Bool("user")
status := hyprlandModuleStatus(module, user) status := hyprlandModuleStatus(module, user)
var msg string
var err error
switch status { switch status {
case "enabled": case "enabled":
hyprlandSetModule("disable", module, user) msg, err = HyprlandSetModule("disable", module, user, false)
case "disabled": case "disabled":
hyprlandSetModule("enable", module, user) msg, err = HyprlandSetModule("enable", module, user, false)
}
if err != nil {
color.Red(err.Error())
return err
} }
color.Green(msg)
return nil return nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment