Skip to content

Commit

Permalink
Merge branch 'repect-xdg'
Browse files Browse the repository at this point in the history
  • Loading branch information
lawl committed Jul 19, 2020
2 parents 0bef36a + 23e3235 commit 37b35ce
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 31 deletions.
25 changes: 21 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ type config struct {
EnableUpdates bool
}

const configDir = ".config/noisetorch/"
const configFile = "config.toml"

func initializeConfigIfNot() {
log.Println("Checking if config needs to be initialized")
conf := config{Threshold: 95, DisplayMonitorSources: false, EnableUpdates: true}
configdir := filepath.Join(os.Getenv("HOME"), configDir)
configdir := configDir()
ok, err := exists(configdir)
if err != nil {
log.Fatalf("Couldn't check if config directory exists: %v\n", err)
Expand All @@ -45,7 +44,7 @@ func initializeConfigIfNot() {
}

func readConfig() *config {
f := filepath.Join(os.Getenv("HOME"), configDir, configFile)
f := filepath.Join(configDir(), configFile)
config := config{}
if _, err := toml.DecodeFile(f, &config); err != nil {
log.Fatalf("Couldn't read config file: %v\n", err)
Expand All @@ -55,14 +54,18 @@ func readConfig() *config {
}

func writeConfig(conf *config) {
f := filepath.Join(os.Getenv("HOME"), configDir, configFile)
f := filepath.Join(configDir(), configFile)
var buffer bytes.Buffer
if err := toml.NewEncoder(&buffer).Encode(&conf); err != nil {
log.Fatalf("Couldn't write config file: %v\n", err)
}
ioutil.WriteFile(f, []byte(buffer.String()), 0644)
}

func configDir() string {
return filepath.Join(xdgOrFallback("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), ".config")), "noisetorch")
}

func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
Expand All @@ -73,3 +76,17 @@ func exists(path string) (bool, error) {
}
return false, err
}

func xdgOrFallback(xdg string, fallback string) string {
dir := os.Getenv(xdg)
if dir != "" {
if ok, err := exists(dir); ok && err == nil {
log.Printf("Resolved $%s to '%s'\n", xdg, dir)
return dir
}

}

log.Printf("Couldn't resolve $%s falling back to '%s'\n", xdg, fallback)
return fallback
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
gioui.org v0.0.0-20200630184602-223f8fd40ae4 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/aarzilli/nucular v0.0.0-20200615134801-81910c722bba
github.com/lawl/pulseaudio v0.0.0-20200704145757-7d4b4b92e7b7
github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad h1:eMxs9EL0Pv
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/lawl/pulseaudio v0.0.0-20200704145757-7d4b4b92e7b7 h1:LjLOowMTfYESP3XW5/KV6TaQVuhdvkBHJoEaReGds6M=
github.com/lawl/pulseaudio v0.0.0-20200704145757-7d4b4b92e7b7/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f h1:MA90ko9/uptNdk2wRHAqm1fpyCogyFTKo162qPv2FgA=
github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
4 changes: 2 additions & 2 deletions rlimit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
Expand All @@ -15,8 +16,7 @@ import (
const rlimitRTTime = 15

func getPulsePid() (int, error) {
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
pulsepidfile := filepath.Join(runtimeDir, "pulse/pid")
pulsepidfile := filepath.Join(xdgOrFallback("XDG_RUNTIME_DIR", fmt.Sprintf("/run/user/%d", os.Getuid())), "pulse/pid")
pidbuf, err := ioutil.ReadFile(pulsepidfile)
if err != nil {
return 0, err
Expand Down
6 changes: 4 additions & 2 deletions vendor/github.com/lawl/pulseaudio/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 36 additions & 21 deletions vendor/github.com/lawl/pulseaudio/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ github.com/golang/freetype/truetype
# github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad
github.com/hashicorp/golang-lru
github.com/hashicorp/golang-lru/simplelru
# github.com/lawl/pulseaudio v0.0.0-20200704145757-7d4b4b92e7b7
# github.com/lawl/pulseaudio v0.0.0-20200719094555-c019d8a2304f
## explicit
github.com/lawl/pulseaudio
# golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
Expand Down

0 comments on commit 37b35ce

Please sign in to comment.