mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Refactor pkg/stremformatter with custom constructors instead of passing a boolean
Signed-off-by: Antonio Murdaca <me@runcom.ninja>
This commit is contained in:
		
							parent
							
								
									37cb2a15bf
								
							
						
					
					
						commit
						6b700bdaca
					
				
					 8 changed files with 50 additions and 25 deletions
				
			
		| 
						 | 
				
			
			@ -199,7 +199,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
 | 
			
		|||
	// Setup an upload progress bar
 | 
			
		||||
	// FIXME: ProgressReader shouldn't be this annoying to use
 | 
			
		||||
	if context != nil {
 | 
			
		||||
		sf := streamformatter.NewStreamFormatter(false)
 | 
			
		||||
		sf := streamformatter.NewStreamFormatter()
 | 
			
		||||
		body = progressreader.New(progressreader.Config{
 | 
			
		||||
			In:        context,
 | 
			
		||||
			Out:       cli.out,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -730,7 +730,7 @@ func (s *Server) postImagesCreate(version version.Version, w http.ResponseWriter
 | 
			
		|||
		if !output.Flushed() {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		sf := streamformatter.NewStreamFormatter(true)
 | 
			
		||||
		sf := streamformatter.NewJSONStreamFormatter()
 | 
			
		||||
		output.Write(sf.FormatError(err))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -814,7 +814,7 @@ func (s *Server) postImagesPush(version version.Version, w http.ResponseWriter,
 | 
			
		|||
		if !output.Flushed() {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		sf := streamformatter.NewStreamFormatter(true)
 | 
			
		||||
		sf := streamformatter.NewJSONStreamFormatter()
 | 
			
		||||
		output.Write(sf.FormatError(err))
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
| 
						 | 
				
			
			@ -843,7 +843,7 @@ func (s *Server) getImagesGet(version version.Version, w http.ResponseWriter, r
 | 
			
		|||
		if !output.Flushed() {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		sf := streamformatter.NewStreamFormatter(true)
 | 
			
		||||
		sf := streamformatter.NewJSONStreamFormatter()
 | 
			
		||||
		output.Write(sf.FormatError(err))
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
| 
						 | 
				
			
			@ -1234,7 +1234,7 @@ func (s *Server) postBuild(version version.Version, w http.ResponseWriter, r *ht
 | 
			
		|||
		if !output.Flushed() {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		sf := streamformatter.NewStreamFormatter(true)
 | 
			
		||||
		sf := streamformatter.NewJSONStreamFormatter()
 | 
			
		||||
		w.Write(sf.FormatError(err))
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -141,7 +141,7 @@ func Build(d *daemon.Daemon, buildConfig *Config) error {
 | 
			
		|||
	}
 | 
			
		||||
	defer context.Close()
 | 
			
		||||
 | 
			
		||||
	sf := streamformatter.NewStreamFormatter(true)
 | 
			
		||||
	sf := streamformatter.NewJSONStreamFormatter()
 | 
			
		||||
 | 
			
		||||
	builder := &Builder{
 | 
			
		||||
		Daemon: d,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,7 +22,7 @@ type ImageImportConfig struct {
 | 
			
		|||
 | 
			
		||||
func (s *TagStore) Import(src string, repo string, tag string, imageImportConfig *ImageImportConfig) error {
 | 
			
		||||
	var (
 | 
			
		||||
		sf      = streamformatter.NewStreamFormatter(true)
 | 
			
		||||
		sf      = streamformatter.NewJSONStreamFormatter()
 | 
			
		||||
		archive archive.ArchiveReader
 | 
			
		||||
		resp    *http.Response
 | 
			
		||||
	)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,7 +29,7 @@ type ImagePullConfig struct {
 | 
			
		|||
 | 
			
		||||
func (s *TagStore) Pull(image string, tag string, imagePullConfig *ImagePullConfig) error {
 | 
			
		||||
	var (
 | 
			
		||||
		sf = streamformatter.NewStreamFormatter(true)
 | 
			
		||||
		sf = streamformatter.NewJSONStreamFormatter()
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	// Resolve the Repository name from fqn to RepositoryInfo
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -495,7 +495,7 @@ func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint *
 | 
			
		|||
// FIXME: Allow to interrupt current push when new push of same image is done.
 | 
			
		||||
func (s *TagStore) Push(localName string, imagePushConfig *ImagePushConfig) error {
 | 
			
		||||
	var (
 | 
			
		||||
		sf = streamformatter.NewStreamFormatter(true)
 | 
			
		||||
		sf = streamformatter.NewJSONStreamFormatter()
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	// Resolve the Repository name from fqn to RepositoryInfo
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,8 +12,14 @@ type StreamFormatter struct {
 | 
			
		|||
	json bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewStreamFormatter(json bool) *StreamFormatter {
 | 
			
		||||
	return &StreamFormatter{json}
 | 
			
		||||
// NewStreamFormatter returns a simple StreamFormatter
 | 
			
		||||
func NewStreamFormatter() *StreamFormatter {
 | 
			
		||||
	return &StreamFormatter{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewJSONStreamFormatter returns a StreamFormatter configured to stream json
 | 
			
		||||
func NewJSONStreamFormatter() *StreamFormatter {
 | 
			
		||||
	return &StreamFormatter{true}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const streamNewline = "\r\n"
 | 
			
		||||
| 
						 | 
				
			
			@ -62,7 +68,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessa
 | 
			
		|||
		progress = &jsonmessage.JSONProgress{}
 | 
			
		||||
	}
 | 
			
		||||
	if sf.json {
 | 
			
		||||
 | 
			
		||||
		b, err := json.Marshal(&jsonmessage.JSONMessage{
 | 
			
		||||
			Status:          action,
 | 
			
		||||
			ProgressMessage: progress.String(),
 | 
			
		||||
| 
						 | 
				
			
			@ -81,10 +86,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *jsonmessa
 | 
			
		|||
	return []byte(action + " " + progress.String() + endl)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (sf *StreamFormatter) Json() bool {
 | 
			
		||||
	return sf.json
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type StdoutFormater struct {
 | 
			
		||||
	io.Writer
 | 
			
		||||
	*StreamFormatter
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,31 +10,55 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
func TestFormatStream(t *testing.T) {
 | 
			
		||||
	sf := NewStreamFormatter(true)
 | 
			
		||||
	sf := NewStreamFormatter()
 | 
			
		||||
	res := sf.FormatStream("stream")
 | 
			
		||||
	if string(res) != "stream"+"\r" {
 | 
			
		||||
		t.Fatalf("%q", res)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatJSONStatus(t *testing.T) {
 | 
			
		||||
	sf := NewStreamFormatter()
 | 
			
		||||
	res := sf.FormatStatus("ID", "%s%d", "a", 1)
 | 
			
		||||
	if string(res) != "a1\r\n" {
 | 
			
		||||
		t.Fatalf("%q", res)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatSimpleError(t *testing.T) {
 | 
			
		||||
	sf := NewStreamFormatter()
 | 
			
		||||
	res := sf.FormatError(errors.New("Error for formatter"))
 | 
			
		||||
	if string(res) != "Error: Error for formatter\r\n" {
 | 
			
		||||
		t.Fatalf("%q", res)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestJSONFormatStream(t *testing.T) {
 | 
			
		||||
	sf := NewJSONStreamFormatter()
 | 
			
		||||
	res := sf.FormatStream("stream")
 | 
			
		||||
	if string(res) != `{"stream":"stream"}`+"\r\n" {
 | 
			
		||||
		t.Fatalf("%q", res)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatStatus(t *testing.T) {
 | 
			
		||||
	sf := NewStreamFormatter(true)
 | 
			
		||||
func TestJSONFormatStatus(t *testing.T) {
 | 
			
		||||
	sf := NewJSONStreamFormatter()
 | 
			
		||||
	res := sf.FormatStatus("ID", "%s%d", "a", 1)
 | 
			
		||||
	if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" {
 | 
			
		||||
		t.Fatalf("%q", res)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatSimpleError(t *testing.T) {
 | 
			
		||||
	sf := NewStreamFormatter(true)
 | 
			
		||||
func TestJSONFormatSimpleError(t *testing.T) {
 | 
			
		||||
	sf := NewJSONStreamFormatter()
 | 
			
		||||
	res := sf.FormatError(errors.New("Error for formatter"))
 | 
			
		||||
	if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" {
 | 
			
		||||
		t.Fatalf("%q", res)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatJSONError(t *testing.T) {
 | 
			
		||||
	sf := NewStreamFormatter(true)
 | 
			
		||||
func TestJSONFormatJSONError(t *testing.T) {
 | 
			
		||||
	sf := NewJSONStreamFormatter()
 | 
			
		||||
	err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
 | 
			
		||||
	res := sf.FormatError(err)
 | 
			
		||||
	if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" {
 | 
			
		||||
| 
						 | 
				
			
			@ -42,8 +66,8 @@ func TestFormatJSONError(t *testing.T) {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFormatProgress(t *testing.T) {
 | 
			
		||||
	sf := NewStreamFormatter(true)
 | 
			
		||||
func TestJSONFormatProgress(t *testing.T) {
 | 
			
		||||
	sf := NewJSONStreamFormatter()
 | 
			
		||||
	progress := &jsonmessage.JSONProgress{
 | 
			
		||||
		Current: 15,
 | 
			
		||||
		Total:   30,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue