ask before removing existing socket

automatically removes the socket after 5 seconds of no input so that its
functions headlessly.
This commit is contained in:
Harvey Tindall 2020-08-31 20:14:14 +01:00
parent 3918d2f30e
commit f4090c1f07
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
2 changed files with 150 additions and 129 deletions

43
main.go
View File

@ -64,21 +64,21 @@ func NewPlayer(conn *dbus.Conn, name string) (p *Player) {
for key, val := range knownPlayers {
if strings.Contains(name, key) {
playerName = val
if val == "Browser" {
break
}
}
if playerName == "Browser" {
file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if err == nil {
cmd := string(file)
for k, v := range knownBrowsers {
if strings.Contains(cmd, k) {
playerName = v
for key, val := range knownBrowsers {
if strings.Contains(cmd, key) {
playerName = val
break
}
}
}
}
break
}
}
p = &Player{
player: conn.Object(name, PATH),
conn: conn,
@ -382,10 +382,11 @@ func main() {
fmt.Println("Response:")
fmt.Printf(response)
}
} else {
os.Exit(0)
}
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
log.Fatalln("Error connecting to DBus:", err)
}
players := &PlayerList{
conn: conn,
@ -397,13 +398,34 @@ func main() {
lastLine := ""
// fmt.Println("New array", players)
// Start command listener
if _, err := os.Stat(SOCK); err == nil {
fmt.Printf("Socket %s already exists, this could mean waybar-mpris is already running.\nStarting this instance will overwrite the file, possibly stopping other instances from accepting commands.\n", SOCK)
var input string
ignoreChoice := false
fmt.Printf("Continue? [y/n]: ")
go func() {
fmt.Scanln(&input)
if strings.Contains(input, "y") && !ignoreChoice {
os.Remove(SOCK)
}
}()
time.Sleep(5 * time.Second)
if input == "" {
fmt.Printf("\nRemoving due to lack of input.\n")
ignoreChoice = true
os.Remove(SOCK)
}
}
go func() {
listener, err := net.Listen("unix", SOCK)
if err != nil {
log.Fatalln("Couldn't establish socket connection at", SOCK)
}
defer listener.Close()
defer func() {
listener.Close()
os.Remove(SOCK)
}()
for {
con, err := listener.Accept()
if err != nil {
@ -510,5 +532,4 @@ func main() {
}
}
}
}
}

Binary file not shown.