From c1ae71251a2b23170a5d42c6acd37cad8675c5f0 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Tue, 15 Oct 2024 10:04:05 +0200 Subject: feat(kdev): Config files --- cmd/kdev/main.go | 31 +++++++++++++++++++++++-------- cmd/kdev/record_extractor.go | 8 ++++++-- 2 files changed, 29 insertions(+), 10 deletions(-) (limited to 'cmd/kdev') diff --git a/cmd/kdev/main.go b/cmd/kdev/main.go index fc316e6..8f7e4a9 100644 --- a/cmd/kdev/main.go +++ b/cmd/kdev/main.go @@ -17,6 +17,7 @@ import ( "github.com/martinlehoux/kagamigo/kcore" "github.com/samber/lo" "github.com/schollz/progressbar/v3" + "github.com/spf13/viper" "golang.org/x/exp/slog" ) @@ -39,8 +40,10 @@ func main() { flag.Parse() after, err := time.Parse(time.DateOnly, *afterS) kcore.Expect(err, "Error parsing date") - excludes := []string{".venv", ".git", ".ruff_cache", "db_dumps", ".mypy_cache", "uploads", "__pycache__", ".coverage"} - keywords := []string{"# TODO"} + if !strings.HasSuffix(*repoPath, "/") { + *repoPath = *repoPath + "/" + } + records := []Record{} if *cpuprofile != "" { f, err := os.Create(*cpuprofile) @@ -48,9 +51,15 @@ func main() { kcore.Expect(pprof.StartCPUProfile(f), "Error starting CPU profile") defer pprof.StopCPUProfile() } - slog.Info("Scanning repository", "repo", *repoPath, "sort", *sortBy, "max", *maxRecords) + slog.Info("Scanning repository", "repo", *repoPath, "sort", *sortBy, "max", *maxRecords, "after", *afterS, "algo", *algo) + viper.SetConfigType("yaml") + configFile, err := os.Open(path.Join(*repoPath, ".kdev.yaml")) + kcore.Expect(err, "Error opening config file") + defer configFile.Close() + kcore.Expect(viper.ReadConfig(configFile), "Error reading config file") + excludes := viper.GetStringSlice("excludes") + keywords := viper.GetStringSlice("keywords") repo, err := git.PlainOpen(path.Join(*repoPath, ".git")) - // repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: path.Join(*repoPath, ".git")}) kcore.Expect(err, "Error opening repository") ref, err := repo.Head() kcore.Expect(err, "Error getting HEAD") @@ -58,12 +67,17 @@ func main() { kcore.Expect(err, "Error getting commit object") progress := progressbar.Default(-1, "Scanning") err = filepath.WalkDir(*repoPath, func(path string, d fs.DirEntry, err error) error { - relativePath := strings.TrimPrefix(path, *repoPath+"/") + relativePath := strings.TrimPrefix(path, *repoPath) + progress.Describe(relativePath) if err != nil { return err } if slices.Contains(excludes, d.Name()) { - return filepath.SkipDir + if d.IsDir() { + return filepath.SkipDir + } else { + return nil + } } if d.Type()&fs.ModeSymlink != 0 { return nil @@ -75,6 +89,7 @@ func main() { return nil }) kcore.Expect(err, "Error walking directory") + records = lo.Filter(records, func(record Record, index int) bool { return record.date.After(after) }) if *sortBy == "random" { records = lo.Shuffle(records) @@ -82,7 +97,7 @@ func main() { slices.SortFunc(records, func(a, b Record) int { return -int(a.date.Sub(b.date).Nanoseconds()) }) } fmt.Println("") - for _, record := range records[:*maxRecords] { + for _, record := range records[:min(*maxRecords, len(records))] { fmt.Printf("%s\t %s:%d\n", record.date.Format("2006-01-02"), record.path, record.line) } } @@ -96,7 +111,7 @@ func processFile(repoPath string, relativePath string, keywords []string, record absolutePath := path.Join(repoPath, relativePath) content, err := os.ReadFile(absolutePath) // #nosec G304 if err != nil { - return err + return kcore.Wrap(err, "Error reading file") } lines := strings.Split(string(content), "\n") diff --git a/cmd/kdev/record_extractor.go b/cmd/kdev/record_extractor.go index 2dc079e..5781077 100644 --- a/cmd/kdev/record_extractor.go +++ b/cmd/kdev/record_extractor.go @@ -55,12 +55,16 @@ func NewGoGitRecordExtractor(head *object.Commit, path string) *GoGitRecordExtra } func (re *GoGitRecordExtractor) Extract(keyword string, path string, line int) Record { - return Record{ + record := Record{ keyword: keyword, path: path, line: line, - date: re.blame.Lines[line-1].Date, } + if re != nil { + record.date = re.blame.Lines[line-1].Date + } + + return record } type GitRecordExtractor struct { -- cgit v1.3