github.com/msales/pkg/v3@v3.24.0/clix/flags.go (about) 1 package clix 2 3 import ( 4 "time" 5 6 "gopkg.in/urfave/cli.v1" 7 ) 8 9 // Flag constants declared for CLI use. 10 const ( 11 FlagPort = "port" 12 13 FlagLogFormat = "log-format" 14 FlagLogLevel = "log-level" 15 FlagLogTags = "log-tags" 16 17 FlagStatsDSN = "stats-dsn" 18 FlagStatsPrefix = "stats-prefix" 19 FlagStatsTags = "stats-tags" 20 21 FlagProfiler = "profiler" 22 FlagProfilerPort = "profiler-port" 23 24 FlagKafkaConsumerBrokers = "kafka-consumer-brokers" 25 FlagKafkaConsumerGroupID = "kafka-consumer-group-id" 26 FlagKafkaConsumerTopic = "kafka-consumer-topic" 27 FlagKafkaConsumerKafkaVersion = "kafka-consumer-kafka-version" 28 FlagKafkaProducerBrokers = "kafka-producer-brokers" 29 FlagKafkaProducerTopic = "kafka-producer-topic" 30 FlagKafkaProducerKafkaVersion = "kafka-producer-kafka-version" 31 32 FlagCommitBatch = "commit-batch" 33 FlagCommitInterval = "commit-interval" 34 35 FlagRedisDSN = "redis-dsn" 36 ) 37 38 type defaults struct { 39 Port string 40 LogFormat string 41 LogLevel string 42 43 ProfilerPort string 44 } 45 46 // Defaults holds the flag default values. 47 var Defaults = defaults{ 48 Port: "80", 49 LogFormat: "json", 50 LogLevel: "info", 51 52 ProfilerPort: "8081", 53 } 54 55 // Flags represents a set of CLI flags. 56 type Flags []cli.Flag 57 58 // Merge joins one or more Flags together, making a new set. 59 func (f Flags) Merge(flags ...Flags) Flags { 60 var m Flags 61 m = append(m, f...) 62 for _, flag := range flags { 63 m = append(m, flag...) 64 } 65 66 return m 67 } 68 69 // ServerFlags are flags that configure a server. 70 var ServerFlags = Flags{ 71 cli.StringFlag{ 72 Name: FlagPort, 73 Value: Defaults.Port, 74 Usage: "Port for HTTP server to listen on", 75 EnvVar: "PORT", 76 }, 77 } 78 79 // KafkaConsumerFlags are flags that configure a Kafka consumer. 80 var KafkaConsumerFlags = Flags{ 81 cli.StringSliceFlag{ 82 Name: FlagKafkaConsumerBrokers, 83 Usage: "Kafka consumer brokers.", 84 EnvVar: "KAFKA_CONSUMER_BROKERS", 85 }, 86 cli.StringFlag{ 87 Name: FlagKafkaConsumerGroupID, 88 Usage: "Kafka consumer group id.", 89 EnvVar: "KAFKA_CONSUMER_GROUP_ID", 90 }, 91 cli.StringFlag{ 92 Name: FlagKafkaConsumerTopic, 93 Usage: "Kafka topic to consume from.", 94 EnvVar: "KAFKA_CONSUMER_TOPIC", 95 }, 96 cli.StringFlag{ 97 Name: FlagKafkaConsumerKafkaVersion, 98 Usage: "Kafka version (e.g. 0.10.2.0 or 2.3.0).", 99 EnvVar: "KAFKA_CONSUMER_KAFKA_VERSION", 100 }, 101 } 102 103 // KafkaProducerFlags are flags that configure a Kafka producer. 104 var KafkaProducerFlags = Flags{ 105 cli.StringSliceFlag{ 106 Name: FlagKafkaProducerBrokers, 107 Usage: "Kafka producer brokers.", 108 EnvVar: "KAFKA_PRODUCER_BROKERS", 109 }, 110 cli.StringFlag{ 111 Name: FlagKafkaProducerTopic, 112 Usage: "Kafka topic to produce into.", 113 EnvVar: "KAFKA_PRODUCER_TOPIC", 114 }, 115 cli.StringFlag{ 116 Name: FlagKafkaProducerKafkaVersion, 117 Usage: "Kafka version (e.g. 0.10.2.0 or 2.3.0).", 118 EnvVar: "KAFKA_PRODUCER_KAFKA_VERSION", 119 }, 120 } 121 122 // CommitterFlags are flags that configure message processing batch size and committing interval. 123 var CommitterFlags = Flags{ 124 cli.IntFlag{ 125 Name: FlagCommitBatch, 126 Value: 500, 127 Usage: "Commit batch size for message processing.", 128 EnvVar: "COMMIT_BATCH", 129 }, 130 cli.DurationFlag{ 131 Name: FlagCommitInterval, 132 Value: 1 * time.Second, 133 Usage: "Commit interval for message processing.", 134 EnvVar: "COMMIT_INTERVAL", 135 }, 136 } 137 138 // RedisFlags are flags that configure redis. 139 var RedisFlags = Flags{ 140 cli.StringFlag{ 141 Name: FlagRedisDSN, 142 Usage: "The DSN of Redis.", 143 EnvVar: "REDIS_DSN", 144 }, 145 } 146 147 // CommonFlags are flags that configure logging and stats. 148 var CommonFlags = Flags{ 149 cli.StringFlag{ 150 Name: FlagLogFormat, 151 Value: Defaults.LogFormat, 152 Usage: "Specify the format of logs. Supported formats: 'terminal', 'json'", 153 EnvVar: "LOG_FORMAT", 154 }, 155 cli.StringFlag{ 156 Name: FlagLogLevel, 157 Value: Defaults.LogLevel, 158 Usage: "Specify the log level. E.g. 'debug', 'warning'.", 159 EnvVar: "LOG_LEVEL", 160 }, 161 cli.StringSliceFlag{ 162 Name: FlagLogTags, 163 Usage: "A list of tags appended to every log. Format: key=value.", 164 EnvVar: "LOG_TAGS", 165 }, 166 167 cli.StringFlag{ 168 Name: FlagStatsDSN, 169 Usage: "The URL of a stats backend.", 170 EnvVar: "STATS_DSN", 171 }, 172 cli.StringFlag{ 173 Name: FlagStatsPrefix, 174 Usage: "The prefix of the measurements names.", 175 EnvVar: "STATS_PREFIX", 176 }, 177 cli.StringSliceFlag{ 178 Name: FlagStatsTags, 179 Usage: "A list of tags appended to every measurement. Format: key=value.", 180 EnvVar: "STATS_TAGS", 181 }, 182 } 183 184 // ProfilerFlags are flags that configure to the profiler. 185 var ProfilerFlags = Flags{ 186 cli.BoolFlag{ 187 Name: FlagProfiler, 188 Usage: "Enable profiler server.", 189 EnvVar: "PROFILER", 190 }, 191 cli.StringFlag{ 192 Name: FlagProfilerPort, 193 Value: Defaults.ProfilerPort, 194 Usage: "Port for the profiler to listen on.", 195 EnvVar: "PROFILER_PORT", 196 }, 197 }