-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f47c186
commit 314b163
Showing
6 changed files
with
162 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,59 @@ | ||
package servicebindings | ||
|
||
import "bytes" | ||
import ( | ||
"os" | ||
) | ||
|
||
// Entry represents the read-only content of a binding entry. | ||
type Entry struct { | ||
data *bytes.Buffer | ||
path string | ||
file *os.File | ||
} | ||
|
||
// MakeEntry returns an Entry from the given content. | ||
func MakeEntry(data []byte) Entry { | ||
return Entry{ | ||
data: bytes.NewBuffer(data), | ||
// NewEntry returns a new Entry whose content is given by the file at the provided path. | ||
func NewEntry(path string) *Entry { | ||
return &Entry{ | ||
path: path, | ||
} | ||
} | ||
|
||
// Bytes returns the raw content of the entry. | ||
func (e Entry) Bytes() []byte { | ||
return e.data.Bytes() | ||
// ReadBytes reads the entire raw content of the entry. There is no need to call Close after calling ReadBytes. | ||
func (e *Entry) ReadBytes() ([]byte, error) { | ||
return os.ReadFile(e.path) | ||
} | ||
|
||
// String returns the content of the entry as a string. | ||
func (e Entry) String() string { | ||
return e.data.String() | ||
// ReadString reads the entire content of the entry as a string. There is no need to call Close after calling | ||
// ReadString. | ||
func (e *Entry) ReadString() (string, error) { | ||
bytes, err := e.ReadBytes() | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(bytes), nil | ||
} | ||
|
||
// Read reads the next len(p) bytes from the entry or until the entry is drained. The return value n is the number of | ||
// bytes read. If the entry has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil. | ||
func (e Entry) Read(p []byte) (int, error) { | ||
return e.data.Read(p) | ||
// Read reads up to len(b) bytes from the entry. It returns the number of bytes read and any error encountered. At end | ||
// of entry data, Read returns 0, io.EOF. | ||
// Close must be called when all read operations are complete. | ||
func (e *Entry) Read(b []byte) (int, error) { | ||
if e.file == nil { | ||
file, err := os.Open(e.path) | ||
if err != nil { | ||
return 0, err | ||
} | ||
e.file = file | ||
} | ||
return e.file.Read(b) | ||
} | ||
|
||
// Close closes the entry and resets it for reading. After calling Close, any subsequent calls to Read will read entry | ||
// data from the beginning. Close is idempotent. | ||
func (e *Entry) Close() error { | ||
if e.file == nil { | ||
return nil | ||
} | ||
defer func() { | ||
e.file = nil | ||
}() | ||
return e.file.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.