From e458a519968295c9bb7944444f4e432c476f0170 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 4 Dec 2019 13:35:36 -0800 Subject: [PATCH] add livelog copy function --- livelog/copy.go | 38 ++++++++++++++++++++++++++++++++++++++ livelog/copy_test.go | 5 +++++ manifest/env.go | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 livelog/copy.go create mode 100644 livelog/copy_test.go diff --git a/livelog/copy.go b/livelog/copy.go new file mode 100644 index 0000000..8c739a6 --- /dev/null +++ b/livelog/copy.go @@ -0,0 +1,38 @@ +// Copyright 2019 Drone.IO Inc. All rights reserved. +// Use of this source code is governed by the Polyform License +// that can be found in the LICENSE file. + +package livelog + +import ( + "bufio" + "io" +) + +// Copy copies from src to dst and removes until either EOF +// is reached on src or an error occurs. +func Copy(dst io.Writer, src io.ReadCloser) error { + r := bufio.NewReader(src) + for { + bytes, err := r.ReadBytes('\n') + if _, err := dst.Write(bytes); err != nil { + return err + } + if flusher, ok := dst.(Flusher); ok { + flusher.Flush() + } + if err != nil { + if err != io.EOF { + return err + } + return nil + } + } +} + +// The Flusher interface is implemented by an io.Writer that +// flushes buffered data to the client. +type Flusher interface { + // Flush sends any buffered data to the client. + Flush() error +} diff --git a/livelog/copy_test.go b/livelog/copy_test.go new file mode 100644 index 0000000..444d865 --- /dev/null +++ b/livelog/copy_test.go @@ -0,0 +1,5 @@ +// Copyright 2019 Drone.IO Inc. All rights reserved. +// Use of this source code is governed by the Polyform License +// that can be found in the LICENSE file. + +package livelog diff --git a/manifest/env.go b/manifest/env.go index db71149..d466527 100644 --- a/manifest/env.go +++ b/manifest/env.go @@ -13,7 +13,7 @@ type ( Secret string `json:"from_secret,omitempty" yaml:"from_secret"` } - // variable is a tempoary type used to unmarshal + // variable is a temporary type used to unmarshal // variables with references to secrets. variable struct { Value string