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