github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/git/initializer.go (about)

     1  package git
     2  
     3  import (
     4  	"context"
     5  	"embed"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/spf13/pflag"
    10  
    11  	"github.com/wawandco/ox/internal/source"
    12  	"github.com/wawandco/ox/plugins/base/new"
    13  )
    14  
    15  var (
    16  	//go:embed templates
    17  	templates embed.FS
    18  )
    19  
    20  // Initializer
    21  type Initializer struct{}
    22  
    23  func (i Initializer) Name() string {
    24  	return "model/initializer"
    25  }
    26  
    27  func (i *Initializer) Initialize(ctx context.Context, options new.Options) error {
    28  	keeps := []string{
    29  		"migrations",
    30  		"public",
    31  	}
    32  
    33  	for _, k := range keeps {
    34  		err := os.MkdirAll(filepath.Join(options.Folder, k), 0777)
    35  		if err != nil {
    36  			return err
    37  		}
    38  
    39  		err = os.WriteFile(filepath.Join(options.Folder, k, ".gitkeep"), []byte{}, 0777)
    40  		if err == nil {
    41  			continue
    42  		}
    43  
    44  		return err
    45  	}
    46  
    47  	files := []struct {
    48  		path     string
    49  		template string
    50  	}{
    51  		{filepath.Join(options.Folder, ".gitignore"), "templates/dot-gitignore.tmpl"},
    52  	}
    53  
    54  	for _, f := range files {
    55  		content, err := templates.ReadFile(f.template)
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		err = source.Build(f.path, string(content), nil)
    61  		if err != nil {
    62  			return err
    63  		}
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func (i *Initializer) ParseFlags([]string) {}
    70  func (i *Initializer) Flags() *pflag.FlagSet {
    71  	return pflag.NewFlagSet("buffalo-models-initializer", pflag.ContinueOnError)
    72  }