Move log to user owned directory

This commit is contained in:
AI Bot
2026-05-03 21:29:49 +02:00
committed by Riyyi
parent 7c4b0eda8b
commit b04869f0e1
4 changed files with 38 additions and 15 deletions
+2 -1
View File
@@ -89,7 +89,8 @@ If the pacman database is older than 24 hours, it is automatically refreshed.
### Logging ### Logging
Operations are logged to `/var/log/declpac.log`. Operation are logged to `$XDG_STATE_HOME/declpac.log`
(or `~/.local/state/declpac.log` on fallback)
## Troubleshooting ## Troubleshooting
+5 -13
View File
@@ -6,6 +6,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/Riyyi/declpac/pkg/lib"
) )
var ErrEmptyList = errors.New("package list is empty") var ErrEmptyList = errors.New("package list is empty")
@@ -28,7 +30,7 @@ func ReadPackages(stateFiles []string) (map[string]bool, error) {
packages := make(map[string]bool) packages := make(map[string]bool)
for _, file := range stateFiles { for _, file := range stateFiles {
expanded := expandPath(file) expanded := lib.ExpandPath(file)
if err := readStateFile(expanded, packages); err != nil { if err := readStateFile(expanded, packages); err != nil {
return nil, err return nil, err
} }
@@ -51,27 +53,17 @@ func ReadPackages(stateFiles []string) (map[string]bool, error) {
// ----------------------------------------- // -----------------------------------------
// private // private
func expandPath(path string) string {
if strings.HasPrefix(path, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return path
}
return filepath.Join(home, path[2:])
}
return path
}
func fileExists(path string) bool { func fileExists(path string) bool {
_, err := os.Stat(path) _, err := os.Stat(path)
return err == nil return err == nil
} }
func getImplicitStateFile() string { func getImplicitStateFile() string {
cfgDir, _ := os.UserConfigDir() cfgDir := os.Getenv("XDG_CONFIG_HOME")
if cfgDir == "" { if cfgDir == "" {
cfgDir = "~/.config" cfgDir = "~/.config"
} }
cfgDir = lib.ExpandPath(cfgDir)
return filepath.Join(cfgDir, "declpac") return filepath.Join(cfgDir, "declpac")
} }
+21
View File
@@ -0,0 +1,21 @@
package lib
import (
"os"
"path/filepath"
"strings"
)
// -----------------------------------------
// public
func ExpandPath(path string) string {
if strings.HasPrefix(path, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return path
}
return filepath.Join(home, path[2:])
}
return path
}
+10 -1
View File
@@ -8,6 +8,8 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/Riyyi/declpac/pkg/lib"
) )
var logFile *os.File var logFile *os.File
@@ -41,11 +43,18 @@ func GetLogWriter() io.Writer {
} }
func OpenLog() error { func OpenLog() error {
logPath := filepath.Join("/var/log", "declpac.log") stateDir := os.Getenv("XDG_STATE_HOME")
if stateDir == "" {
stateDir = "~/.local/state"
}
stateDir = lib.ExpandPath(stateDir)
logPath := filepath.Join(stateDir, "declpac.log")
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
return err return err
} }
logFile = f logFile = f
writeTimestamp() writeTimestamp()
return nil return nil