Move log to user owned directory
This commit is contained in:
+5
-13
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user