github.com/drone/runner-go@v1.12.0/pipeline/streamer/console/console.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  // Package console provides a streamer that writes the pipeline
     6  // output to stdout.
     7  package console
     8  
     9  import (
    10  	"context"
    11  	"io"
    12  	"os"
    13  
    14  	"github.com/drone/runner-go/pipeline"
    15  )
    16  
    17  var _ pipeline.Streamer = (*Console)(nil)
    18  
    19  // Console implements a pipeline streamer that writes the
    20  // pipeline logs to the console using os.Stdout.
    21  type Console struct {
    22  	seq *sequence
    23  	col *sequence
    24  	tty bool
    25  }
    26  
    27  // New returns a new console recorder.
    28  func New(tty bool) *Console {
    29  	return &Console{
    30  		tty: tty,
    31  		seq: new(sequence),
    32  		col: new(sequence),
    33  	}
    34  }
    35  
    36  // Stream returns an io.WriteCloser that prints formatted log
    37  // lines to the console with step name, line number, and optional
    38  // coloring.
    39  func (s *Console) Stream(_ context.Context, _ *pipeline.State, name string) io.WriteCloser {
    40  	if s.tty {
    41  		return &pretty{
    42  			base:  os.Stdout,
    43  			color: colors[s.col.next()%len(colors)],
    44  			name:  name,
    45  			seq:   s.seq,
    46  		}
    47  	}
    48  	return &plain{
    49  		base: os.Stdout,
    50  		name: name,
    51  		seq:  s.seq,
    52  	}
    53  }