Skip to content

Commit

Permalink
chore: Fix linter findings for revive:exported in plugins/inputs/f*
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-pawel committed Oct 20, 2024
1 parent e257c14 commit 55ae4fb
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 188 deletions.
33 changes: 16 additions & 17 deletions plugins/inputs/fail2ban/fail2ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@ import (
var sampleConfig string

var (
execCommand = exec.Command // execCommand is used to mock commands in tests.
execCommand = exec.Command // execCommand is used to mock commands in tests.
metricsTargets = []struct {
target string
field string
}{
{
target: "Currently failed:",
field: "failed",
},
{
target: "Currently banned:",
field: "banned",
},
}
)

const cmd = "fail2ban-client"

type Fail2ban struct {
UseSudo bool `toml:"use_sudo"`
Socket string `toml:"socket"`
path string
}

var metricsTargets = []struct {
target string
field string
}{
{
target: "Currently failed:",
field: "failed",
},
{
target: "Currently banned:",
field: "banned",
},
}

const cmd = "fail2ban-client"

func (*Fail2ban) SampleConfig() string {
return sampleConfig
}
Expand Down
74 changes: 36 additions & 38 deletions plugins/inputs/fibaro/fibaro.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var sampleConfig string

const defaultTimeout = 5 * time.Second

// Fibaro contains connection information
type Fibaro struct {
URL string `toml:"url"`
Username string `toml:"username"`
Expand All @@ -32,38 +31,8 @@ type Fibaro struct {
client *http.Client
}

// getJSON connects, authenticates and reads JSON payload returned by Fibaro box
func (f *Fibaro) getJSON(path string) ([]byte, error) {
var requestURL = f.URL + path

req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return nil, err
}

req.SetBasicAuth(f.Username, f.Password)
resp, err := f.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("response from url %q has status code %d (%s), expected %d (%s)",
requestURL,
resp.StatusCode,
http.StatusText(resp.StatusCode),
http.StatusOK,
http.StatusText(http.StatusOK))
return nil, err
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read response body: %w", err)
}

return bodyBytes, nil
func (*Fibaro) SampleConfig() string {
return sampleConfig
}

func (f *Fibaro) Init() error {
Expand All @@ -78,11 +47,6 @@ func (f *Fibaro) Init() error {
return nil
}

func (*Fibaro) SampleConfig() string {
return sampleConfig
}

// Gather fetches all required information to output metrics
func (f *Fibaro) Gather(acc telegraf.Accumulator) error {
if f.client == nil {
f.client = &http.Client{
Expand Down Expand Up @@ -116,6 +80,40 @@ func (f *Fibaro) Gather(acc telegraf.Accumulator) error {
return nil
}

// getJSON connects, authenticates and reads JSON payload returned by Fibaro box
func (f *Fibaro) getJSON(path string) ([]byte, error) {
var requestURL = f.URL + path

req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
return nil, err
}

req.SetBasicAuth(f.Username, f.Password)
resp, err := f.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("response from url %q has status code %d (%s), expected %d (%s)",
requestURL,
resp.StatusCode,
http.StatusText(resp.StatusCode),
http.StatusOK,
http.StatusText(http.StatusOK))
return nil, err
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read response body: %w", err)
}

return bodyBytes, nil
}

func init() {
inputs.Add("fibaro", func() telegraf.Input {
return &Fibaro{
Expand Down
5 changes: 3 additions & 2 deletions plugins/inputs/fibaro/hc2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/influxdata/telegraf"
)

func Parse(acc telegraf.Accumulator, sectionBytes, roomBytes, deviecsBytes []byte) error {
// Parse parses data from sections, rooms and devices, and adds measurements containing parsed data.
func Parse(acc telegraf.Accumulator, sectionBytes, roomBytes, devicesBytes []byte) error {
var tmpSections []Sections
if err := json.Unmarshal(sectionBytes, &tmpSections); err != nil {
return err
Expand All @@ -28,7 +29,7 @@ func Parse(acc telegraf.Accumulator, sectionBytes, roomBytes, deviecsBytes []byt
}

var devices []Devices
if err := json.Unmarshal(deviecsBytes, &devices); err != nil {
if err := json.Unmarshal(devicesBytes, &devices); err != nil {
return err
}

Expand Down
5 changes: 3 additions & 2 deletions plugins/inputs/fibaro/hc3/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/influxdata/telegraf/internal"
)

func Parse(acc telegraf.Accumulator, sectionBytes, roomBytes, deviecsBytes []byte) error {
// Parse parses data from sections, rooms and devices, and adds measurements containing parsed data.
func Parse(acc telegraf.Accumulator, sectionBytes, roomBytes, devicesBytes []byte) error {
var tmpSections []Sections
if err := json.Unmarshal(sectionBytes, &tmpSections); err != nil {
return err
Expand All @@ -29,7 +30,7 @@ func Parse(acc telegraf.Accumulator, sectionBytes, roomBytes, deviecsBytes []byt
}

var devices []Devices
if err := json.Unmarshal(deviecsBytes, &devices); err != nil {
if err := json.Unmarshal(devicesBytes, &devices); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions plugins/inputs/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (f *File) Init() error {
return err
}

func (f *File) SetParserFunc(fn telegraf.ParserFunc) {
f.parserFunc = fn
}

func (f *File) Gather(acc telegraf.Accumulator) error {
err := f.refreshFilePaths()
if err != nil {
Expand All @@ -71,10 +75,6 @@ func (f *File) Gather(acc telegraf.Accumulator) error {
return nil
}

func (f *File) SetParserFunc(fn telegraf.ParserFunc) {
f.parserFunc = fn
}

func (f *File) refreshFilePaths() error {
var allFiles []string
for _, file := range f.Files {
Expand Down
71 changes: 36 additions & 35 deletions plugins/inputs/filecount/filecount.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,41 @@ import (
var sampleConfig string

type FileCount struct {
Directory string `toml:"directory" deprecated:"1.9.0;1.35.0;use 'directories' instead"`
Directories []string
Name string
Recursive bool
RegularOnly bool
FollowSymlinks bool
Size config.Size
Directory string `toml:"directory" deprecated:"1.9.0;1.35.0;use 'directories' instead"`
Directories []string `toml:"directories"`
Name string `toml:"name"`
Recursive bool `toml:"recursive"`
RegularOnly bool `toml:"regular_only"`
FollowSymlinks bool `toml:"follow_symlinks"`
Size config.Size `toml:"size"`
MTime config.Duration `toml:"mtime"`
fileFilters []fileFilterFunc
globPaths []globpath.GlobPath
Fs fileSystem
Log telegraf.Logger
Log telegraf.Logger `toml:"-"`

fs fileSystem
fileFilters []fileFilterFunc
globPaths []globpath.GlobPath
}

type fileFilterFunc func(os.FileInfo) (bool, error)

func (*FileCount) SampleConfig() string {
return sampleConfig
}

func (fc *FileCount) Gather(acc telegraf.Accumulator) error {
if fc.globPaths == nil {
fc.initGlobPaths(acc)
}

for _, glob := range fc.globPaths {
for _, dir := range fc.onlyDirectories(glob.GetRoots()) {
fc.count(acc, dir, glob)
}
}

return nil
}

func rejectNilFilters(filters []fileFilterFunc) []fileFilterFunc {
filtered := make([]fileFilterFunc, 0, len(filters))
for _, f := range filters {
Expand Down Expand Up @@ -226,29 +245,11 @@ func (fc *FileCount) filter(file os.FileInfo) (bool, error) {
return true, nil
}

func (*FileCount) SampleConfig() string {
return sampleConfig
}

func (fc *FileCount) Gather(acc telegraf.Accumulator) error {
if fc.globPaths == nil {
fc.initGlobPaths(acc)
}

for _, glob := range fc.globPaths {
for _, dir := range fc.onlyDirectories(glob.GetRoots()) {
fc.count(acc, dir, glob)
}
}

return nil
}

func (fc *FileCount) resolveLink(path string) (os.FileInfo, error) {
if fc.FollowSymlinks {
return fc.Fs.Stat(path)
return fc.fs.stat(path)
}
fi, err := fc.Fs.Lstat(path)
fi, err := fc.fs.lstat(path)
if err != nil {
return fi, err
}
Expand All @@ -262,7 +263,7 @@ func (fc *FileCount) resolveLink(path string) (os.FileInfo, error) {
func (fc *FileCount) onlyDirectories(directories []string) []string {
out := make([]string, 0)
for _, path := range directories {
info, err := fc.Fs.Stat(path)
info, err := fc.fs.stat(path)
if err == nil && info.IsDir() {
out = append(out, path)
}
Expand Down Expand Up @@ -295,7 +296,7 @@ func (fc *FileCount) initGlobPaths(acc telegraf.Accumulator) {
}
}

func NewFileCount() *FileCount {
func newFileCount() *FileCount {
return &FileCount{
Directory: "",
Directories: []string{},
Expand All @@ -306,12 +307,12 @@ func NewFileCount() *FileCount {
Size: config.Size(0),
MTime: config.Duration(0),
fileFilters: nil,
Fs: osFS{},
fs: osFS{},
}
}

func init() {
inputs.Add("filecount", func() telegraf.Input {
return NewFileCount()
return newFileCount()
})
}
7 changes: 4 additions & 3 deletions plugins/inputs/filecount/filecount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

func TestNoFilters(t *testing.T) {
Expand Down Expand Up @@ -147,7 +148,7 @@ func TestDirectoryWithTrailingSlash(t *testing.T) {
Directories: []string{getTestdataDir() + string(filepath.Separator)},
Name: "*",
Recursive: true,
Fs: getFakeFileSystem(getTestdataDir()),
fs: getFakeFileSystem(getTestdataDir()),
}

var acc testutil.Accumulator
Expand Down Expand Up @@ -184,7 +185,7 @@ func getNoFilterFileCount() FileCount {
Size: config.Size(0),
MTime: config.Duration(0),
fileFilters: nil,
Fs: getFakeFileSystem(getTestdataDir()),
fs: getFakeFileSystem(getTestdataDir()),
}
}

Expand Down
12 changes: 6 additions & 6 deletions plugins/inputs/filecount/filesystem_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
*/

type fileSystem interface {
Open(name string) (file, error)
Stat(name string) (os.FileInfo, error)
Lstat(name string) (os.FileInfo, error)
open(name string) (file, error)
stat(name string) (os.FileInfo, error)
lstat(name string) (os.FileInfo, error)
}

type file interface {
Expand All @@ -28,6 +28,6 @@ type file interface {
// osFS implements fileSystem using the local disk
type osFS struct{}

func (osFS) Open(name string) (file, error) { return os.Open(name) }
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
func (osFS) Lstat(name string) (os.FileInfo, error) { return os.Lstat(name) }
func (osFS) open(name string) (file, error) { return os.Open(name) }
func (osFS) stat(name string) (os.FileInfo, error) { return os.Stat(name) }
func (osFS) lstat(name string) (os.FileInfo, error) { return os.Lstat(name) }
Loading

0 comments on commit 55ae4fb

Please sign in to comment.