Skip to content

Commit

Permalink
feat: update .gitignore with _ftl during Go init
Browse files Browse the repository at this point in the history
fixes #776
  • Loading branch information
worstell committed Jan 16, 2024
1 parent 561cef4 commit af0cbd8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ reflex.conf
generated_ftl_module.go
*.zip
dist/
examples/**/_ftl/
examples/**/go.work
examples/**/go.work.sum
**/_ftl
29 changes: 29 additions & 0 deletions cmd/ftl/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"archive/zip"
"bufio"
"context"
"fmt"
"html/template"
"os"
"path"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -41,6 +43,9 @@ func (i initGoCmd) Run(ctx context.Context, parent *initCmd) error {
if err := scaffold(parent.Hermit, goruntime.Files(), i.Dir, i, scaffolder.Exclude("^go.mod$")); err != nil {
return err
}
if err := updateGitIgnore(ctx, i.Dir); err != nil {
return err
}
logger.Infof("Running go mod tidy")
return exec.Command(ctx, log.Debug, filepath.Join(i.Dir, i.Name), "go", "mod", "tidy").Run()
}
Expand Down Expand Up @@ -143,3 +148,27 @@ var scaffoldFuncs = template.FuncMap{
return reflect.Indirect(reflect.ValueOf(v)).Type().Name()
},
}

func updateGitIgnore(ctx context.Context, dir string) error {
gitRoot := gitRoot(ctx, dir)
f, err := os.OpenFile(path.Join(gitRoot, ".gitignore"), os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close() //nolint:gosec

scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.TrimSpace(scanner.Text()) == "**/_ftl" {
return nil
}
}

if scanner.Err() != nil {
return scanner.Err()
}

// append if not already present
_, err = f.WriteString("**/_ftl\n")
return err
}

0 comments on commit af0cbd8

Please sign in to comment.