github.com/jacobsoderblom/buffalo@v0.11.0/buffalo/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  // var cfgFile string
    12  
    13  var anywhereCommands = []string{"new", "version", "info", "help"}
    14  
    15  // RootCmd is the hook for all of the other commands in the buffalo binary.
    16  var RootCmd = &cobra.Command{
    17  	SilenceErrors: true,
    18  	Use:           "buffalo",
    19  	Short:         "Helps you build your Buffalo applications that much easier!",
    20  	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    21  		isFreeCommand := false
    22  		for _, freeCmd := range anywhereCommands {
    23  			if freeCmd == cmd.Name() {
    24  				isFreeCommand = true
    25  			}
    26  		}
    27  
    28  		if isFreeCommand {
    29  			return nil
    30  		}
    31  
    32  		if insideBuffaloProject() == false {
    33  			return errors.New("you need to be inside your buffalo project path to run this command")
    34  		}
    35  
    36  		return nil
    37  	},
    38  }
    39  
    40  // Execute adds all child commands to the root command sets flags appropriately.
    41  // This is called by main.main(). It only needs to happen once to the rootCmd.
    42  func Execute() {
    43  	if err := RootCmd.Execute(); err != nil {
    44  		logrus.Errorf("Error: %s\n\n", err)
    45  		os.Exit(-1)
    46  	}
    47  }
    48  
    49  func init() {
    50  	decorate("root", RootCmd)
    51  }
    52  
    53  func insideBuffaloProject() bool {
    54  	if _, err := os.Stat(".buffalo.dev.yml"); err != nil {
    55  		return false
    56  	}
    57  
    58  	return true
    59  }
    60  
    61  // func init() {
    62  // cobra.OnInitialize(initConfig)
    63  // RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.buffalo.yaml)")
    64  // }
    65  
    66  // func initConfig() {
    67  // 	if cfgFile != "" { // enable ability to specify config file via flag
    68  // 		viper.SetConfigFile(cfgFile)
    69  // 	}
    70  //
    71  // 	viper.SetConfigName(".buffalo") // name of config file (without extension)
    72  // 	viper.AddConfigPath("$HOME")    // adding home directory as first search path
    73  // 	viper.AutomaticEnv()            // read in environment variables that match
    74  //
    75  // 	// If a config file is found, read it in.
    76  // 	if err := viper.ReadInConfig(); err == nil {
    77  // 		fmt.Println("Using config file:", viper.ConfigFileUsed())
    78  // 	}
    79  // }