File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -326,6 +326,16 @@ func main() {
326
326
return
327
327
}
328
328
329
+ shortVersion := version
330
+ if shortVersion == "" {
331
+ shortVersion = "unknown"
332
+ }
333
+
334
+ // Configure version
335
+ if err := internal .SetVersion (shortVersion ); err != nil {
336
+ log .Println ("Telegraf version already configured to: " + internal .Version ())
337
+ }
338
+
329
339
if runtime .GOOS == "windows" && ! (* fRunAsConsole ) {
330
340
svcConfig := & service.Config {
331
341
Name : * fServiceName ,
Original file line number Diff line number Diff line change @@ -24,13 +24,32 @@ var (
24
24
TimeoutErr = errors .New ("Command timed out." )
25
25
26
26
NotImplementedError = errors .New ("not implemented yet" )
27
+
28
+ VersionAlreadySetError = errors .New ("version has already been set" )
27
29
)
28
30
31
+ // Set via the main module
32
+ var version string
33
+
29
34
// Duration just wraps time.Duration
30
35
type Duration struct {
31
36
Duration time.Duration
32
37
}
33
38
39
+ // SetVersion sets the telegraf agent version
40
+ func SetVersion (v string ) error {
41
+ if version != "" {
42
+ return VersionAlreadySetError
43
+ }
44
+ version = v
45
+ return nil
46
+ }
47
+
48
+ // Version returns the telegraf agent version
49
+ func Version () string {
50
+ return version
51
+ }
52
+
34
53
// UnmarshalTOML parses the duration from the TOML config file
35
54
func (d * Duration ) UnmarshalTOML (b []byte ) error {
36
55
var err error
Original file line number Diff line number Diff line change @@ -182,3 +182,15 @@ func TestCompressWithGzip(t *testing.T) {
182
182
183
183
assert .Equal (t , testData , string (output ))
184
184
}
185
+
186
+ func TestVersionAlreadySet (t * testing.T ) {
187
+ err := SetVersion ("foo" )
188
+ assert .Nil (t , err )
189
+
190
+ err = SetVersion ("bar" )
191
+
192
+ assert .NotNil (t , err )
193
+ assert .IsType (t , VersionAlreadySetError , err )
194
+
195
+ assert .Equal (t , "foo" , Version ())
196
+ }
You can’t perform that action at this time.
0 commit comments