github.com/vmware/transport-go@v1.3.4/plank/pkg/server/flag_helper.go (about) 1 // Copyright 2019-2021 VMware, Inc. 2 // SPDX-License-Identifier: BSD-2-Clause 3 4 package server 5 6 import ( 7 flag "github.com/spf13/pflag" 8 "github.com/spf13/viper" 9 "github.com/vmware/transport-go/plank/utils" 10 "os" 11 ) 12 13 type serverConfigFactory struct { 14 statics []string 15 flagSet *flag.FlagSet 16 flagsParsed bool 17 } 18 19 func (f *serverConfigFactory) Hostname() string { 20 return viper.GetString(utils.PlatformServerFlagConstants["Hostname"]["FlagName"]) 21 } 22 23 func (f *serverConfigFactory) Port() int { 24 return viper.GetInt(utils.PlatformServerFlagConstants["Port"]["FlagName"]) 25 } 26 27 func (f *serverConfigFactory) RootDir() string { 28 return viper.GetString(utils.PlatformServerFlagConstants["RootDir"]["FlagName"]) 29 } 30 31 func (f *serverConfigFactory) Cert() string { 32 return viper.GetString(utils.PlatformServerFlagConstants["Cert"]["FlagName"]) 33 } 34 35 func (f *serverConfigFactory) CertKey() string { 36 return viper.GetString(utils.PlatformServerFlagConstants["CertKey"]["FlagName"]) 37 } 38 39 func (f *serverConfigFactory) Static() []string { 40 return viper.GetStringSlice(utils.PlatformServerFlagConstants["Static"]["FlagName"]) 41 } 42 43 func (f *serverConfigFactory) SpaPath() string { 44 return viper.GetString(utils.PlatformServerFlagConstants["SpaPath"]["FlagName"]) 45 } 46 47 func (f *serverConfigFactory) NoFabricBroker() bool { 48 return viper.GetBool(utils.PlatformServerFlagConstants["NoFabricBroker"]["FlagName"]) 49 } 50 51 func (f *serverConfigFactory) FabricEndpoint() string { 52 return viper.GetString(utils.PlatformServerFlagConstants["FabricEndpoint"]["FlagName"]) 53 } 54 55 func (f *serverConfigFactory) TopicPrefix() string { 56 return viper.GetString(utils.PlatformServerFlagConstants["TopicPrefix"]["FlagName"]) 57 } 58 59 func (f *serverConfigFactory) QueuePrefix() string { 60 return viper.GetString(utils.PlatformServerFlagConstants["QueuePrefix"]["FlagName"]) 61 } 62 63 func (f *serverConfigFactory) RequestPrefix() string { 64 return viper.GetString(utils.PlatformServerFlagConstants["RequestPrefix"]["FlagName"]) 65 } 66 67 func (f *serverConfigFactory) RequestQueuePrefix() string { 68 return viper.GetString(utils.PlatformServerFlagConstants["RequestQueuePrefix"]["FlagName"]) 69 } 70 71 func (f *serverConfigFactory) ConfigFile() string { 72 return viper.GetString(utils.PlatformServerFlagConstants["ConfigFile"]["FlagName"]) 73 } 74 75 func (f *serverConfigFactory) ShutdownTimeout() int64 { 76 return viper.GetInt64(utils.PlatformServerFlagConstants["ShutdownTimeout"]["FlagName"]) 77 } 78 79 func (f *serverConfigFactory) OutputLog() string { 80 return viper.GetString(utils.PlatformServerFlagConstants["OutputLog"]["FlagName"]) 81 } 82 83 func (f *serverConfigFactory) AccessLog() string { 84 return viper.GetString(utils.PlatformServerFlagConstants["AccessLog"]["FlagName"]) 85 } 86 87 func (f *serverConfigFactory) ErrorLog() string { 88 return viper.GetString(utils.PlatformServerFlagConstants["ErrorLog"]["FlagName"]) 89 } 90 91 func (f *serverConfigFactory) Debug() bool { 92 return viper.GetBool(utils.PlatformServerFlagConstants["Debug"]["FlagName"]) 93 } 94 95 func (f *serverConfigFactory) NoBanner() bool { 96 return viper.GetBool(utils.PlatformServerFlagConstants["NoBanner"]["FlagName"]) 97 } 98 99 func (f *serverConfigFactory) Prometheus() bool { 100 return viper.GetBool(utils.PlatformServerFlagConstants["Prometheus"]["FlagName"]) 101 } 102 103 func (f *serverConfigFactory) RestBridgeTimeout() int64 { 104 return viper.GetInt64(utils.PlatformServerFlagConstants["RestBridgeTimeout"]["FlagName"]) 105 } 106 107 // parseFlags reads OS arguments into the FlagSet in this factory instance 108 func (f *serverConfigFactory) parseFlags(args []string) { 109 f.flagSet.Parse(args[1:]) 110 f.flagsParsed = flag.Parsed() 111 } 112 113 // configureFlags defines flags definitions as well as associate environment variables for a few 114 // flags. see configureFlagsInFlagSet() for detailed flag defining logic. 115 func (f *serverConfigFactory) configureFlags(flagset *flag.FlagSet) { 116 viper.SetEnvPrefix("PLANK_SERVER") 117 viper.BindEnv("hostname") 118 viper.BindEnv("port") 119 viper.BindEnv("rootdir") 120 f.flagSet = flagset 121 f.configureFlagsInFlagSet(f.flagSet) 122 viper.BindPFlags(f.flagSet) 123 } 124 125 // configureFlagsInFlagSet takes the pointer to an arbitrary FlagSet instance and 126 // populates it with the flag definitions necessary for PlatformServerConfig. 127 func (f *serverConfigFactory) configureFlagsInFlagSet(fs *flag.FlagSet) { 128 wd, _ := os.Getwd() 129 fs.StringP( 130 utils.PlatformServerFlagConstants["Hostname"]["FlagName"], 131 utils.PlatformServerFlagConstants["Hostname"]["ShortFlag"], 132 "localhost", 133 utils.PlatformServerFlagConstants["Hostname"]["Description"]) 134 fs.IntP( 135 utils.PlatformServerFlagConstants["Port"]["FlagName"], 136 utils.PlatformServerFlagConstants["Port"]["ShortFlag"], 137 30080, 138 utils.PlatformServerFlagConstants["Port"]["Description"]) 139 fs.StringP( 140 utils.PlatformServerFlagConstants["RootDir"]["FlagName"], 141 utils.PlatformServerFlagConstants["RootDir"]["ShortFlag"], 142 wd, 143 utils.PlatformServerFlagConstants["RootDir"]["Description"]) 144 fs.String( 145 utils.PlatformServerFlagConstants["Cert"]["FlagName"], 146 "", 147 utils.PlatformServerFlagConstants["Cert"]["Description"]) 148 fs.String( 149 utils.PlatformServerFlagConstants["CertKey"]["FlagName"], 150 "", 151 utils.PlatformServerFlagConstants["CertKey"]["Description"]) 152 153 fs.StringSliceVarP( 154 &f.statics, 155 utils.PlatformServerFlagConstants["Static"]["FlagName"], 156 utils.PlatformServerFlagConstants["Static"]["ShortFlag"], 157 []string{}, 158 utils.PlatformServerFlagConstants["Static"]["Description"]) 159 fs.String( 160 utils.PlatformServerFlagConstants["SpaPath"]["FlagName"], 161 "", 162 utils.PlatformServerFlagConstants["SpaPath"]["Description"]) 163 fs.Bool( 164 utils.PlatformServerFlagConstants["NoFabricBroker"]["FlagName"], 165 false, 166 utils.PlatformServerFlagConstants["NoFabricBroker"]["Description"]) 167 fs.String( 168 utils.PlatformServerFlagConstants["FabricEndpoint"]["FlagName"], 169 "/ws", 170 utils.PlatformServerFlagConstants["FabricEndpoint"]["Description"]) 171 fs.String( 172 utils.PlatformServerFlagConstants["TopicPrefix"]["FlagName"], 173 "/topic", 174 utils.PlatformServerFlagConstants["TopicPrefix"]["Description"]) 175 fs.String( 176 utils.PlatformServerFlagConstants["QueuePrefix"]["FlagName"], 177 "/queue", 178 utils.PlatformServerFlagConstants["QueuePrefix"]["Description"]) 179 fs.String( 180 utils.PlatformServerFlagConstants["RequestPrefix"]["FlagName"], 181 "/pub", 182 utils.PlatformServerFlagConstants["RequestPrefix"]["Description"]) 183 fs.String( 184 utils.PlatformServerFlagConstants["RequestQueuePrefix"]["FlagName"], 185 "/pub/queue", 186 utils.PlatformServerFlagConstants["RequestQueuePrefix"]["Description"]) 187 fs.String( 188 utils.PlatformServerFlagConstants["ConfigFile"]["FlagName"], 189 "", 190 utils.PlatformServerFlagConstants["ConfigFile"]["Description"]) 191 fs.Int64( 192 utils.PlatformServerFlagConstants["ShutdownTimeout"]["FlagName"], 193 5, 194 utils.PlatformServerFlagConstants["ShutdownTimeout"]["Description"]) 195 fs.StringP( 196 utils.PlatformServerFlagConstants["OutputLog"]["FlagName"], 197 utils.PlatformServerFlagConstants["OutputLog"]["ShortFlag"], 198 "stdout", 199 utils.PlatformServerFlagConstants["OutputLog"]["Description"]) 200 fs.StringP( 201 utils.PlatformServerFlagConstants["AccessLog"]["FlagName"], 202 utils.PlatformServerFlagConstants["AccessLog"]["ShortFlag"], 203 "stdout", 204 utils.PlatformServerFlagConstants["AccessLog"]["Description"]) 205 fs.StringP( 206 utils.PlatformServerFlagConstants["ErrorLog"]["FlagName"], 207 utils.PlatformServerFlagConstants["ErrorLog"]["ShortFlag"], 208 "stderr", 209 utils.PlatformServerFlagConstants["ErrorLog"]["Description"]) 210 fs.BoolP( 211 utils.PlatformServerFlagConstants["Debug"]["FlagName"], 212 utils.PlatformServerFlagConstants["Debug"]["ShortFlag"], 213 false, 214 utils.PlatformServerFlagConstants["Debug"]["Description"]) 215 fs.BoolP( 216 utils.PlatformServerFlagConstants["NoBanner"]["FlagName"], 217 utils.PlatformServerFlagConstants["NoBanner"]["ShortFlag"], 218 false, 219 utils.PlatformServerFlagConstants["NoBanner"]["Description"]) 220 fs.Bool( 221 utils.PlatformServerFlagConstants["Prometheus"]["FlagName"], 222 false, 223 utils.PlatformServerFlagConstants["Prometheus"]["Description"]) 224 fs.Int64( 225 utils.PlatformServerFlagConstants["RestBridgeTimeout"]["FlagName"], 226 1, 227 utils.PlatformServerFlagConstants["RestBridgeTimeout"]["Description"]) 228 }