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 91e5eaf
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"runtime"
"strings"
"time"

"github.com/aptly-dev/aptly/deb"
"github.com/aptly-dev/aptly/utils"
Expand Down Expand Up @@ -79,36 +78,26 @@ func aptlyGraph(cmd *commander.Command, args []string) error {
return err
}

defer func() {
_ = os.Remove(tempfilename)
}()

if output != "" {
err = utils.CopyFile(tempfilename, output)
if output != "" {

Check failure on line 81 in cmd/graph.go

View workflow job for this annotation

GitHub Actions / test (1.18, no, false)

File is not `gofmt`-ed with `-s` (gofmt)

Check failure on line 81 in cmd/graph.go

View workflow job for this annotation

GitHub Actions / test (1.18, yes, false)

File is not `gofmt`-ed with `-s` (gofmt)
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, " ")

// The process will continue running even after aptly has exited.
viewer := exec.Command(openCommandArgs[0], append(openCommandArgs[1:], tempfilename)...)
viewer.Stderr = os.Stderr
err = viewer.Start()
return err
}

Expand Down

0 comments on commit 91e5eaf

Please sign in to comment.