github.com/yandex/pandora@v0.5.32/core/datasink/file.go (about)

     1  package datasink
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  
     7  	"github.com/spf13/afero"
     8  	"github.com/yandex/pandora/core"
     9  )
    10  
    11  // TODO(skipor): gzip on flag
    12  
    13  type FileConfig struct {
    14  	Path string `config:"path" validate:"required"`
    15  }
    16  
    17  func NewFile(fs afero.Fs, conf FileConfig) core.DataSink {
    18  	return &fileSink{afero.Afero{Fs: fs}, conf}
    19  }
    20  
    21  type fileSink struct {
    22  	fs   afero.Afero
    23  	conf FileConfig
    24  }
    25  
    26  func (s *fileSink) OpenSink() (wc io.WriteCloser, err error) {
    27  	return s.fs.OpenFile(s.conf.Path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
    28  }
    29  
    30  func NewStdout() core.DataSink {
    31  	return hideCloseFileSink{os.Stdout}
    32  }
    33  
    34  func NewStderr() core.DataSink {
    35  	return hideCloseFileSink{os.Stderr}
    36  }
    37  
    38  type hideCloseFileSink struct{ afero.File }
    39  
    40  func (f hideCloseFileSink) OpenSink() (wc io.WriteCloser, err error) {
    41  	return f, nil
    42  }
    43  
    44  func (f hideCloseFileSink) Close() error { return nil }