Browse Source

Move log to user owned directory

master
AI Bot 1 week ago committed by Riyyi
parent
commit
b04869f0e1
  1. 3
      README.md
  2. 18
      pkg/input/input.go
  3. 21
      pkg/lib/lib.go
  4. 11
      pkg/log/log.go

3
README.md

@ -89,7 +89,8 @@ If the pacman database is older than 24 hours, it is automatically refreshed.
### 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

18
pkg/input/input.go

@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"strings"
"github.com/Riyyi/declpac/pkg/lib"
)
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)
for _, file := range stateFiles {
expanded := expandPath(file)
expanded := lib.ExpandPath(file)
if err := readStateFile(expanded, packages); err != nil {
return nil, err
}
@ -51,27 +53,17 @@ func ReadPackages(stateFiles []string) (map[string]bool, error) {
// -----------------------------------------
// 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 {
_, err := os.Stat(path)
return err == nil
}
func getImplicitStateFile() string {
cfgDir, _ := os.UserConfigDir()
cfgDir := os.Getenv("XDG_CONFIG_HOME")
if cfgDir == "" {
cfgDir = "~/.config"
}
cfgDir = lib.ExpandPath(cfgDir)
return filepath.Join(cfgDir, "declpac")
}

21
pkg/lib/lib.go

@ -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
}

11
pkg/log/log.go

@ -8,6 +8,8 @@ import (
"path/filepath"
"strings"
"time"
"github.com/Riyyi/declpac/pkg/lib"
)
var logFile *os.File
@ -41,11 +43,18 @@ func GetLogWriter() io.Writer {
}
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)
if err != nil {
return err
}
logFile = f
writeTimestamp()
return nil

Loading…
Cancel
Save