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

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