github.com/jasonish/buffalo@v0.8.2-0.20170413145823-bacbdd415f1b/buffalo/cmd/console.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  
     9  	"github.com/gobuffalo/makr"
    10  	"github.com/markbates/inflect"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  // consoleCmd represents the console command
    15  var consoleCmd = &cobra.Command{
    16  	Use:     "console",
    17  	Aliases: []string{"c"},
    18  	Short:   "Runs your Buffalo app in a REPL console",
    19  	RunE: func(c *cobra.Command, args []string) error {
    20  		_, err := exec.LookPath("gore")
    21  		if err != nil {
    22  			return errors.New("we could not find \"gore\" in your path. You must first install \"gore\" in order to use the Buffalo console:\n\n$ go get -u github.com/motemen/gore")
    23  		}
    24  		packagePath := packagePath(rootPath)
    25  		packages := []string{}
    26  		for _, p := range []string{"models", "actions"} {
    27  			s, _ := os.Stat(filepath.Join(rootPath, p))
    28  			if s != nil {
    29  				packages = append(packages, filepath.Join(packagePath, p))
    30  			}
    31  		}
    32  
    33  		fname := inflect.Parameterize(packagePath) + "_loader.go"
    34  		g := makr.New()
    35  		g.Add(makr.NewFile(fname, cMain))
    36  		err = g.Run(os.TempDir(), makr.Data{
    37  			"packages": packages,
    38  		})
    39  		os.Chdir(rootPath)
    40  		if err != nil {
    41  			return err
    42  		}
    43  
    44  		cmd := exec.Command("gore", "-autoimport", "-context", filepath.Join(os.TempDir(), fname))
    45  		cmd.Stdin = os.Stdin
    46  		cmd.Stderr = os.Stderr
    47  		cmd.Stdout = os.Stdout
    48  
    49  		return cmd.Run()
    50  	},
    51  }
    52  
    53  func init() {
    54  	RootCmd.AddCommand(consoleCmd)
    55  }
    56  
    57  var cMain = `
    58  package main
    59  
    60  {{range .packages}}
    61  import _ "{{.}}"
    62  {{end}}
    63  `