Commit a00d0de9 authored by Vitaly Lipatov's avatar Vitaly Lipatov

fix: improve error handling and prevent nil pointer dereference

- hyprland/actions.go: add missing return after nil check to prevent panic - repo/actions.go: replace os.Exit with proper error return - config/dbus.go: return error instead of log.Fatalf for graceful handling - hyprland/keyboard-actions.go: add nil check for DBusConn - main.go: handle InitDBus error gracefully - system/commands.go: use descriptive error message instead of empty string
parent 3ab504f2
package config
import (
"log"
"github.com/godbus/dbus/v5"
)
var DBusConn *dbus.Conn
func InitDBus() {
func InitDBus() error {
var err error
DBusConn, err = dbus.SystemBus()
if err != nil {
log.Fatalf("failed to connect to system bus: %v", err)
return err
}
return nil
}
......@@ -95,7 +95,8 @@ func HyprlandCheck(configPath string) ([]HyprConfigError, error) {
func HyprlandCheckCommand(ctx context.Context, cmd *cli.Command) error {
h := config.Env.Hyprland
if h == nil || h.Config == "" {
fmt.Println("")
color.Red("Hyprland не настроен")
return nil
}
result, err := HyprlandCheck(h.Config)
......
......@@ -11,6 +11,9 @@ import (
)
func HyprlandGetKeyboardLayouts() (string, error) {
if config.DBusConn == nil {
return "", fmt.Errorf("DBus недоступен")
}
obj := config.DBusConn.Object("org.freedesktop.locale1", "/org/freedesktop/locale1")
variant := dbus.Variant{}
......
......@@ -15,8 +15,9 @@ import (
func main() {
config.InitConfig()
config.InitDBus()
defer config.DBusConn.Close()
if err := config.InitDBus(); err == nil {
defer config.DBusConn.Close()
}
rootCommand := &cli.Command{
Name: "ximperconf",
......
package repo
import (
"ximperconf/config"
"context"
"fmt"
"io"
"net/http"
"os"
"regexp"
"ximperconf/config"
"github.com/fatih/color"
"github.com/urfave/cli/v3"
)
......@@ -19,20 +18,18 @@ var (
reArchiveDate = regexp.MustCompile(`Sisyphus archive date: ([0-9\-]+)`)
)
func deferredGetHTML() string {
func deferredGetHTML() (string, error) {
resp, err := http.Get(config.Env.Repo.DeferredInfoURL)
if err != nil {
color.Red("Ошибка получения данных")
os.Exit(1)
return "", fmt.Errorf("ошибка получения данных: %w", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
color.Red("Ошибка получения данных")
os.Exit(1)
return "", fmt.Errorf("ошибка чтения данных: %w", err)
}
return string(data)
return string(data), nil
}
func deferredLastUpdate(html string) string {
......@@ -43,7 +40,11 @@ func deferredLastUpdate(html string) string {
}
func DeferredLastUpdateCommand(ctx context.Context, cmd *cli.Command) error {
html := deferredGetHTML()
html, err := deferredGetHTML()
if err != nil {
color.Red(err.Error())
return err
}
lastUpdate := deferredLastUpdate(html)
fmt.Println(lastUpdate)
......@@ -59,7 +60,11 @@ func deferredArchiveDate(html string) string {
}
func DeferredArchiveDateCommand(ctx context.Context, cmd *cli.Command) error {
html := deferredGetHTML()
html, err := deferredGetHTML()
if err != nil {
color.Red(err.Error())
return err
}
archiveDate := deferredArchiveDate(html)
fmt.Println(archiveDate)
......@@ -68,7 +73,11 @@ func DeferredArchiveDateCommand(ctx context.Context, cmd *cli.Command) error {
}
func DeferredInfoCommand(ctx context.Context, cmd *cli.Command) error {
html := deferredGetHTML()
html, err := deferredGetHTML()
if err != nil {
color.Red(err.Error())
return err
}
lastUpdate := deferredLastUpdate(html)
archiveDate := deferredArchiveDate(html)
......
......@@ -16,8 +16,7 @@ func CommandList() *cli.Command {
Usage: "System Management",
Before: func(ctx context.Context, command *cli.Command) (context.Context, error) {
if !config.Env.IsRoot {
fmt.Printf("Эта команда требует root.\n")
return nil, fmt.Errorf("")
return nil, fmt.Errorf("эта команда требует root")
}
return ctx, 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