-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfile.go
66 lines (55 loc) · 1.14 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/straightdave/lesphina/v2"
)
// SourceFile ...
type SourceFile struct {
Src string
BaseName string
Ext string
Les *lesphina.Lesphina
}
func mustReadSourceFiles(src string) []*SourceFile {
res, err := readSourceFiles(src)
if err != nil {
log.Fatal(err)
}
return res
}
func readSourceFiles(src string) ([]*SourceFile, error) {
if strings.TrimSpace(src) == "" {
return nil, fmt.Errorf("src shouldn't be blank")
}
var files []*SourceFile
paths := strings.Split(src, ",")
for _, path := range paths {
path = strings.TrimSpace(path)
if path == "" {
continue
}
_, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("file %s not exist", path)
}
les, err := lesphina.Read(path)
if err != nil {
return nil, fmt.Errorf("cannot parse using lesphina: %v", err)
}
f := &SourceFile{
Src: path,
BaseName: filepath.Base(path),
Ext: filepath.Ext(path),
Les: les,
}
files = append(files, f)
}
if len(files) == 0 {
return nil, fmt.Errorf("no valid file parsed")
}
return files, nil
}