-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.go
More file actions
62 lines (50 loc) · 1.17 KB
/
Copy pathpaths.go
File metadata and controls
62 lines (50 loc) · 1.17 KB
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
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
// removes the given root and all ancestors from a path
func extractSubpath(path, root string) (subpath string) {
root = filepath.Base(root)
root += "/"
_, subpath, _ = strings.Cut(path, root)
return
}
func mdToHtml(path string) string {
return strings.Replace(path, ".md", ".html", 1)
}
// checks that a path exists and is a directory
//
// returns nil if the checks pass, else an error
func validDir(path string) error {
path, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("failed to determine absolute path for file %q: %w", path, err)
}
fileInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("failed to get file info for %q: %w", path, err.(*fs.PathError).Err)
}
if !fileInfo.IsDir() {
return fmt.Errorf("path is not a directory: %q", path)
}
return nil
}
func validateDirs() error {
if err := validDir(contentDir); err != nil {
return err
}
if err := validDir(outputDir); err != nil {
err = os.MkdirAll(outputDir, fs.ModePerm)
if err != nil {
return err
}
}
if err := validDir(assetsDir); err != nil && !skipAssets {
return err
}
return nil
}