| 134 | } |
| 135 | |
| 136 | func NewFileWriter(cfg baker.OutputParams) (baker.Output, error) { |
| 137 | log.WithFields(log.Fields{"fn": "NewFileWriter", "idx": cfg.Index}).Info("Initializing") |
| 138 | |
| 139 | dcfg := cfg.DecodedConfig.(*FileWriterConfig) |
| 140 | dcfg.fillDefaults() |
| 141 | |
| 142 | fw := &FileWriter{ |
| 143 | Cfg: dcfg, |
| 144 | Fields: cfg.Fields, |
| 145 | workers: make(map[string]*fileWorker), |
| 146 | index: cfg.Index, |
| 147 | useReplField: strings.Contains(dcfg.PathString, "{{.Field0}}"), |
| 148 | } |
| 149 | |
| 150 | if fw.useReplField && len(cfg.Fields) == 0 { |
| 151 | return nil, errors.New("if {{.Field0}} is given, at least one field must be given in [output.fields]") |
| 152 | } |
| 153 | |
| 154 | // Compile the template. Trying to check the validity of the path without |
| 155 | // creating it is futile as this is very much os-dependent, plus, if |
| 156 | // PathString contains {{.Field0}}, then the generated path can very much |
| 157 | // contains anything, but we'd only know this at runtime. So it's reasonable |
| 158 | // to handle such errors in fileWorker.makePath. |
| 159 | var err error |
| 160 | if fw.tmpl, err = template.New("fileWorkerType").Parse(dcfg.PathString); err != nil { |
| 161 | return nil, fmt.Errorf("FileWorker: invalid PathString template: %s", err) |
| 162 | } |
| 163 | |
| 164 | return fw, nil |
| 165 | } |
| 166 | |
| 167 | func (w *FileWriter) Run(input <-chan baker.OutputRecord, upch chan<- string) error { |
| 168 | ctxlog := log.WithFields(log.Fields{"idx": w.index}) |