From 3d2a42ddc20c086a4c14db92f91d64d174652c8a Mon Sep 17 00:00:00 2001 From: Arik Jones Date: Thu, 5 Sep 2024 23:05:51 -0500 Subject: [PATCH] feat: Add support for configuration file --- cmd/root.go | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 6557f85..a700ddb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,12 +8,15 @@ import ( "time" "github.com/spf13/cobra" + "github.com/tnypxl/rollup/internal/config" ) var ( path string fileTypes string codeGenPatterns string + configFile string + cfg *config.Config ) var rootCmd = &cobra.Command{ @@ -23,7 +26,21 @@ var rootCmd = &cobra.Command{ in a given project, current path or a custom path, to a single timestamped markdown file whose name is -rollup-.md.`, RunE: func(cmd *cobra.Command, args []string) error { - return runRollup(path, fileTypes, codeGenPatterns) + if configFile == "" { + defaultConfig := config.DefaultConfigPath() + if config.FileExists(defaultConfig) { + configFile = defaultConfig + } + } + + if configFile != "" { + var err error + cfg, err = config.Load(configFile) + if err != nil { + return fmt.Errorf("error loading config file: %v", err) + } + } + return runRollup() }, } @@ -34,7 +51,8 @@ func Execute() error { func init() { rootCmd.Flags().StringVarP(&path, "path", "p", ".", "Path to the project directory") rootCmd.Flags().StringVarP(&fileTypes, "types", "t", ".go,.md,.txt", "Comma-separated list of file extensions to include") - rootCmd.Flags().StringVarP(&codeGenPatterns, "codegen", "c", "", "Comma-separated list of glob patterns for code-generated files") + rootCmd.Flags().StringVarP(&codeGenPatterns, "codegen", "g", "", "Comma-separated list of glob patterns for code-generated files") + rootCmd.Flags().StringVarP(&configFile, "config", "f", "", "Path to the config file (default: rollup.yml in the current directory)") } func matchGlob(pattern, path string) bool { @@ -82,10 +100,19 @@ func isCodeGenerated(filePath string, patterns []string) bool { return false } -func runRollup(path, fileTypes, codeGenPatterns string) error { - // Convert file types and code-generated patterns to slices - types := strings.Split(fileTypes, ",") - codeGenList := strings.Split(codeGenPatterns, ",") +func runRollup() error { + // Use config if available, otherwise use command-line flags + var types, codeGenList []string + if cfg != nil && len(cfg.FileTypes) > 0 { + types = cfg.FileTypes + } else { + types = strings.Split(fileTypes, ",") + } + if cfg != nil && len(cfg.CodeGenerated) > 0 { + codeGenList = cfg.CodeGenerated + } else { + codeGenList = strings.Split(codeGenPatterns, ",") + } // Get the absolute path absPath, err := filepath.Abs(path) @@ -120,7 +147,7 @@ func runRollup(path, fileTypes, codeGenPatterns string) error { } ext := filepath.Ext(path) for _, t := range types { - if ext == t { + if ext == "."+t { // Read file contents content, err := os.ReadFile(path) if err != nil { @@ -137,7 +164,7 @@ func runRollup(path, fileTypes, codeGenPatterns string) error { } // Write file name and contents to the output file - fmt.Fprintf(outputFile, "# File: %s%s\n\n```%s\n%s```\n\n", relPath, codeGenNote, ext[1:], string(content)) + fmt.Fprintf(outputFile, "# File: %s%s\n\n```%s\n%s```\n\n", relPath, codeGenNote, t, string(content)) break } }