diff options
Diffstat (limited to 'client/go/util/io.go')
-rw-r--r-- | client/go/util/io.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/client/go/util/io.go b/client/go/util/io.go new file mode 100644 index 00000000000..d7b849ba9a4 --- /dev/null +++ b/client/go/util/io.go @@ -0,0 +1,39 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// File utilities. +// Author: bratseth + +package util + +import ( + "bytes" + "errors" + "io" + "os" + "strings" +) + +// Returns true if the given path exists +func PathExists(path string) bool { + _, err := os.Stat(path) + return ! errors.Is(err, os.ErrNotExist) +} + +// Returns true is the given path points to an existing directory +func IsDirectory(path string) bool { + info, err := os.Stat(path) + return ! errors.Is(err, os.ErrNotExist) && info.IsDir() +} + +// Returns the content of a reader as a string +func ReaderToString(reader io.Reader) string { + buffer := new(strings.Builder) + io.Copy(buffer, reader) + return buffer.String() +} + +// Returns the content of a reader as a byte array +func ReaderToBytes(reader io.Reader) []byte { + buffer := new(bytes.Buffer) + buffer.ReadFrom(reader) + return buffer.Bytes() +} |