github.com/robryk/drone@v0.2.1-0.20140602202253-40fe4305815d/pkg/build/writer.go (about)

     1  package build
     2  
     3  import (
     4  	//"bytes"
     5  	"fmt"
     6  	"io"
     7  
     8  	"strings"
     9  )
    10  
    11  var (
    12  	// the prefix used to determine if this is
    13  	// data that should be stripped from the output
    14  	prefix = []byte("#DRONE:")
    15  )
    16  
    17  // custom writer to intercept the build
    18  // output
    19  type writer struct {
    20  	io.Writer
    21  }
    22  
    23  // Write appends the contents of p to the buffer. It will
    24  // scan for DRONE special formatting codes embedded in the
    25  // output, and will alter the output accordingly.
    26  func (w *writer) Write(p []byte) (n int, err error) {
    27  
    28  	lines := strings.Split(string(p), "\n")
    29  	for i, line := range lines {
    30  
    31  		if strings.HasPrefix(line, "#DRONE:") {
    32  			var cmd string
    33  
    34  			// extract the command (base16 encoded)
    35  			// from the output
    36  			fmt.Sscanf(line[7:], "%x", &cmd)
    37  
    38  			// echo the decoded command
    39  			cmd = fmt.Sprintf("$ %s", cmd)
    40  			w.Writer.Write([]byte(cmd))
    41  
    42  		} else {
    43  			w.Writer.Write([]byte(line))
    44  		}
    45  
    46  		if i < len(lines)-1 {
    47  			w.Writer.Write([]byte("\n"))
    48  		}
    49  	}
    50  
    51  	return len(p), nil
    52  }
    53  
    54  // WriteString appends the contents of s to the buffer.
    55  func (w *writer) WriteString(s string) (n int, err error) {
    56  	return w.Write([]byte(s))
    57  }