@@ -16,6 +16,7 @@ package apiapp
16
16
17
17
import (
18
18
"fmt"
19
+ "github.com/beego/bee/logger/colors"
19
20
"os"
20
21
path "path/filepath"
21
22
"strings"
@@ -35,14 +36,15 @@ var CmdApiapp = &commands.Command{
35
36
The command 'api' creates a Beego API application.
36
37
37
38
{{"Example:"|bold}}
38
- $ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
39
+ $ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-module=true] [-beego=v1.12.1]
39
40
40
41
If 'conn' argument is empty, the command will generate an example API application. Otherwise the command
41
42
will connect to your database and generate models based on the existing tables.
42
43
43
44
The command 'api' creates a folder named [appname] with the following structure:
44
45
45
46
├── main.go
47
+ ├── go.mod
46
48
├── {{"conf"|foldername}}
47
49
│ └── app.conf
48
50
├── {{"controllers"|foldername}}
@@ -103,6 +105,14 @@ func main() {
103
105
beego.Run()
104
106
}
105
107
108
+ `
109
+ var goMod = `
110
+ module %s
111
+
112
+ go %s
113
+
114
+ require github.com/astaxie/beego %s
115
+ require github.com/smartystreets/goconvey v1.6.4
106
116
`
107
117
108
118
var apirouter = `// @APIVersion 1.0.0
@@ -533,11 +543,15 @@ func TestGet(t *testing.T) {
533
543
}
534
544
535
545
`
546
+ var module utils.DocValue
547
+ var beegoVersion utils.DocValue
536
548
537
549
func init () {
538
550
CmdApiapp .Flag .Var (& generate .Tables , "tables" , "List of table names separated by a comma." )
539
551
CmdApiapp .Flag .Var (& generate .SQLDriver , "driver" , "Database driver. Either mysql, postgres or sqlite." )
540
552
CmdApiapp .Flag .Var (& generate .SQLConn , "conn" , "Connection string used by the driver to connect to a database instance." )
553
+ CmdApiapp .Flag .Var (& module , "module" , "Support go modules" )
554
+ CmdApiapp .Flag .Var (& beegoVersion , "beego" , "set beego version,only take effect by -module=true" )
541
555
commands .AvailableCommands = append (commands .AvailableCommands , CmdApiapp )
542
556
}
543
557
@@ -548,14 +562,38 @@ func createAPI(cmd *commands.Command, args []string) int {
548
562
beeLogger .Log .Fatal ("Argument [appname] is missing" )
549
563
}
550
564
551
- if len (args ) > 1 {
552
- err := cmd .Flag .Parse (args [1 :])
565
+ if len (args ) >= 2 {
566
+ cmd .Flag .Parse (args [1 :])
567
+ } else {
568
+ module = "false"
569
+ }
570
+ var appPath string
571
+ var packPath string
572
+ var err error
573
+ if module != `true` {
574
+ beeLogger .Log .Info ("generate api project support GOPATH" )
575
+ version .ShowShortVersionBanner ()
576
+ appPath , packPath , err = utils .CheckEnv (args [0 ])
553
577
if err != nil {
554
- beeLogger .Log .Error (err .Error ())
578
+ beeLogger .Log .Fatalf ("%s" , err )
579
+ }
580
+ } else {
581
+ beeLogger .Log .Info ("generate api project support go modules." )
582
+ appPath = path .Join (utils .GetBeeWorkPath (), args [0 ])
583
+ packPath = args [0 ]
584
+ if beegoVersion .String () == `` {
585
+ beegoVersion .Set (`v1.12.1` )
586
+ }
587
+ }
588
+
589
+ if utils .IsExist (appPath ) {
590
+ beeLogger .Log .Errorf (colors .Bold ("Application '%s' already exists" ), appPath )
591
+ beeLogger .Log .Warn (colors .Bold ("Do you want to overwrite it? [Yes|No] " ))
592
+ if ! utils .AskForConfirmation () {
593
+ os .Exit (2 )
555
594
}
556
595
}
557
596
558
- appPath , packPath , err := utils .CheckEnv (args [0 ])
559
597
appName := path .Base (args [0 ])
560
598
if err != nil {
561
599
beeLogger .Log .Fatalf ("%s" , err )
@@ -567,6 +605,10 @@ func createAPI(cmd *commands.Command, args []string) int {
567
605
beeLogger .Log .Info ("Creating API..." )
568
606
569
607
os .MkdirAll (appPath , 0755 )
608
+ if module == `true` { //generate first for calc model name
609
+ fmt .Fprintf (output , "\t %s%screate%s\t %s%s\n " , "\x1b [32m" , "\x1b [1m" , "\x1b [21m" , path .Join (appPath , "go.mod" ), "\x1b [0m" )
610
+ utils .WriteToFile (path .Join (appPath , "go.mod" ), fmt .Sprintf (goMod , packPath , utils .GetGoVersionSkipMinor (), beegoVersion .String ()))
611
+ }
570
612
fmt .Fprintf (output , "\t %s%screate%s\t %s%s\n " , "\x1b [32m" , "\x1b [1m" , "\x1b [21m" , appPath , "\x1b [0m" )
571
613
os .Mkdir (path .Join (appPath , "conf" ), 0755 )
572
614
fmt .Fprintf (output , "\t %s%screate%s\t %s%s\n " , "\x1b [32m" , "\x1b [1m" , "\x1b [21m" , path .Join (appPath , "conf" ), "\x1b [0m" )
0 commit comments