github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/buffalo/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/gobuffalo/buffalo"
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/spf13/cobra"
    11  )
    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:         "Build Buffalo applications with ease",
    20  	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    21  		if err := buffalo.LoadPlugins(); err != nil {
    22  			return err
    23  		}
    24  		isFreeCommand := false
    25  		for _, freeCmd := range anywhereCommands {
    26  			if freeCmd == cmd.Name() {
    27  				isFreeCommand = true
    28  			}
    29  		}
    30  
    31  		if isFreeCommand {
    32  			return nil
    33  		}
    34  
    35  		if !insideBuffaloProject() {
    36  			return fmt.Errorf("you need to be inside your buffalo project path to run this command")
    37  		}
    38  
    39  		return nil
    40  	},
    41  }
    42  
    43  // Execute adds all child commands to the root command sets flags appropriately.
    44  // This is called by main.main(). It only needs to happen once to the rootCmd.
    45  func Execute() {
    46  	if err := RootCmd.Execute(); err != nil {
    47  		if strings.Contains(err.Error(), dbNotFound) || strings.Contains(err.Error(), popNotFound) {
    48  			logrus.Errorf(popInstallInstructions)
    49  			os.Exit(-1)
    50  		}
    51  		logrus.Errorf("Error: %s", err)
    52  		if strings.Contains(err.Error(), dbNotFound) || strings.Contains(err.Error(), popNotFound) {
    53  			fmt.Println(popInstallInstructions)
    54  			os.Exit(-1)
    55  		}
    56  		os.Exit(-1)
    57  	}
    58  }
    59  
    60  const dbNotFound = `unknown command "db"`
    61  const popNotFound = `unknown command "pop"`
    62  const popInstallInstructions = `Pop support has been moved to the https://github.com/gobuffalo/buffalo-pop plugin.
    63  
    64  !! PLEASE READ PLUGIN DOCUMENTATION - https://gobuffalo.io/en/docs/plugins
    65  
    66  Buffalo Plugins Installation*:
    67  
    68  	$ buffalo plugins install github.com/gobuffalo/buffalo-pop
    69  
    70  `
    71  
    72  func init() {
    73  	decorate("root", RootCmd)
    74  }
    75  
    76  func insideBuffaloProject() bool {
    77  	if _, err := os.Stat(".buffalo.dev.yml"); err != nil {
    78  		return false
    79  	}
    80  
    81  	return true
    82  }