cmd/file.go   A
last analyzed

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A main.NewFileHandler 0 9 3
A main.*FileHandler.WriteAll 0 11 3
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