@@ -10,6 +10,7 @@ import (
1010 "github.com/aide-family/magicbox/strutil"
1111 "github.com/aide-family/rabbit/cmd"
1212 "github.com/aide-family/rabbit/internal/conf"
13+ "github.com/aide-family/rabbit/pkg/config"
1314 "github.com/aide-family/rabbit/pkg/enum"
1415)
1516
@@ -18,76 +19,93 @@ type Flags struct {
1819 configPath string
1920 clientConfigOutputPath string
2021
21- httpAddress string
22- httpNetwork string
23- httpTimeout string
24- grpcAddress string
25- grpcNetwork string
26- grpcTimeout string
27- environment string
22+ * conf. Bootstrap
23+ environment string
24+ httpTimeout string
25+ grpcTimeout string
26+ jwtExpire string
27+ eventBusTimeout string
28+ registryType string
2829}
2930
3031var flags Flags
3132
32- func (f * Flags ) addFlags (c * cobra.Command ) {
33- c .Flags ().StringVarP (& f .configPath , "config" , "c" , "./config" , "config file (default is ./config)" )
34- c .Flags ().StringVarP (& f .clientConfigOutputPath , "client-config-output" , "o" , "~/.rabbit/config.yaml" , "client config output file (default is ~/.rabbit/config.yaml)" )
33+ func (f * Flags ) addFlags (c * cobra.Command , bc * conf.Bootstrap ) {
34+ f .Bootstrap = bc
35+ c .Flags ().StringVarP (& f .configPath , "config" , "c" , "" , `Example: -c=./config/, --config="./config/"` )
36+ c .Flags ().StringVarP (& f .clientConfigOutputPath , "client-config-output" , "o" , "~/.rabbit/" , `Example: -o=./client/, --client-config-output="~/.rabbit/"` )
3537
36- c .Flags ().StringVar (& f .httpAddress , "http-address" , "0.0.0.0:8080" , "http address (default is 0.0.0.0:8080)" )
37- c .Flags ().StringVar (& f .httpNetwork , "http-network" , "tcp" , "http network (default is tcp)" )
38- c .Flags ().StringVar (& f .httpTimeout , "http-timeout" , "10s" , "http timeout (default is 10s)" )
39- c .Flags ().StringVar (& f .grpcAddress , "grpc-address" , "0.0.0.0:9090" , "grpc address (default is 0.0.0.0:9090)" )
40- c .Flags ().StringVar (& f .grpcNetwork , "grpc-network" , "tcp" , "grpc network (default is tcp)" )
41- c .Flags ().StringVar (& f .grpcTimeout , "grpc-timeout" , "10s" , "grpc timeout (default is 10s)" )
42- c .Flags ().StringVarP (& f .environment , "environment" , "e" , "PROD" , "environment (DEV, TEST, PREVIEW, PROD)" )
38+ c .Flags ().StringVar (& f .environment , "environment" , f .Environment .String (), `Example: --environment="DEV", --environment="TEST", --environment="PREVIEW", --environment="PROD"` )
39+ c .Flags ().StringVar (& f .Server .Http .Address , "http-address" , f .Server .Http .Address , `Example: --http-address="0.0.0.0:8080", --http-address=":8080"` )
40+ c .Flags ().StringVar (& f .Server .Http .Network , "http-network" , f .Server .Http .Network , `Example: --http-network="tcp"` )
41+ c .Flags ().StringVar (& f .httpTimeout , "http-timeout" , f .Server .Http .Timeout .AsDuration ().String (), `Example: --http-timeout="10s", --http-timeout="1m", --http-timeout="1h", --http-timeout="1d"` )
42+ c .Flags ().StringVar (& f .Server .Grpc .Address , "grpc-address" , f .Server .Grpc .Address , `Example: --grpc-address="0.0.0.0:9090", --grpc-address=":9090"` )
43+ c .Flags ().StringVar (& f .Server .Grpc .Network , "grpc-network" , f .Server .Grpc .Network , `Example: --grpc-network="tcp"` )
44+ c .Flags ().StringVar (& f .grpcTimeout , "grpc-timeout" , f .Server .Grpc .Timeout .AsDuration ().String (), `Example: --grpc-timeout="10s", --grpc-timeout="1m", --grpc-timeout="1h", --grpc-timeout="1d"` )
45+ c .Flags ().StringVar (& f .Jwt .Secret , "jwt-secret" , f .Jwt .Secret , `Example: --jwt-secret="xxx"` )
46+ c .Flags ().StringVar (& f .jwtExpire , "jwt-expire" , f .Jwt .Expire .AsDuration ().String (), `Example: --jwt-expire="10s", --jwt-expire="1m", --jwt-expire="1h", --jwt-expire="1d"` )
47+ c .Flags ().StringVar (& f .Jwt .Issuer , "jwt-issuer" , f .Jwt .Issuer , `Example: --jwt-issuer="xxx"` )
48+ c .Flags ().StringVar (& f .Main .Username , "main-username" , f .Main .Username , `Example: --main-username="root"` )
49+ c .Flags ().StringVar (& f .Main .Password , "main-password" , f .Main .Password , `Example: --main-password="123456"` )
50+ c .Flags ().StringVar (& f .Main .Host , "main-host" , f .Main .Host , `Example: --main-host="localhost"` )
51+ c .Flags ().Int32Var (& f .Main .Port , "main-port" , f .Main .Port , `Example: --main-port=3306"` )
52+ c .Flags ().StringVar (& f .Main .Database , "main-database" , f .Main .Database , `Example: --main-database="rabbit"` )
53+ c .Flags ().StringVar (& f .Main .Debug , "main-debug" , f .Main .Debug , `Example: --main-debug="false"` )
54+ c .Flags ().StringVar (& f .Main .UseSystemLogger , "main-use-system-logger" , f .Main .UseSystemLogger , `Example: --main-use-system-logger="true"` )
55+ c .Flags ().Int32Var (& f .EventBus .WorkerCount , "event-bus-worker-count" , f .EventBus .WorkerCount , `Example: --event-bus-worker-count=1"` )
56+ c .Flags ().StringVar (& f .eventBusTimeout , "event-bus-timeout" , f .EventBus .Timeout .AsDuration ().String (), `Example: --event-bus-timeout="10s", --event-bus-timeout="1m", --event-bus-timeout="1h", --event-bus-timeout="1d"` )
57+ c .Flags ().StringVar (& f .registryType , "registry-type" , f .RegistryType .String (), `Example: --registry-type="etcd"` )
58+ c .Flags ().StringVar (& f .Etcd .Endpoints , "etcd-endpoints" , f .Etcd .Endpoints , `Example: --etcd-endpoints="127.0.0.1:2379"` )
59+ c .Flags ().StringVar (& f .Etcd .Username , "etcd-username" , f .Etcd .Username , `Example: --etcd-username="root"` )
60+ c .Flags ().StringVar (& f .Etcd .Password , "etcd-password" , f .Etcd .Password , `Example: --etcd-password="123456"` )
61+ c .Flags ().StringVar (& f .Kubernetes .Namespace , "kubernetes-namespace" , f .Kubernetes .Namespace , `Example: --kubernetes-namespace="moon"` )
62+ c .Flags ().StringVar (& f .Kubernetes .KubeConfig , "kubernetes-kubeconfig" , f .Kubernetes .KubeConfig , `Example: --kubernetes-kubeconfig="~/.kube/config"` )
63+ c .Flags ().StringVar (& f .SwaggerBasicAuth .Username , "swagger-basic-auth-username" , f .SwaggerBasicAuth .Username , `Example: --swagger-basic-auth-username="root"` )
64+ c .Flags ().StringVar (& f .SwaggerBasicAuth .Password , "swagger-basic-auth-password" , f .SwaggerBasicAuth .Password , `Example: --swagger-basic-auth-password="123456"` )
65+ c .Flags ().StringVar (& f .MetricsBasicAuth .Username , "metrics-basic-auth-username" , f .MetricsBasicAuth .Username , `Example: --metrics-basic-auth-username="root"` )
66+ c .Flags ().StringVar (& f .MetricsBasicAuth .Password , "metrics-basic-auth-password" , f .MetricsBasicAuth .Password , `Example: --metrics-basic-auth-password="123456"` )
67+ c .Flags ().StringVar (& f .EnableClientConfig , "enable-client-config" , f .EnableClientConfig , `Example: --enable-client-config="true"` )
68+ c .Flags ().StringVar (& f .EnableSwagger , "enable-swagger" , f .EnableSwagger , `Example: --enable-swagger="true"` )
69+ c .Flags ().StringVar (& f .EnableMetrics , "enable-metrics" , f .EnableMetrics , `Example: --enable-metrics="true"` )
70+ c .Flags ().StringVar (& f .UseDatabase , "use-database" , f .UseDatabase , `Example: --use-database="true"` )
71+ c .Flags ().StringSliceVar (& f .ConfigPaths , "config-paths" , f .ConfigPaths , `Example: --config-paths="./datasource"` )
4372}
4473
45- func (f * Flags ) applyToBootstrap (bc * conf.Bootstrap ) {
46- if pointer .IsNil (bc .GetServer ()) {
47- bc .Server = & conf.Server {
48- Http : & conf.Server_HTTPServer {},
49- Grpc : & conf.Server_GRPCServer {},
50- }
51- }
52- httpConf := bc .GetServer ().GetHttp ()
53- if pointer .IsNil (httpConf ) {
54- httpConf = & conf.Server_HTTPServer {}
55- bc .Server .Http = httpConf
56- }
57- if strutil .IsEmpty (httpConf .Address ) {
58- httpConf .Address = f .httpAddress
74+ func (f * Flags ) applyToBootstrap () {
75+ metadata := f .Server .Metadata
76+ if pointer .IsNil (metadata ) {
77+ metadata = make (map [string ]string )
5978 }
60- if strutil . IsEmpty ( httpConf . Network ) {
61- httpConf . Network = f .httpNetwork
62- }
63- if httpConf . GetTimeout (). AsDuration () <= 0 {
64- timeout , err := time . ParseDuration (f .httpTimeout )
65- if pointer .IsNil (err ) {
66- httpConf .Timeout = durationpb .New (timeout )
79+ metadata [ "repository" ] = f . Repo
80+ metadata [ "author" ] = f .Author
81+ metadata [ "email" ] = f . Email
82+ f . Server . Metadata = metadata
83+ if strutil . IsNotEmpty (f .httpTimeout ) {
84+ if timeout , err := time . ParseDuration ( f . httpTimeout ); pointer .IsNil (err ) {
85+ f . Server . Http .Timeout = durationpb .New (timeout )
6786 }
6887 }
69- grpcConf := bc .GetServer ().GetGrpc ()
70- if pointer .IsNil (grpcConf ) {
71- grpcConf = & conf.Server_GRPCServer {}
72- bc .Server .Grpc = grpcConf
73- }
74- if strutil .IsEmpty (grpcConf .Address ) {
75- grpcConf .Address = f .grpcAddress
88+ if strutil .IsNotEmpty (f .grpcTimeout ) {
89+ if timeout , err := time .ParseDuration (f .grpcTimeout ); pointer .IsNil (err ) {
90+ f .Server .Grpc .Timeout = durationpb .New (timeout )
91+ }
7692 }
77- if strutil .IsEmpty (grpcConf .Network ) {
78- grpcConf .Network = f .grpcNetwork
93+
94+ if strutil .IsNotEmpty (f .environment ) {
95+ f .Environment = enum .Environment (enum .Environment_value [f .environment ])
7996 }
80- if grpcConf .GetTimeout ().AsDuration () <= 0 {
81- timeout , err := time .ParseDuration (f .grpcTimeout )
82- if pointer .IsNil (err ) {
83- grpcConf .Timeout = durationpb .New (timeout )
97+ if strutil .IsNotEmpty (f .jwtExpire ) {
98+ if expire , err := time .ParseDuration (f .jwtExpire ); pointer .IsNil (err ) {
99+ f .Jwt .Expire = durationpb .New (expire )
84100 }
85101 }
86- if bc .Environment .IsUnknown () {
87- env := enum .Environment_PROD
88- if strutil .IsNotEmpty (f .environment ) {
89- env = enum .Environment (enum .Environment_value [f .environment ])
102+ if strutil .IsNotEmpty (f .eventBusTimeout ) {
103+ if timeout , err := time .ParseDuration (f .eventBusTimeout ); pointer .IsNil (err ) {
104+ f .EventBus .Timeout = durationpb .New (timeout )
90105 }
91- bc .Environment = env
106+ }
107+
108+ if strutil .IsNotEmpty (f .registryType ) {
109+ f .RegistryType = config .RegistryType (config .RegistryType_value [f .registryType ])
92110 }
93111}
0 commit comments