github.com/royge/pop@v4.13.1+incompatible/soda/cmd/generate/config_cmd.go (about)

     1  package generate
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/gobuffalo/genny"
    11  	"github.com/gobuffalo/pop"
    12  	"github.com/gobuffalo/pop/genny/config"
    13  	"github.com/gobuffalo/pop/internal/defaults"
    14  	"github.com/gobuffalo/pop/internal/oncer"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  func init() {
    19  	ConfigCmd.Flags().StringVarP(&dialect, "type", "t", "postgres", fmt.Sprintf("The type of database you want to use (%s)", strings.Join(pop.AvailableDialects, ", ")))
    20  }
    21  
    22  var dialect string
    23  
    24  // ConfigCmd is the command to generate pop config files
    25  var ConfigCmd = &cobra.Command{
    26  	Use:              "config",
    27  	Short:            "Generates a database.yml file for your project.",
    28  	PersistentPreRun: func(c *cobra.Command, args []string) {},
    29  	RunE: func(cmd *cobra.Command, args []string) error {
    30  		cflag := cmd.Flag("config")
    31  		cflagVal := ""
    32  		if cflag != nil {
    33  			cflagVal = cflag.Value.String()
    34  		}
    35  		cfgFile := defaults.String(cflagVal, "database.yml")
    36  
    37  		run := genny.WetRunner(context.Background())
    38  
    39  		pwd, _ := os.Getwd()
    40  		g, err := config.New(&config.Options{
    41  			Root:     pwd,
    42  			Prefix:   filepath.Base(pwd),
    43  			FileName: cfgFile,
    44  			Dialect:  dialect,
    45  		})
    46  		if err != nil {
    47  			return err
    48  		}
    49  		run.With(g)
    50  
    51  		return run.Run()
    52  	},
    53  }
    54  
    55  // GenerateConfig generates pop configuration files.
    56  //
    57  // Deprecated: use github.com/gobuffalo/pop/genny/config instead.
    58  func GenerateConfig(cfgFile string, data map[string]interface{}) error {
    59  	oncer.Deprecate(0, "generate.GenerateConfig", "Use github.com/gobuffalo/pop/genny/config instead.")
    60  	return Config(cfgFile, data)
    61  }
    62  
    63  // Config generates pop configuration files.
    64  // Deprecated: use github.com/gobuffalo/pop/genny/config instead.
    65  func Config(cfgFile string, data map[string]interface{}) error {
    66  	oncer.Deprecate(0, "generate.Config", "Use github.com/gobuffalo/pop/genny/config instead.")
    67  	pwd, _ := os.Getwd()
    68  
    69  	run := genny.WetRunner(context.Background())
    70  
    71  	d, _ := data["dialect"].(string)
    72  	g, err := config.New(&config.Options{
    73  		Root:     pwd,
    74  		Prefix:   filepath.Base(pwd),
    75  		FileName: cfgFile,
    76  		Dialect:  d,
    77  	})
    78  
    79  	if err != nil {
    80  		return err
    81  	}
    82  	run.With(g)
    83  
    84  	return run.Run()
    85  }