mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-08 05:50:47 +00:00
Merge pull request #512 from smira/500-xdg-open
Customize viewer per platform
This commit is contained in:
+34
-3
@@ -8,6 +8,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/smira/aptly/deb"
|
"github.com/smira/aptly/deb"
|
||||||
"github.com/smira/aptly/utils"
|
"github.com/smira/aptly/utils"
|
||||||
@@ -77,23 +80,51 @@ func aptlyGraph(cmd *commander.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
_ = os.Remove(tempfilename)
|
||||||
|
}()
|
||||||
|
|
||||||
if output != "" {
|
if output != "" {
|
||||||
err = utils.CopyFile(tempfilename, output)
|
err = utils.CopyFile(tempfilename, output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to copy %s -> %s: %s", tempfilename, output, err)
|
return fmt.Errorf("unable to copy %s -> %s: %s", tempfilename, output, err)
|
||||||
}
|
}
|
||||||
_ = os.Remove(tempfilename)
|
|
||||||
|
|
||||||
fmt.Printf("Output saved to %s\n", output)
|
fmt.Printf("Output saved to %s\n", output)
|
||||||
} else {
|
} 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
|
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 {
|
func makeCmdGraph() *commander.Command {
|
||||||
cmd := &commander.Command{
|
cmd := &commander.Command{
|
||||||
Run: aptlyGraph,
|
Run: aptlyGraph,
|
||||||
|
|||||||
Reference in New Issue
Block a user