nurse/magefiles/common.go

78 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-09-27 20:19:27 +00:00
package main
import (
"io/fs"
"log/slog"
2022-09-27 20:19:27 +00:00
"os"
"path/filepath"
"slices"
2022-09-27 20:19:27 +00:00
"strings"
)
const defaultDirPermissions = 0o755
var (
GoSourceFiles []string
GeneratedMockFiles []string
WorkingDir string
OutDir string
dirsToIgnore = []string{
".git",
"magefiles",
".concourse",
".run",
".task",
}
)
func init() {
if wd, err := os.Getwd(); err != nil {
panic(err)
} else {
WorkingDir = wd
}
OutDir = filepath.Join(WorkingDir, "out")
if err := os.MkdirAll(OutDir, defaultDirPermissions); err != nil {
panic(err)
}
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))
2022-09-27 20:19:27 +00:00
if err := initSourceFiles(); err != nil {
panic(err)
}
slog.Info("Completed initialization")
2022-09-27 20:19:27 +00:00
}
func initSourceFiles() error {
return filepath.WalkDir(WorkingDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
if slices.Contains(dirsToIgnore, filepath.Base(path)) {
return fs.SkipDir
}
return nil
}
_, ext, found := strings.Cut(filepath.Base(path), ".")
if !found {
return nil
}
switch ext {
case "mock.go":
GeneratedMockFiles = append(GeneratedMockFiles, path)
case "go":
GoSourceFiles = append(GoSourceFiles, path)
}
return nil
})
}