Reroute logs printed to stdout

pull/4115/head
Sharif Elgamal 2019-04-18 17:19:20 -07:00
parent 3746fb9994
commit 2c2e7c1271
No known key found for this signature in database
GPG Key ID: 23CC0225BD9FD702
1 changed files with 18 additions and 0 deletions

View File

@ -17,7 +17,10 @@ limitations under the License.
package machine
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
@ -288,6 +291,21 @@ func getDstPath(image, dst string) (string, error) {
// CacheImage caches an image
func CacheImage(image, dst string) error {
// There are go-containerregistry calls here that result in
// ugly log messages getting printed to stdout. Capture
// stdout instead and writing it to info.
r, w, err := os.Pipe()
if err != nil {
return errors.Wrap(err, "opening writing buffer")
}
log.SetOutput(w)
defer func() {
log.SetOutput(os.Stdout)
var buf bytes.Buffer
io.Copy(&buf, r)
glog.Infof(buf.String())
}()
glog.Infof("Attempting to cache image: %s at %s\n", image, dst)
if _, err := os.Stat(dst); err == nil {
return nil