Skip to content

Commit

Permalink
Fix: Graph deleted before aptly exits
Browse files Browse the repository at this point in the history
The temporary output file is now only deleted after copying it to the output location.

Fixes: #1213
  • Loading branch information
reglim committed Sep 13, 2023
1 parent 214e907 commit 68a1237
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 68a1237

Please sign in to comment.