| Total Lines | 37 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | package main |
||
| 2 | |||
| 3 | import ( |
||
| 4 | "fmt" |
||
| 5 | "os" |
||
| 6 | "strings" |
||
| 7 | ) |
||
| 8 | |||
| 9 | // FileHandler handles list file. |
||
| 10 | type FileHandler struct { |
||
| 11 | file string |
||
| 12 | } |
||
| 13 | |||
| 14 | // NewFileHandler returns initialized *FileHandler |
||
| 15 | func NewFileHandler(file string) (*FileHandler, error) { |
||
| 16 | info, err := os.Stat(file) |
||
| 17 | if err == nil && info.IsDir() { |
||
| 18 | return nil, fmt.Errorf("'%s' is dir, please set file path", file) |
||
| 19 | } |
||
| 20 | |||
| 21 | return &FileHandler{ |
||
| 22 | file: file, |
||
| 23 | }, nil |
||
| 24 | } |
||
| 25 | |||
| 26 | // WriteAll writes lines into file |
||
| 27 | func (f *FileHandler) WriteAll(lines []string) error { |
||
| 28 | fp, err := os.Create(f.file) |
||
| 29 | if err != nil { |
||
| 30 | return err |
||
| 31 | } |
||
| 32 | defer fp.Close() |
||
| 33 | |||
| 34 | if _, err := fp.WriteString(strings.Join(lines, "\n")); err != nil { |
||
| 35 | return err |
||
| 36 | } |
||
| 37 | return fp.Sync() |
||
| 38 | } |
||
| 39 |