-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
80 lines (65 loc) · 1.56 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"flag"
"log"
"os"
"path/filepath"
"strings"
)
func IsDir(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir(), nil
}
func ProcessFile(file string , OutFile string, id string) error {
log.Println("Processing file: ", file)
data, err := os.ReadFile(file)
if err != nil {
return err
}
if strings.TrimSpace(id) != "" {
if OutFile == "." {
return Convert(file, string(data), id)
}
return Convert(OutFile, string(data), id)
}
if OutFile == "." {
return ConvertWithoutId(file, string(data))
}
return ConvertWithoutId(OutFile, string(data))
}
func main() {
FilePtr := flag.String("file", ".", "filename of ris file or directory path to ris file(s).")
OutFilePtr := flag.String("out", ".", "new filename of bib file")
IdPtr := flag.String("id", " ", "BibTeX article id")
flag.Parse()
files := []string{}
file := *FilePtr
OutFile := *OutFilePtr
id := *IdPtr
var err error
isDir, err := IsDir(file)
if err != nil {
log.Fatal(err)
}
if isDir || file == "." {
files, err = filepath.Glob(filepath.Join(file, "*.ris"))
if err != nil {
log.Fatal(err)
}
} else {
files = append(files, file)
}
if len(files) == 0 {
log.Println("No .ris files found")
return
}
log.Printf("Found %d .ris files in %s\n", len(files), file)
for _, f := range files {
if err := ProcessFile(f, OutFile, id); err != nil {
log.Printf("Failed to process file %s: %v", f, err)
}
}
}