Get an open file for the log, or open a new one
(baseDir string, log stern.Log, openFilesMap map[string]*os.File)
| 172 | |
| 173 | // Get an open file for the log, or open a new one |
| 174 | func getLogFile(baseDir string, log stern.Log, openFilesMap map[string]*os.File) (*os.File, |
| 175 | error, |
| 176 | ) { |
| 177 | filePath := path.Join(baseDir, log.Namespace, log.PodName, log.ContainerName+".log") |
| 178 | dirFile := path.Dir(filePath) |
| 179 | |
| 180 | file, ok := openFilesMap[filePath] |
| 181 | if ok { |
| 182 | return file, nil |
| 183 | } |
| 184 | |
| 185 | // If we don't have the file already opened, we open it |
| 186 | err := os.MkdirAll(dirFile, 0o700) |
| 187 | if err != nil { |
| 188 | return nil, fmt.Errorf("cannot ensure directory existence (%v): %w", dirFile, err) |
| 189 | } |
| 190 | file, err = os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o600) //nolint:gosec |
| 191 | if err != nil { |
| 192 | return nil, fmt.Errorf("cannot open file %v: %w", filePath, err) |
| 193 | } |
| 194 | |
| 195 | openFilesMap[filePath] = file |
| 196 | return file, nil |
| 197 | } |