Skip to content

Commit

Permalink
feat: add logic for a .bcignore file
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjydev committed Apr 18, 2024
1 parent a8892b7 commit c3795d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
31 changes: 31 additions & 0 deletions internal/templates/ignore.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 15 additions & 3 deletions internal/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package templates

import (
"embed"
"fmt"
"os"
"slices"
"strings"
"text/template"
)
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit c3795d8

Please sign in to comment.