From 355bc2aa3b66c8d3afa29fbbf13f31e184dd5724 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Tue, 12 May 2026 17:42:23 +0200 Subject: kdev: add --logLevel flag to configure slog output level Co-Authored-By: Claude Sonnet 4.6 --- cmd/kdev/main.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'cmd/kdev/main.go') diff --git a/cmd/kdev/main.go b/cmd/kdev/main.go index c884896..dc404e0 100644 --- a/cmd/kdev/main.go +++ b/cmd/kdev/main.go @@ -30,16 +30,16 @@ type Record struct { } var ( - excludes map[string]bool = map[string]bool{} - repoPath string - after time.Time - before time.Time - sortBy string - maxRecords int - algo string - cpuProfile string - keywords []string - binExtensions = map[string]bool{} + excludes map[string]bool = map[string]bool{} + repoPath string + after time.Time + before time.Time + sortBy string + maxRecords int + algo string + cpuProfile string + keywords []string + logLevel string ) func initConfig() { @@ -55,6 +55,7 @@ func initConfig() { 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.String("logLevel", "info", "Log level (debug, info, warn, error)") pflag.Parse() viper.SetConfigType("yaml") @@ -91,6 +92,10 @@ func initConfig() { for _, exclude := range viper.GetStringSlice("excludes") { excludes[exclude] = true } + logLevel = viper.GetString("logLevel") + level := slog.LevelInfo + kcore.Expect(level.UnmarshalText([]byte(logLevel)), "Invalid log level") + slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level}))) } func main() { @@ -128,13 +133,7 @@ func main() { } } -func isBinaryFile(absolutePath, relativePath string) bool { - // First check by file extension (fast) - ext := strings.ToLower(filepath.Ext(relativePath)) - if _, ok := binExtensions[ext]; ok { - return true - } - +func isBinaryFile(absolutePath string) bool { file, err := os.Open(absolutePath) // #nosec G304 Repo walking kcore.Expect(err, "failed to open file") defer file.Close() @@ -177,7 +176,7 @@ func walkRepo(keywords []string, factory RecordExtractorFactory, records *[]Reco if d.IsDir() { return nil } - if isBinaryFile(path, relativePath) { + if isBinaryFile(path) { return nil } kcore.Expect(progress.Add(1), "Error incrementing progress") -- cgit v1.3