aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/util/io.go
blob: d7b849ba9a4eb719b8669d842082e0db0576695c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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()
}