Skip to content

Commit

Permalink
feat(restart): check if process is running (#1623)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrie25 authored Apr 28, 2022
1 parent ff18628 commit 249cf69
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/cmd/restart.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,21 +17,33 @@ func RestartSpotify(flags ...string) {

switch runtime.GOOS {
case "windows":
exec.Command("taskkill", "/F", "/IM", "spotify.exe").Run()
if isAppX {
ps, _ := exec.LookPath("powershell.exe")
exe := filepath.Join(os.Getenv("LOCALAPPDATA"), "Microsoft", "WindowsApps", "Spotify.exe")
flags = append([]string{"-NoProfile", "-NonInteractive", `& "` + exe + `" --app-directory="` + appDestPath + `"`}, flags...)
exec.Command(ps, flags...).Start()
} else {
exec.Command(filepath.Join(spotifyPath, "spotify.exe"), flags...).Start()
isRunning := exec.Command("tasklist", "/FI", "ImageName eq spotify.exe")
result, _ := isRunning.Output()
if !bytes.Contains(result, []byte("No tasks are running")) {
exec.Command("taskkill", "/F", "/IM", "spotify.exe").Run()
if isAppX {
ps, _ := exec.LookPath("powershell.exe")
exe := filepath.Join(os.Getenv("LOCALAPPDATA"), "Microsoft", "WindowsApps", "Spotify.exe")
flags = append([]string{"-NoProfile", "-NonInteractive", `& "` + exe + `" --app-directory="` + appDestPath + `"`}, flags...)
exec.Command(ps, flags...).Start()
} else {
exec.Command(filepath.Join(spotifyPath, "spotify.exe"), flags...).Start()
}
}
case "linux":
exec.Command("pkill", "spotify").Run()
exec.Command(filepath.Join(spotifyPath, "spotify"), flags...).Start()
isRunning := exec.Command("pgrep", "spotify")
_, err := isRunning.Output()
if err == nil {
exec.Command("pkill", "spotify").Run()
exec.Command(filepath.Join(spotifyPath, "spotify"), flags...).Start()
}
case "darwin":
exec.Command("pkill", "Spotify").Run()
flags = append([]string{"-a", "/Applications/Spotify.app"}, flags...)
exec.Command("open", flags...).Start()
isRunning := exec.Command("sh", "-c", "ps aux | grep 'Spotify' | grep -v grep")
_, err := isRunning.CombinedOutput()
if err == nil {
exec.Command("pkill", "Spotify").Run()
flags = append([]string{"-a", "/Applications/Spotify.app"}, flags...)
exec.Command("open", flags...).Start()
}
}
}

0 comments on commit 249cf69

Please sign in to comment.