From c3795d806280ef940184d73a0b6eb23230f71744 Mon Sep 17 00:00:00 2001 From: Hayden Young <22327045+hbjydev@users.noreply.github.com> Date: Thu, 18 Apr 2024 01:44:47 +0100 Subject: [PATCH] feat: add logic for a .bcignore file --- internal/templates/ignore.go | 31 +++++++++++++++++++++++++++++++ internal/templates/templates.go | 18 +++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 internal/templates/ignore.go diff --git a/internal/templates/ignore.go b/internal/templates/ignore.go new file mode 100644 index 0000000..bd7fb2f --- /dev/null +++ b/internal/templates/ignore.go @@ -0,0 +1,31 @@ +package templates + +import ( + "os" + "strings" +) + +func HasIgnoreFile(configDir string) bool { + _, err := os.Stat(configDir+"/.bcignore") + return err == nil +} + +func GetIgnoredFiles(configDir string) (files []string, err error) { + if !HasIgnoreFile(configDir) { + return + } + + b, err := os.ReadFile(configDir+"/.bcignore") + if err != nil { + return + } + + for _, line := range strings.Split(string(b), "\n") { + if strings.HasPrefix(line, "#") { + continue + } + files = append(files, line) + } + + return +} diff --git a/internal/templates/templates.go b/internal/templates/templates.go index 1443b80..dd8ca79 100644 --- a/internal/templates/templates.go +++ b/internal/templates/templates.go @@ -2,7 +2,9 @@ package templates import ( "embed" + "fmt" "os" + "slices" "strings" "text/template" ) @@ -64,12 +66,22 @@ func RenderTemplate(t *template.Template, path string, context any) (string, err } func WriteFiles(in map[string]string) error { - for k, v := range in { - if err := EnsureDirExists(k); err != nil { + for filename, contents := range in { + if err := EnsureDirExists(filename); err != nil { return err } - if err := os.WriteFile(k, []byte(v), 0644); err != nil { + ignored, err := GetIgnoredFiles(".") + if err != nil { + return fmt.Errorf("failed to get ignored files: %v", err) + } + + // Skip over files in the .bcignore file + if slices.Contains(ignored, filename) { + continue + } + + if err := os.WriteFile(filename, []byte(contents), 0644); err != nil { return err } }