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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"os/user"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/markbates/inflect"
    12  	"github.com/sirupsen/logrus"
    13  
    14  	"github.com/pkg/errors"
    15  
    16  	"github.com/gobuffalo/buffalo/generators/newapp"
    17  	"github.com/gobuffalo/buffalo/meta"
    18  	"github.com/gobuffalo/envy"
    19  	"github.com/gobuffalo/makr"
    20  	"github.com/gobuffalo/plush"
    21  	"github.com/gobuffalo/pop"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  var rootPath string
    26  
    27  var app = newapp.Generator{
    28  	App:        meta.New("."),
    29  	DBType:     "postgres",
    30  	CIProvider: "none",
    31  	AsWeb:      true,
    32  	Docker:     "multi",
    33  	Bootstrap:  3,
    34  }
    35  
    36  var newCmd = &cobra.Command{
    37  	Use:   "new [name]",
    38  	Short: "Creates a new Buffalo application",
    39  	RunE: func(cmd *cobra.Command, args []string) error {
    40  		if len(args) <= 0 {
    41  			return errors.New("you must enter a name for your new application")
    42  		}
    43  
    44  		app.Name = inflect.Name(args[0])
    45  		app.Version = Version
    46  
    47  		if app.Name == "." {
    48  			app.Name = inflect.Name(filepath.Base(app.Root))
    49  		} else {
    50  			app.Root = filepath.Join(app.Root, app.Name.File())
    51  		}
    52  		aa := meta.New(app.Root)
    53  		app.ActionsPkg = aa.ActionsPkg
    54  		app.GriftsPkg = aa.GriftsPkg
    55  		app.ModelsPkg = aa.ModelsPkg
    56  		app.PackagePkg = aa.PackagePkg
    57  
    58  		if err := app.Validate(); err != nil {
    59  			if errors.Cause(err) == newapp.ErrNotInGoPath {
    60  				return notInGoPath(app)
    61  			}
    62  			return errors.WithStack(err)
    63  		}
    64  
    65  		app.WithPop = !app.SkipPop
    66  		app.WithWebpack = !app.SkipWebpack
    67  		app.WithYarn = !app.SkipYarn
    68  		app.AsWeb = !app.AsAPI
    69  		if app.AsAPI {
    70  			app.WithWebpack = false
    71  		}
    72  
    73  		if err := app.Run(app.Root, makr.Data{}); err != nil {
    74  			return errors.WithStack(err)
    75  		}
    76  
    77  		logrus.Infof("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name)
    78  		logrus.Infof("You can find your new application at:\n%v", app.Root)
    79  		logrus.Info("\nPlease read the README.md file in your new application for next steps on running your application.")
    80  
    81  		return nil
    82  	},
    83  }
    84  
    85  func currentUser() (string, error) {
    86  	if _, err := exec.LookPath("git"); err == nil {
    87  		if b, err := exec.Command("git", "config", "github.user").Output(); err != nil {
    88  			return string(b), nil
    89  		}
    90  	}
    91  	u, err := user.Current()
    92  	if err != nil {
    93  		return "", err
    94  	}
    95  	username := u.Username
    96  	if t := strings.Split(username, `\`); len(t) > 0 {
    97  		username = t[len(t)-1]
    98  	}
    99  	return username, nil
   100  }
   101  
   102  func notInGoPath(ag newapp.Generator) error {
   103  	username, err := currentUser()
   104  	if err != nil {
   105  		return errors.WithStack(err)
   106  	}
   107  	pwd, _ := os.Getwd()
   108  	t, err := plush.Render(notInGoWorkspace, plush.NewContextWith(map[string]interface{}{
   109  		"name":     ag.Name,
   110  		"gopath":   envy.GoPath(),
   111  		"current":  pwd,
   112  		"username": username,
   113  	}))
   114  	if err != nil {
   115  		return err
   116  	}
   117  	logrus.Error(t)
   118  	os.Exit(-1)
   119  	return nil
   120  }
   121  
   122  func init() {
   123  	pwd, _ := os.Getwd()
   124  
   125  	app.App = meta.New(pwd)
   126  
   127  	decorate("new", newCmd)
   128  	RootCmd.AddCommand(newCmd)
   129  	newCmd.Flags().BoolVar(&app.AsAPI, "api", false, "skip all front-end code and configure for an API server")
   130  	newCmd.Flags().BoolVarP(&app.Force, "force", "f", false, "delete and remake if the app already exists")
   131  	newCmd.Flags().BoolVarP(&app.Verbose, "verbose", "v", false, "verbosely print out the go get commands")
   132  	newCmd.Flags().BoolVar(&app.SkipPop, "skip-pop", false, "skips adding pop/soda to your app")
   133  	newCmd.Flags().BoolVar(&app.WithDep, "with-dep", false, "adds github.com/golang/dep to your app")
   134  	newCmd.Flags().BoolVar(&app.SkipWebpack, "skip-webpack", false, "skips adding Webpack to your app")
   135  	newCmd.Flags().BoolVar(&app.SkipYarn, "skip-yarn", false, "use npm instead of yarn for frontend dependencies management")
   136  	newCmd.Flags().StringVar(&app.DBType, "db-type", "postgres", fmt.Sprintf("specify the type of database you want to use [%s]", strings.Join(pop.AvailableDialects, ", ")))
   137  	newCmd.Flags().StringVar(&app.Docker, "docker", "multi", "specify the type of Docker file to generate [none, multi, standard]")
   138  	newCmd.Flags().StringVar(&app.CIProvider, "ci-provider", "none", "specify the type of ci file you would like buffalo to generate [none, travis, gitlab-ci]")
   139  	newCmd.Flags().StringVar(&app.VCS, "vcs", "git", "specify the Version control system you would like to use [none, git, bzr]")
   140  	newCmd.Flags().IntVar(&app.Bootstrap, "bootstrap", app.Bootstrap, "specify version for Bootstrap [3, 4]")
   141  }
   142  
   143  const notInGoWorkspace = `Oops! It would appear that you are not in your Go Workspace.
   144  
   145  Your $GOPATH is set to "<%= gopath %>".
   146  
   147  You are currently in "<%= current %>".
   148  
   149  The standard location for putting Go projects is something along the lines of "$GOPATH/src/github.com/<%= username %>/<%= name %>" (adjust accordingly).
   150  
   151  We recommend you go to "$GOPATH/src/github.com/<%= username %>/" and try "buffalo new <%= name %>" again.`