Skip to content

Commit

Permalink
compress temp files and use bash as default shell
Browse files Browse the repository at this point in the history
this uses gzip.BestSpeed as the compression level.
This has some CPU cost but BestSpeed is very efficient and
reduces the size of tmp files for my examples by 2-4X.
  • Loading branch information
brentp committed Mar 10, 2017
1 parent b3bfcfc commit 36f12ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.3.7
=====
+ compress temporary files with gzip (BestSpeed)
+ default to bash instead of sh if it exists and SHELL is not specified

0.3.6
=====
+ output gargs version in help.
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// Version is the current version
const Version = "0.3.6"
const Version = "0.3.7"

// ExitCode is the highest exit code seen in any command
var ExitCode = 0
Expand Down
23 changes: 18 additions & 5 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package process
import (
"bufio"
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -31,7 +32,11 @@ var prefix = fmt.Sprintf("gargs.%d.", os.Getpid())
func getShell() string {
shell := os.Getenv("SHELL")
if shell == "" {
shell = "sh"
if _, err := os.Stat("/bin/bash"); err == nil {
shell = "bash"
} else {
shell = "sh"
}
}
return shell
}
Expand Down Expand Up @@ -219,15 +224,21 @@ func oneRun(command string, callback CallBack, env []string) *Command {
if err != nil {
return newCommand(bufio.NewReader(bytes.NewReader(res)), tmp, command, err)
}
btmp := bufio.NewWriter(tmp)
_, err = io.CopyBuffer(btmp, bpipe, res)

gtmp, err := gzip.NewWriterLevel(tmp, gzip.BestSpeed)
if err != nil {
return newCommand(bufio.NewReader(bytes.NewReader(res)), tmp, command, err)
}

_, err = io.CopyBuffer(gtmp, bpipe, res)
if err != nil {
return newCommand(bufio.NewReader(bytes.NewReader(res)), tmp, command, err)
}
if c, ok := opipe.(io.ReadCloser); ok {
c.Close()
}
btmp.Flush()
gtmp.Flush()
gtmp.Close()
_, err = tmp.Seek(0, 0)
if err == nil {
err = cmd.Wait()
Expand All @@ -237,7 +248,9 @@ func oneRun(command string, callback CallBack, env []string) *Command {
err = e
}
}
return newCommand(bufio.NewReader(tmp), tmp, command, err)
var grdr *gzip.Reader
grdr, err = gzip.NewReader(tmp)
return newCommand(bufio.NewReader(grdr), tmp, command, err)
}

// istring holds a command and an index.
Expand Down

0 comments on commit 36f12ab

Please sign in to comment.