Skip to content

Commit

Permalink
adds output argument to ExecCmd (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jritsema authored May 2, 2022
1 parent 3ea5a04 commit 11f06e7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.18.1
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,41 @@ A kitchen sink of Go tools that I've found useful. Uses only the standard librar
go get github.com/jritsema/gotoolbox
```

### utils
### utilities

```go
package main

import utils "github.com/jritsema/gotoolbox"
import "github.com/jritsema/gotoolbox"

func main() {

s := []string{"a", "b", "c"}
if utils.SliceContains(&s, "b") {
if gotoolbox.SliceContainsLike(&s, "b") {
fmt.Println("b exists")
}

err := utils.Retry(3, 1, func() error {
err := gotoolbox.Retry(3, 1, func() error {
return callBrittleAPI()
})
if err != nil {
fmt.Println("callBrittleAPI failed after 3 retries: %w", err)
}

config, err := utils.ReadJSONFile("config.json")
f := "config.json"
if !gotoolbox.IsDirectory(f) && gotoolbox.FileExists(f) {
config, err := gotoolbox.ReadJSONFile(f)
if err != nil {
fmt.Println("error reading json file: %w", err)
}
}

value := gotoolbox.GetEnvWithDefault("MY_ENVVAR", "true")

command := exec.Command("docker", "build", "-t", "foo", ".")
err = gotoolbox.ExecCmd(command, true)
if err != nil {
fmt.Println("error reading json file: %w", err)
fmt.Println("error executing command: %w", err)
}
}
```
Expand Down
7 changes: 6 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ func GetEnvWithDefault(key, defaultValue string) string {
}

//ExecCmd executes a command and waits
func ExecCmd(cmd *exec.Cmd) error {
func ExecCmd(cmd *exec.Cmd, output bool) error {

var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf

if output {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}

err := cmd.Start()
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module github.com/jritsema/gotoolbox

go 1.15

go 1.18

0 comments on commit 11f06e7

Please sign in to comment.