From e3e68b9f22e58417bdd935bf212df7d359130ff3 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Thu, 23 Mar 2017 00:28:01 +0300 Subject: [PATCH] Customize viewer per platform --- cmd/graph.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/cmd/graph.go b/cmd/graph.go index 9da66a26..f7575b3b 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -8,6 +8,9 @@ import ( "os" "os/exec" "path/filepath" + "runtime" + "strings" + "time" "github.com/smira/aptly/deb" "github.com/smira/aptly/utils" @@ -77,23 +80,51 @@ func aptlyGraph(cmd *commander.Command, args []string) error { return err } + defer func() { + _ = os.Remove(tempfilename) + }() + if output != "" { err = utils.CopyFile(tempfilename, output) if err != nil { return fmt.Errorf("unable to copy %s -> %s: %s", tempfilename, output, err) } - _ = os.Remove(tempfilename) fmt.Printf("Output saved to %s\n", output) } else { - fmt.Printf("Rendered to %s file: %s, trying to open it...\n", format, tempfilename) + command := getOpenCommand() + fmt.Printf("Rendered to %s file: %s, trying to open it with: %s %s...\n", format, tempfilename, command, tempfilename) - _ = exec.Command("open", tempfilename).Run() + args := strings.Split(command, " ") + + viewer := exec.Command(args[0], append(args[1:], tempfilename)...) + viewer.Stderr = os.Stderr + if err = viewer.Start(); err == nil { + // Wait for a second so that the visualizer has a chance to + // open the input file. This needs to be done even if we're + // waiting for the visualizer as it can be just a wrapper that + // spawns a browser tab and returns right away. + defer func(t <-chan time.Time) { + <-t + }(time.After(time.Second)) + } } return err } +// getOpenCommand tries to guess command to open image for OS +func getOpenCommand() string { + switch runtime.GOOS { + case "darwin": + return "/usr/bin/open" + case "windows": + return "cmd /c start" + default: + return "xdg-open" + } +} + func makeCmdGraph() *commander.Command { cmd := &commander.Command{ Run: aptlyGraph,