github.com/cheikhshift/buffalo@v0.9.5/buffalo/cmd/root.go (about)

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