add check for hyprland config modules

parent 74eec0de
......@@ -22,20 +22,28 @@ var (
HomeDir, _ = os.UserHomeDir()
)
func CheckHyprland(ctx context.Context, cmd *cli.Command) error {
h := config.Env.Hyprland
if h == nil || h.Config == "" {
fmt.Println("")
}
type HyprConfigError struct {
File string
Line int
Text string
}
command := exec.Command("hyprland", "--verify-config")
output, err := command.CombinedOutput()
func HyprlandCheck(configPath string) ([]HyprConfigError, error) {
cmd := exec.Command(
"hyprland", "--verify-config",
"-c", configPath,
)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("")
if _, ok := err.(*exec.ExitError); !ok {
return nil, err
}
}
lines := strings.Split(string(output), "\n")
result := []string{}
results := []HyprConfigError{}
start := false
for _, line := range lines {
......@@ -43,11 +51,62 @@ func CheckHyprland(ctx context.Context, cmd *cli.Command) error {
start = true
continue
}
if start && strings.TrimSpace(line) != "" {
result = append(result, line)
if !start {
continue
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
// вложенные дубли
if strings.Contains(line, ": Config error in file ") {
continue
}
prefix, msg, ok := strings.Cut(line, ": ")
if !ok {
continue
}
var file string
var lineNum int
_, err := fmt.Sscanf(
prefix,
"Config error in file %s at line %d",
&file, &lineNum,
)
if err != nil {
continue
}
results = append(results, HyprConfigError{
File: file,
Line: lineNum,
Text: msg,
})
}
return results, nil
}
func HyprlandCheckCommand(ctx context.Context, cmd *cli.Command) error {
h := config.Env.Hyprland
if h == nil || h.Config == "" {
fmt.Println("")
}
result, err := HyprlandCheck(h.Config)
if err != nil {
color.Red(err.Error())
return err
}
for _, e := range result {
fmt.Printf("%s:%d -> %s\n", e.File, e.Line, e.Text)
}
fmt.Println(strings.Join(result, "\n"))
return nil
}
......@@ -146,6 +205,57 @@ func HyprlandModuleStatusCommand(ctx context.Context, cmd *cli.Command) error {
return nil
}
func HyprlandModuleCheck(module string, user bool, tmp bool) ([]HyprConfigError, error) {
if module == "" {
return nil, fmt.Errorf("укажите модуль для проверки")
}
modulefile := HyprlandGetModuleFile(module, user)
if !utils.FileExists(modulefile) {
return nil, fmt.Errorf("модуль '%s' не найден", module)
}
if tmp {
tmpfile, err := os.CreateTemp("", "ximperconf-hypr-module-check-*.conf")
if err != nil {
return nil, err
}
defer os.Remove(tmpfile.Name())
info, _ := hyprlandVarInfo()
for name, value := range info {
fmt.Fprintf(tmpfile, "$%s = %s\n", name, value)
}
fmt.Fprintf(tmpfile, "source = %s\n", modulefile)
tmpfile.Close()
modulefile = tmpfile.Name()
}
info, err := HyprlandCheck(modulefile)
if err != nil {
return nil, err
}
return info, nil
}
func HyprlandModuleCheckCommand(ctx context.Context, cmd *cli.Command) error {
info, err := HyprlandModuleCheck(cmd.Args().Get(0), cmd.Bool("user"), true)
if err != nil {
color.Red(err.Error())
return err
}
for _, e := range info {
fmt.Printf("%s:%d -> %s\n", e.File, e.Line, e.Text)
}
return nil
}
func HyprlandSetModule(action string, module string, user bool, onlyNew bool) (string, error) {
sysFile, lineFull, lineTilde := hyprlandModuleLines(module, user)
......@@ -322,10 +432,21 @@ func hyprlandInfoModules(user bool, filter string) {
for _, module := range modules {
status := hyprlandModuleStatusStruct(module, user)
errors, err := HyprlandModuleCheck(module, user, true)
if err != nil {
errors = []HyprConfigError{}
}
errornum := len(errors)
desc := ""
if errornum > 0 {
desc = fmt.Sprintf("(ошибки: %d)", errornum)
}
items = append(items, ui.TreeItem{
Name: module,
Status: status,
Description: "",
Description: desc,
})
}
......
......@@ -17,7 +17,7 @@ func CommandList() *cli.Command {
{
Name: "check",
Usage: "Check the Hyprland config",
Action: CheckHyprland,
Action: HyprlandCheckCommand,
},
{
Name: "sync-xkb-layouts",
......@@ -45,6 +45,13 @@ func CommandList() *cli.Command {
},
Commands: []*cli.Command{
{
Name: "check",
Usage: "Check Hyprland module",
ArgsUsage: "module",
Action: HyprlandModuleCheckCommand,
ShellComplete: ShellCompleteModule("all"),
},
{
Name: "status",
Usage: "Hyprland module status",
ArgsUsage: "module",
......
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