diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..0e32bb7 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +golang 1.18.1 diff --git a/README.md b/README.md index 87c4d37..4c8c3f5 100644 --- a/README.md +++ b/README.md @@ -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) } } ``` diff --git a/cli.go b/cli.go index ec2b8da..6c1b608 100644 --- a/cli.go +++ b/cli.go @@ -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 diff --git a/go.mod b/go.mod index 93497c3..fed4133 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,3 @@ module github.com/jritsema/gotoolbox -go 1.15 - +go 1.18