diff --git a/cmd/graph.go b/cmd/graph.go index 4425d087b..957c0acb5 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -79,35 +79,33 @@ func aptlyGraph(cmd *commander.Command, args []string) error { return err } - defer func() { - _ = os.Remove(tempfilename) - }() - - if output != "" { - err = utils.CopyFile(tempfilename, output) + if output != "" { + err = utils.CopyFile(tempfilename, output) + _ = os.Remove(tempfilename) if err != nil { return fmt.Errorf("unable to copy %s -> %s: %s", tempfilename, output, err) } fmt.Printf("Output saved to %s\n", output) - } else { - command := getOpenCommand() - fmt.Printf("Rendered to %s file: %s, trying to open it with: %s %s...\n", format, tempfilename, command, tempfilename) - - 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 nil + } + + openCommand := getOpenCommand() + fmt.Printf("Rendered to %s file: %s, trying to open it with: %s %s...\n", format, tempfilename, openCommand, tempfilename) + + openCommandArgs := strings.Split(openCommand, " ") + + viewer := exec.Command(openCommandArgs[0], append(openCommandArgs[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 }