unhardcode data dir #115

This commit is contained in:
piotr
2024-02-08 01:20:03 +01:00
parent 71ce3b5d75
commit d4996af84a
4 changed files with 52 additions and 29 deletions

4
go.mod
View File

@@ -1,6 +1,6 @@
module github.com/nwg-piotr/nwg-drawer module github.com/nwg-piotr/nwg-drawer
go 1.21 go 1.22
require ( require (
github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37 github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37
@@ -15,5 +15,5 @@ require (
github.com/joshuarubin/lifecycle v1.1.4 // indirect github.com/joshuarubin/lifecycle v1.1.4 // indirect
go.uber.org/multierr v1.11.0 // indirect go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.6.0 // indirect golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect golang.org/x/sys v0.17.0 // indirect
) )

2
go.sum
View File

@@ -48,6 +48,8 @@ golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

55
main.go
View File

@@ -234,7 +234,7 @@ func main() {
// Otherwise the command may behave in two ways: // Otherwise the command may behave in two ways:
// 1. kill the running non-residennt instance and exit; // 1. kill the running non-residennt instance and exit;
// 2. die if a resident instance found. // 2. die if a resident instance found.
lockFilePath := path.Join(dataDir(), "nwg-drawer.lock") lockFilePath := path.Join(dataHome(), "nwg-drawer.lock")
lockFile, err := singleinstance.CreateLockFile(lockFilePath) lockFile, err := singleinstance.CreateLockFile(lockFilePath)
if err != nil { if err != nil {
pid, err := readTextFile(lockFilePath) pid, err := readTextFile(lockFilePath)
@@ -579,32 +579,37 @@ func main() {
} }
// Power Button Bar // Power Button Bar
if *pbExit != "" || *pbLock != "" || *pbPoweroff != "" || *pbReboot != "" || *pbSleep != "" { dDir := dataDir()
powerBarWrapper, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) if dDir != "" {
outerVBox.PackStart(powerBarWrapper, false, false, 0) if *pbExit != "" || *pbLock != "" || *pbPoweroff != "" || *pbReboot != "" || *pbSleep != "" {
powerButtonsWrapper, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) powerBarWrapper, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
powerBarWrapper.PackStart(powerButtonsWrapper, true, false, 12) outerVBox.PackStart(powerBarWrapper, false, false, 0)
powerButtonsWrapper, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
powerBarWrapper.PackStart(powerButtonsWrapper, true, false, 12)
if *pbLock != "" { if *pbLock != "" {
btn := powerButton("/usr/share/nwg-drawer/img/lock.svg", *pbLock) btn := powerButton(filepath.Join(dDir, "img/lock.svg"), *pbLock)
powerButtonsWrapper.PackStart(btn, true, false, 0) powerButtonsWrapper.PackStart(btn, true, false, 0)
} }
if *pbExit != "" { if *pbExit != "" {
btn := powerButton("/usr/share/nwg-drawer/img/exit.svg", *pbExit) btn := powerButton(filepath.Join(dDir, "img/exit.svg"), *pbExit)
powerButtonsWrapper.PackStart(btn, true, false, 0) powerButtonsWrapper.PackStart(btn, true, false, 0)
} }
if *pbReboot != "" { if *pbReboot != "" {
btn := powerButton("/usr/share/nwg-drawer/img/reboot.svg", *pbReboot) btn := powerButton(filepath.Join(dDir, "img/reboot.svg"), *pbReboot)
powerButtonsWrapper.PackStart(btn, true, false, 0) powerButtonsWrapper.PackStart(btn, true, false, 0)
} }
if *pbSleep != "" { if *pbSleep != "" {
btn := powerButton("/usr/share/nwg-drawer/img/sleep.svg", *pbSleep) btn := powerButton(filepath.Join(dDir, "img/sleep.svg"), *pbSleep)
powerButtonsWrapper.PackStart(btn, true, false, 0) powerButtonsWrapper.PackStart(btn, true, false, 0)
} }
if *pbPoweroff != "" { if *pbPoweroff != "" {
btn := powerButton("/usr/share/nwg-drawer/img/poweroff.svg", *pbPoweroff) btn := powerButton(filepath.Join(dDir, "img/poweroff.svg"), *pbPoweroff)
powerButtonsWrapper.PackStart(btn, true, false, 0) powerButtonsWrapper.PackStart(btn, true, false, 0)
}
} }
} else {
log.Warn("Couldn't find data dir, power bar icons unavailable")
} }
statusLineWrapper, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) statusLineWrapper, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)

View File

@@ -173,7 +173,7 @@ func configHome() string {
return path.Join(os.Getenv("HOME"), ".config") return path.Join(os.Getenv("HOME"), ".config")
} }
func dataDir() string { func dataHome() string {
var dir string var dir string
if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" { if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" {
dir = path.Join(xdgData, "nwg-drawer") dir = path.Join(xdgData, "nwg-drawer")
@@ -181,7 +181,7 @@ func dataDir() string {
dir = path.Join(home, ".local/share/nwg-drawer") dir = path.Join(home, ".local/share/nwg-drawer")
} }
log.Infof("Data dir: %s", dir) log.Infof("Data home: %s", dir)
createDir(dir) createDir(dir)
return dir return dir
@@ -233,6 +233,22 @@ func copyFile(src, dst string) error {
return os.Chmod(dst, srcinfo.Mode()) return os.Chmod(dst, srcinfo.Mode())
} }
func dataDir() string {
xdgDataDirs := os.Getenv("XDG_DATA_DIRS")
if xdgDataDirs == "" {
xdgDataDirs = "/usr/local/share/:/usr/share/"
}
for _, d := range strings.Split(xdgDataDirs, ":") {
p := filepath.Join(d, "nwg-drawer")
if pathExists(p) {
log.Infof("Data dir: %v", p)
return p
}
}
log.Warnf("Data dir not found")
return ""
}
func getAppDirs() []string { func getAppDirs() []string {
var dirs []string var dirs []string