github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/cmd/new/options.go (about) 1 package new 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "os/user" 8 "path/filepath" 9 "strings" 10 11 pop "github.com/gobuffalo/buffalo-pop/v3/genny/newapp" 12 "github.com/JarrahG/buffalocli/internal/genny/ci" 13 "github.com/JarrahG/buffalocli/internal/genny/docker" 14 "github.com/JarrahG/buffalocli/internal/genny/newapp/core" 15 "github.com/JarrahG/buffalocli/internal/genny/refresh" 16 "github.com/JarrahG/buffalocli/internal/genny/vcs" 17 fname "github.com/gobuffalo/flect/name" 18 "github.com/gobuffalo/meta" 19 "github.com/spf13/viper" 20 ) 21 22 var configError error 23 24 type options struct { 25 Options *core.Options 26 Module string 27 Force bool 28 Verbose bool 29 DryRun bool 30 } 31 32 func parseNewOptions(args []string) (options, error) { 33 nopts := options{ 34 Force: viper.GetBool("force"), 35 Verbose: viper.GetBool("verbose"), 36 DryRun: viper.GetBool("dry-run"), 37 Module: viper.GetString("module"), 38 } 39 40 if len(args) == 0 { 41 return nopts, fmt.Errorf("you must enter a name for your new application") 42 } 43 if configError != nil { 44 return nopts, configError 45 } 46 47 pwd, err := os.Getwd() 48 if err != nil { 49 return nopts, err 50 } 51 app := meta.New(pwd) 52 app.WithGrifts = true 53 app.Name = fname.New(args[0]) 54 app.Bin = filepath.Join("bin", app.Name.String()) 55 56 if app.Name.String() == "." { 57 app.Name = fname.New(filepath.Base(app.Root)) 58 } else { 59 app.Root = filepath.Join(app.Root, app.Name.File().String()) 60 } 61 62 if len(nopts.Module) == 0 { 63 aa := meta.New(app.Root) 64 app.PackageRoot(aa.PackagePkg) 65 } else { 66 app.PackageRoot(nopts.Module) 67 } 68 69 app.AsAPI = viper.GetBool("api") 70 app.VCS = viper.GetString("vcs") 71 72 app.WithPop = !viper.GetBool("skip-pop") 73 app.WithWebpack = !viper.GetBool("skip-webpack") 74 app.WithYarn = !viper.GetBool("skip-yarn") 75 app.WithNodeJs = app.WithWebpack 76 app.AsWeb = !app.AsAPI 77 78 if app.AsAPI { 79 app.WithWebpack = false 80 app.WithYarn = false 81 app.WithNodeJs = false 82 } 83 84 opts := &core.Options{} 85 86 if x := viper.GetBool("skip-docker"); !x { 87 opts.Docker = &docker.Options{} 88 } 89 90 app.WithDocker = !viper.GetBool("skip-docker") 91 92 if x := viper.GetString("ci-provider"); len(x) > 0 && x != "none" { 93 opts.CI = &ci.Options{ 94 Provider: x, 95 DBType: viper.GetString("db-type"), 96 } 97 } 98 99 if len(app.VCS) > 0 && app.VCS != "none" { 100 opts.VCS = &vcs.Options{ 101 Provider: app.VCS, 102 } 103 } 104 105 if app.WithPop { 106 d := viper.GetString("db-type") 107 if d == "sqlite3" { 108 app.WithSQLite = true 109 } 110 111 opts.Pop = &pop.Options{ 112 Prefix: app.Name.File().String(), 113 Dialect: d, 114 } 115 } 116 117 opts.Refresh = &refresh.Options{} 118 119 opts.App = app 120 nopts.Options = opts 121 122 return nopts, nil 123 } 124 125 func initConfig(skipConfig *bool, cfgFile *string) func() { 126 return func() { 127 if *skipConfig { 128 return 129 } 130 131 var err error 132 if *cfgFile != "" { // enable ability to specify config file via flag 133 viper.SetConfigFile(*cfgFile) 134 // Will error only if the --config flag is used 135 if err = viper.ReadInConfig(); err != nil { 136 configError = err 137 } 138 } else { 139 viper.SetConfigName(".buffalo") // name of config file (without extension) 140 viper.AddConfigPath("$HOME") // adding home directory as first search path 141 viper.AutomaticEnv() // read in environment variables that match 142 viper.ReadInConfig() 143 } 144 } 145 } 146 147 func currentUser() (string, error) { 148 _, err := exec.LookPath("git") 149 if err == nil { 150 b, err := exec.Command("git", "config", "github.user").Output() 151 if err == nil { 152 return string(b), nil 153 } 154 } 155 156 u, err := user.Current() 157 if err != nil { 158 return "", err 159 } 160 161 username := u.Username 162 if t := strings.Split(username, `\`); len(t) > 0 { 163 username = t[len(t)-1] 164 } 165 166 return username, nil 167 }