Browse Source

Add global logger to tools.

Ben 1 week ago
parent
commit
8b58cc1669
4 changed files with 24 additions and 15 deletions
  1. 1 2
      .gitignore
  2. 2 0
      Dockerfile
  3. 9 13
      tools/docker.go
  4. 12 0
      tools/tools.go

+ 1 - 2
.gitignore

@@ -1,5 +1,4 @@
 workspace
 test
 pkg/job
-main.go
-Dockerfile
+main.go

+ 2 - 0
Dockerfile

@@ -0,0 +1,2 @@
+FROM golang:latest
+

+ 9 - 13
tools/docker.go

@@ -4,7 +4,6 @@ import (
 	"context"
 	"io"
 	"net/netip"
-	"os"
 	"strings"
 	"time"
 
@@ -34,7 +33,6 @@ type (
 	dockerImage struct {
 		docker *docker
 		auth   string
-		out    io.Writer
 	}
 	dockerContainer struct {
 		docker     *docker
@@ -340,13 +338,11 @@ func (t *dockerContainer) Remove() error {
 func (t *docker) Image() *dockerImage {
 	return &dockerImage{
 		docker: t,
-		out:    os.Stderr,
 	}
 }
 
 // RegistryAuth is the base64 encoded credentials for the registry
 func (t *dockerImage) Auth(RegistryAuth string) { t.auth = RegistryAuth }
-func (t *dockerImage) Out(out io.Writer)        { t.out = out }
 func (t *dockerImage) Build(tag, path, dockerfile string) error {
 	cli, err := t.docker.client()
 	if err != nil {
@@ -372,9 +368,9 @@ func (t *dockerImage) Build(tag, path, dockerfile string) error {
 	}
 	defer buildResponse.Body.Close()
 
-	if t.out != nil {
-		fd, isTerminal := term.GetFdInfo(t.out)
-		return jsonmessage.DisplayJSONMessagesStream(buildResponse.Body, t.out, fd, isTerminal, nil)
+	if log != nil {
+		fd, isTerminal := term.GetFdInfo(log)
+		return jsonmessage.DisplayJSONMessagesStream(buildResponse.Body, log, fd, isTerminal, nil)
 	}
 
 	return nil
@@ -413,9 +409,9 @@ func (t *dockerImage) Push(image string) error {
 	}
 	defer res.Close()
 
-	if t.out != nil {
-		fd, isTerminal := term.GetFdInfo(t.out)
-		return jsonmessage.DisplayJSONMessagesStream(res, t.out, fd, isTerminal, nil)
+	if log != nil {
+		fd, isTerminal := term.GetFdInfo(log)
+		return jsonmessage.DisplayJSONMessagesStream(res, log, fd, isTerminal, nil)
 	}
 
 	return nil
@@ -454,9 +450,9 @@ func (t *dockerImage) Pull(image string, timeout time.Duration) error {
 
 	defer res.Close()
 
-	if t.out != nil {
-		fd, isTerminal := term.GetFdInfo(t.out)
-		return jsonmessage.DisplayJSONMessagesStream(res, t.out, fd, isTerminal, nil)
+	if log != nil {
+		fd, isTerminal := term.GetFdInfo(log)
+		return jsonmessage.DisplayJSONMessagesStream(res, log, fd, isTerminal, nil)
 	} else {
 		err = res.Wait(ctx)
 		if err != nil {

+ 12 - 0
tools/tools.go

@@ -1 +1,13 @@
 package tools
+
+import (
+	"io"
+
+	stdlog "log"
+)
+
+var log = stdlog.New(io.Discard, "", stdlog.LstdFlags).Writer()
+
+func SetLogger (writer io.Writer) {
+	log = writer
+}