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