<commit_before>package cmd

import (
	"fmt"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strings"

	"github.com/pkg/errors"

	"github.com/gobuffalo/buffalo/generators/newapp"
	"github.com/gobuffalo/buffalo/meta"
	"github.com/gobuffalo/envy"
	"github.com/gobuffalo/makr"
	"github.com/gobuffalo/plush"
	"github.com/spf13/cobra"
)

var rootPath string

var app = newapp.Generator{
	App:        meta.New("."),
	DBType:     "postgres",
	CIProvider: "none",
	AsWeb:      true,
	Docker:     "multi",
	VCS:        "git",
}

var newCmd = &cobra.Command{
	Use:   "new [name]",
	Short: "Creates a new Buffalo application",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) <= 0 {
			return errors.New("you must enter a name for your new application")
		}

		app.Name = meta.Name(args[0])
		app.Version = Version

		if app.Name == "." {
			app.Name = meta.Name(filepath.Base(app.Root))
		} else {
			app.Root = filepath.Join(app.Root, app.Name.File())
		}
		aa := meta.New(app.Root)
		app.ActionsPkg = aa.ActionsPkg
		app.GriftsPkg = aa.GriftsPkg
		app.ModelsPkg = aa.ModelsPkg
		app.PackagePkg = aa.PackagePkg

		if err := app.Validate(); err != nil {
			if errors.Cause(err) == newapp.ErrNotInGoPath {
				return notInGoPath(app)
			}
			return errors.WithStack(err)
		}

		app.WithPop = !app.SkipPop
		app.WithWebpack = !app.SkipWebpack
		app.WithYarn = !app.SkipYarn
		app.AsWeb = !app.AsAPI

		if err := app.Run(app.Root, makr.Data{}); err != nil {
			return errors.WithStack(err)
		}

		fmt.Printf("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name)
		fmt.Println("You can find your new application at:")
		fmt.Println(app.Root)
		fmt.Println("\nPlease read the README.md file in your new application for next steps on running your application.")

		return nil
	},
}

func notInGoPath(ag newapp.Generator) error {
	b, _ := exec.Command("git", "config", "github.user").Output()
	username := string(b)
	if username == "" {
		u, err := user.Current()
		if err != nil {
			return err
		}
		username = u.Username
		if t := strings.Split(username, `\`); len(t) > 0 {
			username = t[len(t)-1]
		}
	}
	pwd, _ := os.Getwd()
	t, err := plush.Render(notInGoWorkspace, plush.NewContextWith(map[string]interface{}{
		"name":     ag.Name,
		"gopath":   envy.GoPath(),<commit_msg>made a few tweaks to the PR<commit_after>package cmd

import (
	"fmt"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strings"

	"github.com/pkg/errors"

	"github.com/gobuffalo/buffalo/generators/newapp"
	"github.com/gobuffalo/buffalo/meta"
	"github.com/gobuffalo/envy"
	"github.com/gobuffalo/makr"
	"github.com/gobuffalo/plush"
	"github.com/spf13/cobra"
)

var rootPath string

var app = newapp.Generator{
	App:        meta.New("."),
	DBType:     "postgres",
	CIProvider: "none",
	AsWeb:      true,
	Docker:     "multi",
	VCS:        "git",
}

var newCmd = &cobra.Command{
	Use:   "new [name]",
	Short: "Creates a new Buffalo application",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) <= 0 {
			return errors.New("you must enter a name for your new application")
		}

		app.Name = meta.Name(args[0])
		app.Version = Version

		if app.Name == "." {
			app.Name = meta.Name(filepath.Base(app.Root))
		} else {
			app.Root = filepath.Join(app.Root, app.Name.File())
		}
		aa := meta.New(app.Root)
		app.ActionsPkg = aa.ActionsPkg
		app.GriftsPkg = aa.GriftsPkg
		app.ModelsPkg = aa.ModelsPkg
		app.PackagePkg = aa.PackagePkg

		if err := app.Validate(); err != nil {
			if errors.Cause(err) == newapp.ErrNotInGoPath {
				return notInGoPath(app)
			}
			return errors.WithStack(err)
		}

		app.WithPop = !app.SkipPop
		app.WithWebpack = !app.SkipWebpack
		app.WithYarn = !app.SkipYarn
		app.AsWeb = !app.AsAPI

		if err := app.Run(app.Root, makr.Data{}); err != nil {
			return errors.WithStack(err)
		}

		fmt.Printf("Congratulations! Your application, %s, has been successfully built!\n\n", app.Name)
		fmt.Println("You can find your new application at:")
		fmt.Println(app.Root)
		fmt.Println("\nPlease read the README.md file in your new application for next steps on running your application.")

		return nil
	},
}

func currentUser() (string, error) {
	if _, err := exec.LookPath("git"); err == nil {
		if b, err := exec.Command("git", "config", "github.user").Output(); err != nil {
			return string(b), nil
		}
	}
	u, err := user.Current()
	if err != nil {
		return "", err
	}
	username := u.Username
	if t := strings.Split(username, `\`); len(t) > 0 {
		username = t[len(t)-1]
	}
	return username, nil
}

func notInGoPath(ag newapp.Generator) error {
	username, err := currentUser()
	if err != nil {
		return errors.WithStack(err)
	}
	pwd, _ := os.Getwd()
	t, err := plush.Render(notInGoWorkspace, plush.NewContextWith(map[string]interface{}{
		"name":     ag.Name,
		"gopath":   envy.GoPath(),
{'fraction_non_alphanumeric': 0.15888429752066116, 'fraction_numerical': 0.0024793388429752068, 'mean_word_length': 3.4290942360475754, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3260444', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1861, 'n_words': 406}