From 923b7ef45595e1f8e8f1f136d735e99a336e365d Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Mon, 10 Mar 2025 13:50:10 +0100 Subject: feat(kdev): Uniform config precedence --- cmd/kdev/main.go | 93 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 40 deletions(-) (limited to 'cmd/kdev') diff --git a/cmd/kdev/main.go b/cmd/kdev/main.go index 44d5c5c..190b86c 100644 --- a/cmd/kdev/main.go +++ b/cmd/kdev/main.go @@ -1,7 +1,6 @@ package main import ( - "flag" "fmt" "io/fs" "os" @@ -17,6 +16,7 @@ import ( "github.com/martinlehoux/kagamigo/kcore" "github.com/samber/lo" "github.com/schollz/progressbar/v3" + "github.com/spf13/pflag" "github.com/spf13/viper" "golang.org/x/exp/slog" ) @@ -28,45 +28,62 @@ type Record struct { date time.Time } -var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") -var sortBy = flag.String("sort", "date", "Sort by date or random") -var maxRecords = flag.Int("max", 25, "Maximum number of records to display") -var repoPath = flag.String("repo", ".", "Path to the repository") -var afterS = flag.String("after", "2022-09-01", "Only show records after this date") -var algo = flag.String("algo", "git", "Record extraction algorithm (git, go-git, stat)") +var excludes map[string]bool = map[string]bool{} +var repoPath string +var after time.Time +var sortBy string +var maxRecords int +var algo string +var cpuProfile string +var keywords []string -var excludes map[string]bool = map[string]bool{ - ".git": true, - ".kdev.yaml": true, +func initConfig() { + var err error + pflag.StringVar(&repoPath, "repo", ".", "Path to the repository") + if !strings.HasSuffix(repoPath, "/") { + repoPath = repoPath + "/" + } + pflag.String("sort", "date", "Sort by date or random") + pflag.Int("max", 25, "Maximum number of records to display") + pflag.String("after", "2022-09-01", "Only show records after this date") + pflag.String("algo", "git", "Record extraction algorithm (git, go-git, stat)") + pflag.String("cpuProfile", "", "write cpu profile to file") + pflag.StringSlice("excludes", []string{".git", ".kdev.yaml"}, "Excluded directories") + pflag.Parse() + + viper.SetConfigType("yaml") + viper.SetConfigName(".kdev") + viper.AddConfigPath(".") + viper.AddConfigPath(repoPath) + kcore.Expect(viper.BindPFlags(pflag.CommandLine), "Error binding flags") + kcore.Expect(viper.ReadInConfig(), "Error reading config file") + + after, err = time.Parse(time.DateOnly, viper.GetString("after")) + kcore.Expect(err, "Error parsing date") + sortBy = viper.GetString("sort") + maxRecords = viper.GetInt("max") + algo = viper.GetString("algo") + cpuProfile = viper.GetString("cpuProfile") + keywords = viper.GetStringSlice("keywords") + for _, exclude := range viper.GetStringSlice("excludes") { + excludes[exclude] = true + } } func main() { var err error - flag.Parse() - after, err := time.Parse(time.DateOnly, *afterS) - kcore.Expect(err, "Error parsing date") - if !strings.HasSuffix(*repoPath, "/") { - *repoPath = *repoPath + "/" - } + initConfig() records := []Record{} - if *cpuprofile != "" { - f, err := os.Create(*cpuprofile) + if cpuProfile != "" { + f, err := os.Create(cpuProfile) // #nosec G304 CLI arg kcore.Expect(err, "Error creating CPU profile") kcore.Expect(pprof.StartCPUProfile(f), "Error starting CPU profile") defer pprof.StopCPUProfile() } - 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") - for _, exclude := range viper.GetStringSlice("excludes") { - excludes[exclude] = true - } - keywords := viper.GetStringSlice("keywords") - repo, err := git.PlainOpen(path.Join(*repoPath, ".git")) + slog.Info("Scanning repository", "repo", repoPath, "sort", sortBy, "max", maxRecords, "after", after, "algo", algo) + + repo, err := git.PlainOpen(path.Join(repoPath, ".git")) kcore.Expect(err, "Error opening repository") ref, err := repo.Head() kcore.Expect(err, "Error getting HEAD") @@ -75,21 +92,21 @@ func main() { kcore.Expect(walkRepo(keywords, head, &records), "Error walking directory") records = lo.Filter(records, func(record Record, index int) bool { return record.date.After(after) }) - if *sortBy == "random" { + if sortBy == "random" { records = lo.Shuffle(records) } else { slices.SortFunc(records, func(a, b Record) int { return -int(a.date.Sub(b.date).Nanoseconds()) }) } fmt.Println("") - for _, record := range records[:min(*maxRecords, len(records))] { + 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) } } func walkRepo(keywords []string, head *object.Commit, records *[]Record) error { progress := progressbar.Default(-1, "Scanning") - return filepath.WalkDir(*repoPath, func(path string, d fs.DirEntry, err error) error { - relativePath := strings.TrimPrefix(path, *repoPath) + return filepath.WalkDir(repoPath, func(path string, d fs.DirEntry, err error) error { + relativePath := strings.TrimPrefix(path, repoPath) progress.Describe(relativePath) if err != nil { return err @@ -106,7 +123,7 @@ func walkRepo(keywords []string, head *object.Commit, records *[]Record) error { } if !d.IsDir() { kcore.Expect(progress.Add(1), "Error incrementing progress") - return processFile(*repoPath, relativePath, keywords, records, head) + return processFile(repoPath, relativePath, keywords, records, head) } return nil }) @@ -138,19 +155,15 @@ func processFile(repoPath string, relativePath string, keywords []string, record } var recordExtractor RecordExtractor - switch *algo { + switch algo { case "git": recordExtractor = &GitRecordExtractor{repoPath: repoPath} case "go-git": recordExtractor = NewGoGitRecordExtractor(head, relativePath) case "stat": recordExtractor = NewStatRecordExtractor(absolutePath) - default: - kcore.Assert(false, "wrong algo value (git, go-git, stat)") - } - if recordExtractor == nil { - return nil } + kcore.Assert(recordExtractor != nil, "wrong algo value (git, go-git, stat)") for _, line := range matchingLines { record, err := recordExtractor.Extract(line.keyword, relativePath, line.line) if err != nil { -- cgit v1.3