github.com/lenfree/buffalo@v0.7.3-0.20170207163156-891616ea4064/buffalo/cmd/soda_generators.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/markbates/gentronics"
     5  	"github.com/markbates/pop/soda/cmd/generate"
     6  )
     7  
     8  func newSodaGenerator() *gentronics.Generator {
     9  	g := gentronics.New()
    10  
    11  	should := func(data gentronics.Data) bool {
    12  		if _, ok := data["withPop"]; ok {
    13  			return ok
    14  		}
    15  		return false
    16  	}
    17  
    18  	f := gentronics.NewFile("models/models.go", nModels)
    19  	f.Should = should
    20  	g.Add(f)
    21  
    22  	c := gentronics.NewCommand(goGet("github.com/markbates/pop/..."))
    23  	c.Should = should
    24  	g.Add(c)
    25  
    26  	c = gentronics.NewCommand(goInstall("github.com/markbates/pop/soda"))
    27  	c.Should = should
    28  	g.Add(c)
    29  
    30  	g.Add(&gentronics.Func{
    31  		Should: should,
    32  		Runner: func(rootPath string, data gentronics.Data) error {
    33  			data["dialect"] = data["dbType"]
    34  			return generate.GenerateConfig("./database.yml", data)
    35  		},
    36  	})
    37  
    38  	return g
    39  }
    40  
    41  const nModels = `package models
    42  
    43  import (
    44  	"log"
    45  	"os"
    46  
    47  	"github.com/markbates/going/defaults"
    48  	"github.com/markbates/pop"
    49  )
    50  
    51  // DB is a connection to your database to be used
    52  // throughout your application.
    53  var DB *pop.Connection
    54  
    55  func init() {
    56  	var err error
    57  	env := defaults.String(os.Getenv("GO_ENV"), "development")
    58  	DB, err = pop.Connect(env)
    59  	if err != nil {
    60  		log.Fatal(err)
    61  	}
    62  	pop.Debug = env == "development"
    63  }
    64  `