forked from kujtimiihoxha/kit
-
Notifications
You must be signed in to change notification settings - Fork 53
/
g_service.go
84 lines (80 loc) · 3.03 KB
/
g_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cmd
import (
"github.com/GrantZheng/kit/generator"
"github.com/GrantZheng/kit/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var methods []string
var initserviceCmd = &cobra.Command{
Use: "service",
Short: "Initiate a service",
Aliases: []string{"s"},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
logrus.Error("You must provide a name for the service")
return
}
if viper.GetString("g_s_transport") == "grpc" {
if !checkProtoc() {
return
}
}
// pbPath,pbImportPath only work when transport is grpc
var pbPath, pbImportPath string
pbPath = viper.GetString("g_s_pb_path")
pbImportPath = viper.GetString("g_s_pb_import_path")
if pbPath != "" {
exist := utils.IsExist(pbPath)
if !exist {
logrus.Errorf("You must provide a existed pb path to put pb dir, given path:<%s> does not exist", pbPath)
return
}
// The pbImportPath validity is not checked here, and it is the responsibility of the developer
if pbImportPath == "" {
logrus.Error("You must provide pb import path by --pb_import_path or -i, because you have provided pb_path")
return
}
}
var emw, smw bool
if viper.GetBool("g_s_dmw") {
emw = true
smw = true
} else {
emw = viper.GetBool("g_s_endpoint_mdw")
smw = viper.GetBool("g_s_svc_mdw")
}
g := generator.NewGenerateService(
args[0],
viper.GetString("g_s_transport"),
pbPath,
pbImportPath,
smw,
viper.GetBool("g_s_gorilla"),
emw,
methods,
)
if err := g.Generate(); err != nil {
logrus.Error(err)
}
},
}
func init() {
generateCmd.AddCommand(initserviceCmd)
initserviceCmd.Flags().StringP("transport", "t", "http", "The transport you want your service to be initiated with")
initserviceCmd.Flags().StringP("pb_path", "p", "", "Specify path to store pb dir")
initserviceCmd.Flags().StringP("pb_import_path", "i", "", "Specify path to import pb")
initserviceCmd.Flags().BoolP("dmw", "w", false, "Generate default middleware for service and endpoint")
initserviceCmd.Flags().Bool("gorilla", false, "Generate http using gorilla mux")
initserviceCmd.Flags().StringArrayVarP(&methods, "methods", "m", []string{}, "Specify methods to be generated")
initserviceCmd.Flags().Bool("svc-mdw", false, "If set a default Logging and Instrumental middleware will be created and attached to the service")
initserviceCmd.Flags().Bool("endpoint-mdw", false, "If set a default Logging and Tracking middleware will be created and attached to the endpoint")
viper.BindPFlag("g_s_transport", initserviceCmd.Flags().Lookup("transport"))
viper.BindPFlag("g_s_pb_path", initserviceCmd.Flags().Lookup("pb_path"))
viper.BindPFlag("g_s_pb_import_path", initserviceCmd.Flags().Lookup("pb_import_path"))
viper.BindPFlag("g_s_dmw", initserviceCmd.Flags().Lookup("dmw"))
viper.BindPFlag("g_s_gorilla", initserviceCmd.Flags().Lookup("gorilla"))
viper.BindPFlag("g_s_svc_mdw", initserviceCmd.Flags().Lookup("svc-mdw"))
viper.BindPFlag("g_s_endpoint_mdw", initserviceCmd.Flags().Lookup("endpoint-mdw"))
}