Skip to content

Commit 9bc8703

Browse files
committed
宽松检查, 便于灵活构建
1 parent a90dab5 commit 9bc8703

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

pkg/wabook/book.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,32 @@ type Book struct {
1616
}
1717

1818
func LoadBook(path string) (book *Book, err error) {
19+
bookIniPath := filepath.Join(path, "book.ini")
20+
bookTomlPath := filepath.Join(path, "book.toml")
21+
1922
book = &Book{Root: path}
20-
book.Info, err = LoadConfig(filepath.Join(path, "book.ini"))
23+
book.Info, err = LoadConfig(bookIniPath)
2124
if err != nil {
22-
if info, errx := LoadConfig(filepath.Join(path, "book.toml")); errx != nil {
23-
return nil, err
25+
if info, errx := LoadConfig(bookTomlPath); errx != nil {
26+
if fileExists(bookIniPath) || fileExists(bookTomlPath) {
27+
// 文件存在说明有错误
28+
return nil, err
29+
} else {
30+
// 不存在则生成一个空的
31+
book.Info = &BookToml{}
32+
}
2433
} else {
2534
book.Info = info
2635
}
2736
}
2837
book.Summary, err = LoadSummary(filepath.Join(path, "SUMMARY.md"))
2938
if err != nil {
30-
return nil, err
39+
if fileExists(filepath.Join(path, "SUMMARY.md")) {
40+
return nil, err
41+
} else {
42+
err = nil
43+
}
44+
book.Summary = &Summary{}
3145
}
3246
book.Talks = loadTalks(book.Root)
3347
return

pkg/wabook/utils.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2024 <chaishushan{AT}gmail.com>. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package wabook
6+
7+
import "os"
8+
9+
func fileExists(path string) bool {
10+
fi, err := os.Lstat(path)
11+
if err != nil {
12+
return false
13+
}
14+
if fi.IsDir() {
15+
return false
16+
}
17+
return true
18+
}

0 commit comments

Comments
 (0)