Skip to content

Commit 1682f4b

Browse files
author
wangwei
committed
ginhelper执行输出明细
1 parent abc9dcb commit 1682f4b

File tree

4 files changed

+67
-35
lines changed

4 files changed

+67
-35
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ __(2)、如果你只是在src目录下开发项目,你可以这样操作,这
6565
2、下载其他的依赖
6666

6767
go get -u github.com/gin-gonic/gin
68-
go get github.com/astaxie/beego #这里我们可以使用beego的confighttplib、logs、cache等模块
68+
go get github.com/astaxie/beego/config #这里我们可以使用beego的config, 如果你还想使用beego的其他模块请自行下载,如:httplib、logs、cache等模块
6969
go get github.com/beego/bee #可选下载
7070
github.com/jinzhu/gorm
7171
github.com/go-redis/redis

main.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,43 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"github.com/logrusorgru/aurora"
67
"github.com/will110/ginhelper/project"
78
)
89

9-
const usage = `
10-
Ginhelper is a gin web framework helper, Help you develop fast.
11-
12-
USAGE
13-
14-
ginhelper [arguments]
15-
16-
AVAILABLE ARGUMENTS
17-
18-
create -> Create single gin web framework
19-
`
10+
const _version = "v1.1.1"
2011

2112
func main() {
2213
flag.Parse()
2314
arg := flag.Args()
2415
if len(arg) == 0 {
25-
fmt.Println(usage)
16+
getHelp()
2617
return
2718
}
2819

29-
if arg[0] == "create" {
20+
switch arg[0] {
21+
case "create":
3022
project.GenerateProject()
31-
} else {
32-
fmt.Println(usage)
23+
case "version":
24+
fmt.Println("ginhelper " + _version)
25+
case "help":
26+
getHelp()
27+
default:
28+
getHelp()
3329
}
3430
}
31+
32+
func getHelp() {
33+
title :=aurora.White("Ginhelper is a gin web framework helper, Help you develop fast.")
34+
usage := aurora.Magenta("USAGE")
35+
t1 := aurora.BrightWhite("ginhelper [arguments]")
36+
t2 := aurora.Underline(aurora.Magenta("AVAILABLE ARGUMENTS"))
37+
t3 := aurora.BrightWhite("create ")
38+
t4 := aurora.Underline(aurora.BrightWhite("Create single gin web framework"))
39+
t5 := aurora.BrightWhite("version ")
40+
t6 := aurora.Underline(aurora.BrightWhite("Prints the current GinHelper version"))
41+
t7 := aurora.Underline(aurora.BrightWhite("Use ginhelper help for more information."))
42+
43+
buf := fmt.Sprintf("%v\n\n%v\n %v\n\n%v\n\n %v%v\n %v%v\n\n\n\n%v", title, usage, t1, t2, t3, t4, t5, t6, t7)
44+
fmt.Println(buf)
45+
}

project/project.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"bufio"
55
"errors"
66
"fmt"
7+
"github.com/logrusorgru/aurora"
78
"log"
89
"os"
910
"os/exec"
1011
"path"
1112
"path/filepath"
1213
"strings"
14+
"time"
1315
)
1416

1517
var dirList = []string{
@@ -56,10 +58,10 @@ func GenerateProject() {
5658
appName = modName
5759
}
5860

59-
fmt.Println("Project creating ...")
61+
fmt.Println(aurora.BrightWhite("[INFO] Creating project..."))
6062
generateDir()
6163
generateAllFile()
62-
fmt.Println("Congratulations on the completion of the project. Take a look at it.")
64+
fmt.Println(aurora.BrightRed(time.Now().Format("2006-01-02 15:04:05") + " [SUCC] New project successfully created, Take a look at it."))
6365
}
6466

6567
func generateDir() {
@@ -69,6 +71,8 @@ func generateDir() {
6971
if _, err := os.Stat(fp); os.IsNotExist(err) {
7072
if er := os.MkdirAll(fp, 0777); er != nil {
7173
log.Fatalf("Could not create the "+ v +" directory: %s", er)
74+
}else {
75+
fmt.Println(strings.ReplaceAll(fp, "/", getSep()))
7276
}
7377
}
7478
}
@@ -137,6 +141,7 @@ func generateGitignoreFile() {
137141
gitignoreTmep += appName + ".exe\n" + appName + ".exe*\n" + appName+"\n"
138142
_, _ = f.WriteString(gitignoreTmep)
139143
_ = f.Close()
144+
fmt.Println(strings.ReplaceAll(fpath, "/", getSep()))
140145
}
141146

142147
func getAppDir() (string, error) {
@@ -210,4 +215,10 @@ func generateFile(dir, fileName, str string, ext ...string) {
210215
_ = f.Close()
211216
cmd := exec.Command("gofmt", "-w", fpath)
212217
_ = cmd.Run()
218+
219+
fmt.Println(strings.ReplaceAll(fpath, "/", getSep()))
220+
}
221+
222+
func getSep() string {
223+
return string(filepath.Separator)
213224
}

project/temp.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,26 @@ var localAppConf = `name = "hello-local"
204204
var previewAppConf = `include "app.conf"
205205
#当前环境的配置可以配置在这里
206206
207+
name = "preivew"
208+
207209
#请保持在最后一行
208210
include "local_app.conf"
209211
`
210212

211213
var prodAppConf = `include "app.conf"
212214
#当前环境的配置可以配置在这里
213215
216+
name = "prod"
217+
214218
#请保持在最后一行
215219
include "local_app.conf"
216220
`
217221

218222
var testAppConf = `include "app.conf"
219223
#当前环境的配置可以配置在这里
220224
225+
name = "test"
226+
221227
#请保持在最后一行
222228
include "local_app.conf"
223229
`
@@ -359,20 +365,20 @@ import (
359365
"flag"
360366
"github.com/jinzhu/gorm"
361367
"fmt"
362-
"github.com/astaxie/beego"
368+
"{{baseDir}}/pkg/utils"
363369
)
364370
365371
var (
366372
Conn *gorm.DB
367373
)
368374
369375
func GetDbConnect() (*gorm.DB, error) {
370-
dbConStr := fmt.Sprintf("%s:%s@tcp(%s)/%s%s?charset=utf8&parseTime=True&loc=Local", beego.AppConfig.String("db::user"), beego.AppConfig.String("db::password"), beego.AppConfig.String("db::host"), beego.AppConfig.String("db::database"), getConnectDbName())
376+
dbConStr := fmt.Sprintf("%s:%s@tcp(%s)/%s%s?charset=utf8&parseTime=True&loc=Local", utils.BeeConfig.String("db::user"), utils.BeeConfig.String("db::password"), utils.BeeConfig.String("db::host"), utils.BeeConfig.String("db::database"), getConnectDbName())
371377
fmt.Println(dbConStr, "dbConStr")
372378
db, err := gorm.Open("mysql", dbConStr)
373379
Conn = db
374380
375-
runMode := beego.AppConfig.String("runmode")
381+
runMode := utils.BeeConfig.String("runmode")
376382
if runMode == "dev" || runMode == "test" {
377383
db.LogMode(true)
378384
}
@@ -395,11 +401,11 @@ var pkgDbMongodbTemp = `package db
395401
import (
396402
"context"
397403
"fmt"
398-
"github.com/astaxie/beego"
404+
"time"
405+
"{{baseDir}}/pkg/utils"
399406
"go.mongodb.org/mongo-driver/mongo"
400407
"go.mongodb.org/mongo-driver/mongo/options"
401408
"go.mongodb.org/mongo-driver/mongo/readpref"
402-
"time"
403409
)
404410
405411
var (
@@ -409,7 +415,7 @@ var (
409415
//连接mongodb
410416
func GetMongodbClient() (*mongo.Client, error) {
411417
ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
412-
host := fmt.Sprintf("mongodb://%s", beego.AppConfig.String("mongodb::host"))
418+
host := fmt.Sprintf("mongodb://%s", utils.BeeConfig.String("mongodb::host"))
413419
client, err := mongo.Connect(ctx, options.Client().ApplyURI(host))
414420
cancel()
415421
if err != nil {
@@ -435,7 +441,7 @@ var pkgDbRedisTemp = `package db
435441
/*
436442
import (
437443
"github.com/go-redis/redis"
438-
"github.com/astaxie/beego"
444+
"{{baseDir}}/pkg/utils"
439445
)
440446
441447
var (
@@ -444,15 +450,15 @@ var (
444450
445451
//连接redis
446452
func GetRedisClient() (*redis.Client, error){
447-
db ,_ := beego.AppConfig.Int("redis::database")
453+
db ,_ := utils.BeeConfig.Int("redis::database")
448454
redisOption := &redis.Options{
449-
Addr: beego.AppConfig.String("redis::host"),
455+
Addr: utils.BeeConfig.String("redis::host"),
450456
DB: db,
451457
}
452458
453-
runMode := beego.AppConfig.String("runmode")
459+
runMode := utils.BeeConfig.String("runmode")
454460
if runMode != "prod" {
455-
redisOption.Password = beego.AppConfig.String("redis::password")
461+
redisOption.Password = utils.BeeConfig.String("redis::password")
456462
}
457463
client := redis.NewClient(redisOption)
458464
@@ -505,8 +511,11 @@ type UserForm struct {
505511

506512
var pkgUtilsConfigTemp = `package utils
507513
514+
import "github.com/astaxie/beego/config"
515+
508516
var (
509517
MyConfig *WebConfig
518+
BeeConfig config.Configer
510519
)
511520
512521
type WebConfig struct {
@@ -782,7 +791,7 @@ import (
782791
"log"
783792
"os"
784793
"{{baseDir}}/pkg/utils"
785-
"github.com/astaxie/beego"
794+
"github.com/astaxie/beego/config"
786795
)
787796
788797
var envMode = "WebRunMode"
@@ -797,10 +806,11 @@ func InitConfig() {
797806
}
798807
799808
configName := fmt.Sprintf("conf/%s_app.conf", env)
800-
err := beego.LoadAppConfig("ini", configName)
809+
beeConfig, err := config.NewConfig("ini", configName)
801810
if err != nil {
802811
log.Fatal(err)
803812
}
813+
utils.BeeConfig = beeConfig
804814
805815
configList, err := LoadConfig()
806816
if err != nil {
@@ -816,10 +826,10 @@ func InitConfig() {
816826
*/
817827
func LoadConfig() (*utils.WebConfig, error) {
818828
config := &utils.WebConfig{}
819-
config.DbHost = beego.AppConfig.String("db::host")
820-
config.DbName = beego.AppConfig.String("db::user")
821-
config.DbPassword = beego.AppConfig.String("db::password")
822-
version, err := beego.AppConfig.Float("version")
829+
config.DbHost = utils.BeeConfig.String("db::host")
830+
config.DbName = utils.BeeConfig.String("db::user")
831+
config.DbPassword = utils.BeeConfig.String("db::password")
832+
version, err := utils.BeeConfig.Float("version")
823833
if err != nil {
824834
config.Version = version
825835
}

0 commit comments

Comments
 (0)