diff --git a/spicetify.go b/spicetify.go index c3c5833973..c3a34949ad 100644 --- a/spicetify.go +++ b/spicetify.go @@ -5,11 +5,14 @@ import ( "io/ioutil" "log" "os" + "os/exec" "runtime" + "strings" "sync" colorable "github.com/mattn/go-colorable" "github.com/spicetify/spicetify-cli/src/cmd" + spotifystatus "github.com/spicetify/spicetify-cli/src/status/spotify" "github.com/spicetify/spicetify-cli/src/utils" ) @@ -96,6 +99,28 @@ func init() { appFocus = true styleFocus = true liveUpdate = true + case "--check-update": + upgradeStatus := cmd.Upgrade(version) + if upgradeStatus { + ex, err := os.Executable() + if err != nil { + ex = "spicetify" + } + + spotStat := spotifystatus.Get(utils.FindAppPath()) + cmds := []string{"backup", "apply"} + if !spotStat.IsBackupable() { + cmds = append([]string{"restore"}, cmds...) + } + + cmd := exec.Command(ex, cmds...) + utils.CmdScanner(cmd) + + cmd = exec.Command(ex, strings.Join(commands[:], " ")) + utils.CmdScanner(cmd) + + os.Exit(0) + } } } diff --git a/src/cmd/upgrade.go b/src/cmd/upgrade.go index b4a0d987f5..c1afea93dd 100644 --- a/src/cmd/upgrade.go +++ b/src/cmd/upgrade.go @@ -11,13 +11,13 @@ import ( "github.com/spicetify/spicetify-cli/src/utils" ) -func Upgrade(currentVersion string) { +func Upgrade(currentVersion string) bool { utils.PrintBold("Fetch latest release info:") tagName, err := utils.FetchLatestTag() if err != nil { utils.PrintError("Cannot fetch latest release info") utils.PrintError(err.Error()) - return + return false } utils.PrintGreen("OK") @@ -25,7 +25,7 @@ func Upgrade(currentVersion string) { utils.PrintInfo("Latest release: " + tagName) if currentVersion == tagName { utils.PrintSuccess("Already up-to-date.") - return + return false } var assetURL string = "https://github.com/spicetify/spicetify-cli/releases/download/v" + tagName + "/spicetify-" + tagName @@ -105,6 +105,7 @@ func Upgrade(currentVersion string) { utils.PrintGreen("OK") utils.PrintSuccess("spicetify is up-to-date.") utils.PrintInfo(`Please run "spicetify restore backup apply" to receive new features and bug fixes`) + return true } func permissionError(err error) { diff --git a/src/utils/scanner.go b/src/utils/scanner.go new file mode 100644 index 0000000000..18a13da097 --- /dev/null +++ b/src/utils/scanner.go @@ -0,0 +1,20 @@ +package utils + +import ( + "bufio" + "fmt" + "os/exec" +) + +// CmdScanner is a helper function to scan output from exec.Cmd +func CmdScanner(cmd *exec.Cmd) { + stdout, _ := cmd.StdoutPipe() + cmd.Start() + + scanner := bufio.NewScanner(stdout) + for scanner.Scan() { + m := scanner.Text() + fmt.Println(m) + } + cmd.Wait() +}