Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #3911: Expose telegraf agent version in internal module #4828

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ func main() {
return
}

// Configure version
if err := internal.SetVersion(formatFullVersion()); err != nil {
log.Println("Telegraf version already configured to: " + internal.Version())
}

if runtime.GOOS == "windows" && !(*fRunAsConsole) {
svcConfig := &service.Config{
Name: *fServiceName,
Expand Down
19 changes: 19 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,32 @@ var (
TimeoutErr = errors.New("Command timed out.")

NotImplementedError = errors.New("not implemented yet")

VersionAlreadySetError = errors.New("version has already been set")
)

// Set via the main module
var version string

// Duration just wraps time.Duration
type Duration struct {
Duration time.Duration
}

// SetVersion sets the telegraf agent version
func SetVersion(v string) error {
if version != "" {
return VersionAlreadySetError
}
version = v
return nil
}

// Version returns the telegraf agent version
func Version() string {
return version
}

// UnmarshalTOML parses the duration from the TOML config file
func (d *Duration) UnmarshalTOML(b []byte) error {
var err error
Expand Down
12 changes: 12 additions & 0 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,15 @@ func TestCompressWithGzip(t *testing.T) {

assert.Equal(t, testData, string(output))
}

func TestVersionAlreadySet(t *testing.T) {
err := SetVersion("foo")
assert.Nil(t, err)

err = SetVersion("bar")

assert.NotNil(t, err)
assert.IsType(t, VersionAlreadySetError, err)

assert.Equal(t, "foo", Version())
}