main.*FileHandler.WriteAll   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 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