Skip to content

Commit

Permalink
Merge pull request #48 from rhcarvalho/refactor-archive
Browse files Browse the repository at this point in the history
Extract archive logic to a function
  • Loading branch information
rhcarvalho authored Sep 8, 2016
2 parents b46da2c + 92c881a commit c6f4cc6
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func readSpaceSeparated(r io.Reader) ([]string, error) {
return words, nil
}

// archive archives the target path to a tar.gz file.
func archive(path string) error {
var stdout, stderr bytes.Buffer
cmd := exec.Command("tar", "-czf", path+".tar.gz", path)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
return cmd.Run()
}

func main() {
flag.Parse()

Expand Down Expand Up @@ -114,16 +123,11 @@ func main() {
// basePath. After that, logs will go only to stderr.
fileOnlyLogger.Printf("Dumped system information to: %s", basePath)

var stdout, stderr bytes.Buffer

cmd := exec.Command("tar", "-czf", basePath+".tar.gz", basePath)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Printf("There was an error archiving the dumped data, so the dumped system information is stored unarchived in: %s", basePath)
if err := archive(basePath); err != nil {
fileOnlyLogger.Printf("Could not create data archive: %v", err)
log.Printf("Could not archive dump data, unarchived data in: %s", basePath)
return
}

// The archive was created successfully, remove basePath. The
// error from os.RemoveAll is intentionally ignored, since there
// is no useful action we can do, and we don't need to confuse
Expand Down

0 comments on commit c6f4cc6

Please sign in to comment.