File tree 6 files changed +53
-4
lines changed
cmd/chirpstack-gateway-bridge/cmd
6 files changed +53
-4
lines changed Original file line number Diff line number Diff line change @@ -418,6 +418,15 @@ marshaler="{{ .Integration.Marshaler }}"
418
418
# Max. execution duration.
419
419
max_execution_duration="{{ .MetaData.Dynamic.MaxExecutionDuration }}"
420
420
421
+ # Split delimiter.
422
+ #
423
+ # When the output of a command returns multiple lines, ChirpStack Gateway Bridge
424
+ # assumes multiple values are returned. In this case it will split by the given delimiter
425
+ # to obtain the key / value of each row. The key will be prefixed with the name of the
426
+ # configured command.
427
+ split_delimiter="{{ .MetaData.Dynamic.SplitDelimiter }}"
428
+
429
+
421
430
# Commands to execute.
422
431
#
423
432
# The value of the stdout will be used as the key value (string).
Original file line number Diff line number Diff line change @@ -67,6 +67,7 @@ func init() {
67
67
68
68
viper .SetDefault ("integration.mqtt.auth.azure_iot_hub.sas_token_expiration" , 24 * time .Hour )
69
69
70
+ viper .SetDefault ("meta_data.dynamic.split_delimiter" , "=" )
70
71
viper .SetDefault ("meta_data.dynamic.execution_interval" , time .Minute )
71
72
viper .SetDefault ("meta_data.dynamic.max_execution_duration" , time .Second )
72
73
Original file line number Diff line number Diff line change @@ -446,6 +446,15 @@ marshaler="protobuf"
446
446
# Max. execution duration.
447
447
max_execution_duration="1s"
448
448
449
+ # Split delimiter.
450
+ #
451
+ # When the output of a command returns multiple lines, ChirpStack Gateway Bridge
452
+ # assumes multiple values are returned. In this case it will split by the given delimiter
453
+ # to obtain the key / value of each row. The key will be prefixed with the name of the
454
+ # configured command.
455
+ split_delimiter="="
456
+
457
+
449
458
# Commands to execute.
450
459
#
451
460
# The value of the stdout will be used as the key value (string).
Original file line number Diff line number Diff line change @@ -110,13 +110,14 @@ type Config struct {
110
110
EndpointEnabled bool `mapstructure:"endpoint_enabled"`
111
111
Bind string `mapstructure:"bind"`
112
112
} `mapstructure:"prometheus"`
113
- } `mapstructure:"metrics"`
113
+ } `mapstructure:"metrics"`
114
114
115
115
MetaData struct {
116
116
Static map [string ]string `mapstructure:"static"`
117
117
Dynamic struct {
118
118
ExecutionInterval time.Duration `mapstructure:"execution_interval"`
119
119
MaxExecutionDuration time.Duration `mapstructure:"max_execution_duration"`
120
+ SplitDelimiter string `mapstructure:"split_delimiter"`
120
121
Commands map [string ]string `mapstructure:"commands"`
121
122
} `mapstructure:"dynamic"`
122
123
} `mapstructure:"meta_data"`
Original file line number Diff line number Diff line change 22
22
cmnds map [string ]string
23
23
cached map [string ]string
24
24
25
- interval time.Duration
26
- maxExecution time.Duration
25
+ interval time.Duration
26
+ maxExecution time.Duration
27
+ splitDelimiter string
27
28
)
28
29
29
30
// Setup configures the metadata package.
@@ -36,6 +37,7 @@ func Setup(conf config.Config) error {
36
37
37
38
interval = conf .MetaData .Dynamic .ExecutionInterval
38
39
maxExecution = conf .MetaData .Dynamic .MaxExecutionDuration
40
+ splitDelimiter = conf .MetaData .Dynamic .SplitDelimiter
39
41
40
42
go func () {
41
43
for {
@@ -71,7 +73,23 @@ func runCommands() {
71
73
continue
72
74
}
73
75
74
- newKV [k ] = out
76
+ if strings .Contains (out , "\n " ) {
77
+ rows := strings .Split (out , "\n " )
78
+ for _ , row := range rows {
79
+ kv := strings .Split (row , splitDelimiter )
80
+ if len (kv ) != 2 {
81
+ log .WithFields (log.Fields {
82
+ "row" : row ,
83
+ "split_delimiter" : splitDelimiter ,
84
+ }).Warning ("metadata: can not split output in key / value" )
85
+ } else {
86
+ newKV [k + "_" + kv [0 ]] = kv [1 ]
87
+ }
88
+ }
89
+
90
+ } else {
91
+ newKV [k ] = out
92
+ }
75
93
}
76
94
77
95
mux .Lock ()
Original file line number Diff line number Diff line change @@ -116,9 +116,20 @@ func TestMetaData(t *testing.T) {
116
116
"bar" : "test2" ,
117
117
},
118
118
},
119
+ {
120
+ Name : "command returns multiple rows" ,
121
+ Commands : map [string ]string {
122
+ "bar" : `echo -e "foo=bar\nalice=bob"` ,
123
+ },
124
+ Expected : map [string ]string {
125
+ "bar_foo" : "bar" ,
126
+ "bar_alice" : "bob" ,
127
+ },
128
+ },
119
129
}
120
130
121
131
maxExecution = time .Second
132
+ splitDelimiter = "="
122
133
123
134
for _ , tst := range tests {
124
135
t .Run (tst .Name , func (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments