@@ -3,7 +3,7 @@ package command
3
3
import (
4
4
"flag"
5
5
"os"
6
- "strings "
6
+ "strconv "
7
7
8
8
"github.com/hashicorp/vault/internalshared/configutil"
9
9
"github.com/posener/complete"
@@ -15,20 +15,18 @@ type logFlags struct {
15
15
flagLogLevel string
16
16
flagLogFormat string
17
17
flagLogFile string
18
- flagLogRotateBytes string
18
+ flagLogRotateBytes int
19
19
flagLogRotateDuration string
20
- flagLogRotateMaxFiles string
20
+ flagLogRotateMaxFiles int
21
21
}
22
22
23
- type provider = func (key string ) (string , bool )
24
-
25
23
// valuesProvider has the intention of providing a way to supply a func with a
26
24
// way to retrieve values for flags and environment variables without having to
27
- // directly call a specific implementation. The reasoning for its existence is
28
- // to facilitate testing.
25
+ // directly call a specific implementation.
26
+ // The reasoning for its existence is to facilitate testing.
29
27
type valuesProvider struct {
30
- flagProvider provider
31
- envVarProvider provider
28
+ flagProvider func ( string ) (flag. Value , bool )
29
+ envVarProvider func ( string ) ( string , bool )
32
30
}
33
31
34
32
// addLogFlags will add the set of 'log' related flags to a flag set.
@@ -65,7 +63,7 @@ func (f *FlagSet) addLogFlags(l *logFlags) {
65
63
Usage : "Path to the log file that Vault should use for logging" ,
66
64
})
67
65
68
- f .StringVar ( & StringVar {
66
+ f .IntVar ( & IntVar {
69
67
Name : flagNameLogRotateBytes ,
70
68
Target : & l .flagLogRotateBytes ,
71
69
Usage : "Number of bytes that should be written to a log before it needs to be rotated. " +
@@ -79,23 +77,34 @@ func (f *FlagSet) addLogFlags(l *logFlags) {
79
77
"Must be a duration value such as 30s" ,
80
78
})
81
79
82
- f .StringVar ( & StringVar {
80
+ f .IntVar ( & IntVar {
83
81
Name : flagNameLogRotateMaxFiles ,
84
82
Target : & l .flagLogRotateMaxFiles ,
85
83
Usage : "The maximum number of older log file archives to keep" ,
86
84
})
87
85
}
88
86
89
- // getValue will attempt to find the flag with the corresponding flag name (key)
90
- // and return the value along with a bool representing whether of not the flag had been found/set.
91
- func (f * FlagSets ) getValue (flagName string ) (string , bool ) {
92
- var result string
87
+ // envVarValue attempts to get a named value from the environment variables.
88
+ // The value will be returned as a string along with a boolean value indiciating
89
+ // to the caller whether the named env var existed.
90
+ func envVarValue (key string ) (string , bool ) {
91
+ if key == "" {
92
+ return "" , false
93
+ }
94
+ return os .LookupEnv (key )
95
+ }
96
+
97
+ // flagValue attempts to find the named flag in a set of FlagSets.
98
+ // The flag.Value is returned if it was specified, and the boolean value indicates
99
+ // to the caller if the flag was specified by the end user.
100
+ func (f * FlagSets ) flagValue (flagName string ) (flag.Value , bool ) {
101
+ var result flag.Value
93
102
var isFlagSpecified bool
94
103
95
104
if f != nil {
96
105
f .Visit (func (fl * flag.Flag ) {
97
106
if fl .Name == flagName {
98
- result = fl .Value . String ()
107
+ result = fl .Value
99
108
isFlagSpecified = true
100
109
}
101
110
})
@@ -104,51 +113,63 @@ func (f *FlagSets) getValue(flagName string) (string, bool) {
104
113
return result , isFlagSpecified
105
114
}
106
115
107
- // getAggregatedConfigValue uses the provided keys to check CLI flags and environment
116
+ // overrideValue uses the provided keys to check CLI flags and environment
108
117
// variables for values that may be used to override any specified configuration.
109
- // If nothing can be found in flags/env vars or config, the 'fallback' (default) value will be provided.
110
- func (p * valuesProvider ) getAggregatedConfigValue (flagKey , envVarKey , current , fallback string ) string {
118
+ func (p * valuesProvider ) overrideValue (flagKey , envVarKey string ) (string , bool ) {
111
119
var result string
112
- current = strings . TrimSpace ( current )
120
+ found := true
113
121
114
122
flg , flgFound := p .flagProvider (flagKey )
115
123
env , envFound := p .envVarProvider (envVarKey )
116
124
117
125
switch {
118
126
case flgFound :
119
- result = flg
127
+ result = flg . String ()
120
128
case envFound :
121
- // Use value from env var
122
129
result = env
123
- case current != "" :
124
- // Use value from config
125
- result = current
126
130
default :
127
- // Use the default value
128
- result = fallback
131
+ found = false
129
132
}
130
133
131
- return result
134
+ return result , found
132
135
}
133
136
134
- // updateLogConfig will accept a shared config and specifically attempt to update the 'log' related config keys.
135
- // For each 'log' key we aggregate file config/ env vars and CLI flags to select the one with the highest precedence.
137
+ // applyLogConfigOverrides will accept a shared config and specifically attempt to update the 'log' related config keys.
138
+ // For each 'log' key, we aggregate file config, env vars and CLI flags to select the one with the highest precedence.
136
139
// This method mutates the config object passed into it.
137
- func (f * FlagSets ) updateLogConfig (config * configutil.SharedConfig ) {
140
+ func (f * FlagSets ) applyLogConfigOverrides (config * configutil.SharedConfig ) {
138
141
p := & valuesProvider {
139
- flagProvider : func ( key string ) ( string , bool ) { return f . getValue ( key ) } ,
140
- envVarProvider : func ( key string ) ( string , bool ) {
141
- if key == "" {
142
- return "" , false
143
- }
144
- return os . LookupEnv ( key )
145
- },
142
+ flagProvider : f . flagValue ,
143
+ envVarProvider : envVarValue ,
144
+ }
145
+
146
+ // Update log level
147
+ if val , found := p . overrideValue ( flagNameLogLevel , EnvVaultLogLevel ); found {
148
+ config . LogLevel = val
146
149
}
147
150
148
- config .LogLevel = p .getAggregatedConfigValue (flagNameLogLevel , EnvVaultLogLevel , config .LogLevel , "info" )
149
- config .LogFormat = p .getAggregatedConfigValue (flagNameLogFormat , EnvVaultLogFormat , config .LogFormat , "" )
150
- config .LogFile = p .getAggregatedConfigValue (flagNameLogFile , "" , config .LogFile , "" )
151
- config .LogRotateDuration = p .getAggregatedConfigValue (flagNameLogRotateDuration , "" , config .LogRotateDuration , "" )
152
- config .LogRotateBytes = p .getAggregatedConfigValue (flagNameLogRotateBytes , "" , config .LogRotateBytes , "" )
153
- config .LogRotateMaxFiles = p .getAggregatedConfigValue (flagNameLogRotateMaxFiles , "" , config .LogRotateMaxFiles , "" )
151
+ // Update log format
152
+ if val , found := p .overrideValue (flagNameLogFormat , EnvVaultLogFormat ); found {
153
+ config .LogFormat = val
154
+ }
155
+
156
+ // Update log file name
157
+ if val , found := p .overrideValue (flagNameLogFile , "" ); found {
158
+ config .LogFile = val
159
+ }
160
+
161
+ // Update log rotation duration
162
+ if val , found := p .overrideValue (flagNameLogRotateDuration , "" ); found {
163
+ config .LogRotateDuration = val
164
+ }
165
+
166
+ // Update log max files
167
+ if val , found := p .overrideValue (flagNameLogRotateMaxFiles , "" ); found {
168
+ config .LogRotateMaxFiles , _ = strconv .Atoi (val )
169
+ }
170
+
171
+ // Update log rotation max bytes
172
+ if val , found := p .overrideValue (flagNameLogRotateBytes , "" ); found {
173
+ config .LogRotateBytes , _ = strconv .Atoi (val )
174
+ }
154
175
}
0 commit comments