github.com/corylanou/buffalo@v0.8.0/buffalo/cmd/new.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"os/user"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/gobuffalo/buffalo/generators/newapp"
    12  	"github.com/gobuffalo/envy"
    13  	"github.com/gobuffalo/plush"
    14  	"github.com/markbates/inflect"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  var rootPath string
    19  var app = &newapp.App{}
    20  
    21  var newCmd = &cobra.Command{
    22  	Use:   "new [name]",
    23  	Short: "Creates a new Buffalo application",
    24  	RunE: func(cmd *cobra.Command, args []string) error {
    25  		if !validDbType() {
    26  			return fmt.Errorf("Unknown db-type %s expecting one of postgres, mysql or sqlite3", app.DBType)
    27  		}
    28  
    29  		if len(args) == 0 {
    30  			return errors.New("you must enter a name for your new application")
    31  		}
    32  
    33  		app.Name = args[0]
    34  
    35  		if app.Name == "." {
    36  			app.Name = filepath.Base(app.RootPath)
    37  		} else {
    38  			app.RootPath = filepath.Join(app.RootPath, app.Name)
    39  		}
    40  
    41  		err := validateInGoPath()
    42  		if err != nil {
    43  			return err
    44  		}
    45  
    46  		s, _ := os.Stat(app.RootPath)
    47  		if s != nil {
    48  			if app.Force {
    49  				os.RemoveAll(app.RootPath)
    50  			} else {
    51  				return fmt.Errorf("%s already exists! Either delete it or use the -f flag to force", app.Name)
    52  			}
    53  		}
    54  
    55  		err = genNewFiles()
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		fmt.Printf("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name)
    61  		fmt.Println("You can find your new application at:")
    62  		fmt.Println(app.RootPath)
    63  		fmt.Println("\nPlease read the README.md file in your new application for next steps on running your application.")
    64  
    65  		return nil
    66  	},
    67  }
    68  
    69  func validDbType() bool {
    70  	return app.DBType == "postgres" || app.DBType == "mysql" || app.DBType == "sqlite3"
    71  }
    72  
    73  func validateInGoPath() error {
    74  	gpMultiple := envy.GoPaths()
    75  
    76  	var gp string
    77  	for i := 0; i < len(gpMultiple); i++ {
    78  		if strings.HasPrefix(app.RootPath, filepath.Join(gpMultiple[i], "src")) {
    79  			gp = gpMultiple[i]
    80  			break
    81  		}
    82  	}
    83  
    84  	if gp == "" {
    85  		u, err := user.Current()
    86  		if err != nil {
    87  			return err
    88  		}
    89  		t, err := plush.Render(notInGoWorkspace, plush.NewContextWith(map[string]interface{}{
    90  			"name":     app.Name,
    91  			"gopath":   envy.GoPath(),
    92  			"current":  app.RootPath,
    93  			"username": u.Username,
    94  		}))
    95  		if err != nil {
    96  			return err
    97  		}
    98  		fmt.Println(t)
    99  		os.Exit(-1)
   100  	}
   101  	return nil
   102  }
   103  
   104  func goPath(root string) string {
   105  	gpMultiple := envy.GoPaths()
   106  	path := ""
   107  
   108  	for i := 0; i < len(gpMultiple); i++ {
   109  		if strings.HasPrefix(root, filepath.Join(gpMultiple[i], "src")) {
   110  			path = gpMultiple[i]
   111  			break
   112  		}
   113  	}
   114  	return path
   115  }
   116  
   117  func packagePath(rootPath string) string {
   118  	gosrcpath := strings.Replace(filepath.Join(goPath(rootPath), "src"), "\\", "/", -1)
   119  	rootPath = strings.Replace(rootPath, "\\", "/", -1)
   120  	return strings.Replace(rootPath, gosrcpath+"/", "", 2)
   121  }
   122  
   123  func genNewFiles() error {
   124  	packagePath := packagePath(app.RootPath)
   125  
   126  	data := map[string]interface{}{
   127  		"name":        app.Name,
   128  		"titleName":   inflect.Titleize(app.Name),
   129  		"packagePath": packagePath,
   130  		"actionsPath": packagePath + "/actions",
   131  		"modelsPath":  packagePath + "/models",
   132  		"withPop":     !app.SkipPop,
   133  		"withWebpack": !app.SkipWebpack,
   134  		"dbType":      app.DBType,
   135  		"version":     Version,
   136  		"ciProvider":  app.CIProvider,
   137  	}
   138  	if app.WithYarn {
   139  		data["withYarn"] = true
   140  	}
   141  
   142  	g, err := app.Generator(data)
   143  	if err != nil {
   144  		return err
   145  	}
   146  	return g.Run(app.RootPath, data)
   147  }
   148  
   149  func init() {
   150  	pwd, _ := os.Getwd()
   151  
   152  	rootPath = pwd
   153  	app.RootPath = pwd
   154  
   155  	RootCmd.AddCommand(newCmd)
   156  	newCmd.Flags().BoolVarP(&app.Force, "force", "f", false, "delete and remake if the app already exists")
   157  	newCmd.Flags().BoolVarP(&app.Verbose, "verbose", "v", false, "verbosely print out the go get/install commands")
   158  	newCmd.Flags().BoolVar(&app.SkipPop, "skip-pop", false, "skips adding pop/soda to your app")
   159  	newCmd.Flags().BoolVar(&app.SkipWebpack, "skip-webpack", false, "skips adding Webpack to your app")
   160  	newCmd.Flags().BoolVar(&app.WithYarn, "with-yarn", false, "allows the use of yarn instead of npm as dependency manager")
   161  	newCmd.Flags().StringVar(&app.DBType, "db-type", "postgres", "specify the type of database you want to use [postgres, mysql, sqlite3]")
   162  	newCmd.Flags().StringVar(&app.CIProvider, "ci-provider", "none", "specify the type of ci file you would like buffalo to generate [none, travis, gitlab-ci]")
   163  }
   164  
   165  const notInGoWorkspace = `Oops! It would appear that you are not in your Go Workspace.
   166  
   167  Your $GOPATH is set to "<%= gopath %>".
   168  
   169  You are currently in "<%= current %>".
   170  
   171  The standard location for putting Go projects is something along the lines of "$GOPATH/src/github.com/<%= username %>/<%= name %>" (adjust accordingly).
   172  
   173  We recommend you go to "$GOPATH/src/github.com/<%= username %>/" and try "buffalo new <%= name %>" again.`
   174  
   175  const noGoPath = `You do not have a $GOPATH set. In order to work with Go, you must set up your $GOPATH and your Go Workspace.
   176  
   177  We recommend reading this tutorial on setting everything up: https://www.goinggo.net/2016/05/installing-go-and-your-workspace.html
   178  
   179  When you're ready come back and try again. Don't worry, Buffalo will be right here waiting for you. :)`