Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Chlebek <[email protected]>
  • Loading branch information
echlebek committed Dec 7, 2023
1 parent 9d5dd38 commit 72f4348
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
3 changes: 1 addition & 2 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"context"
"io"
"io/ioutil"
"sync"
"sync/atomic"
)
Expand Down Expand Up @@ -45,7 +44,7 @@ func (d *DiscardWriter) ReadFrom(r io.Reader) (int64, error) {
}

func NewDiscardWriter() *DiscardWriter {
discard := ioutil.Discard.(discardInterface)
discard := io.Discard.(discardInterface)
return &DiscardWriter{discardWriter: discard}
}

Expand Down
3 changes: 1 addition & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"

corev2 "github.com/sensu/core/v2"
Expand All @@ -27,7 +26,7 @@ func sendEvent(path string, outputEvent *corev2.Event) error {
}()

if status := resp.StatusCode; status >= 400 {
b, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
b, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
return fmt.Errorf("error writing event: status %d: %s", status, string(b))
}

Expand Down
19 changes: 9 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ type Config struct {
}

var (
defaultNameTemplate = "{{ .Check.Name }}-alert"
plugin = Config{
plugin = Config{
PluginConfig: sensu.PluginConfig{
Name: "sensu-check-log",
Short: "Check Log",
Expand Down Expand Up @@ -300,7 +299,7 @@ func checkArgs(event *corev2.Event) (int, error) {
return sensu.CheckStateCritical, fmt.Errorf("--disable-event-generation not selected but event missing from stdin")
}
if plugin.LogFileExpr == "" && plugin.LogFile == "" {
return sensu.CheckStateCritical, fmt.Errorf("At least one of --log-file or --log-file-expr must be specified")
return sensu.CheckStateCritical, fmt.Errorf("at least one of --log-file or --log-file-expr must be specified")
}
if plugin.LogFileExpr != "" && plugin.LogPath == "" {
return sensu.CheckStateCritical, fmt.Errorf("--log-path must be specified if --log-file-expr is used")
Expand All @@ -314,11 +313,11 @@ func checkArgs(event *corev2.Event) (int, error) {
if _, err := os.Stat(plugin.StateDir); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(plugin.StateDir, os.ModePerm)
if err != nil {
return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created.", plugin.StateDir)
return sensu.CheckStateCritical, fmt.Errorf("selected --state-directory %s does not exist and cannot be created", plugin.StateDir)
}
}
if _, err := os.Stat(plugin.StateDir); err != nil {
return sensu.CheckStateCritical, fmt.Errorf("Unexpected error accessing --state-directory %s: %s", plugin.StateDir, err)
return sensu.CheckStateCritical, fmt.Errorf("unexpected error accessing --state-directory %s: %s", plugin.StateDir, err)
}
if plugin.DryRun {
plugin.Verbose = true
Expand Down Expand Up @@ -379,7 +378,7 @@ func buildLogArray() ([]string, error) {
if filepath.IsAbs(absPath) {
logs = append(logs, absPath)
} else {
return nil, fmt.Errorf("Path %s not absolute", absPath)
return nil, fmt.Errorf("path %s not absolute", absPath)
}

}
Expand All @@ -401,7 +400,7 @@ func buildLogArray() ([]string, error) {
logs = append(logs, path)
}
} else {
return fmt.Errorf("Path %s not absolute", path)
return fmt.Errorf("path %s not absolute", path)
}
}
return nil
Expand Down Expand Up @@ -457,10 +456,10 @@ func processLogFile(file string, enc *json.Encoder) (int, error) {
if plugin.EnableStateReset {
state = State{}
if plugin.Verbose {
fmt.Printf("Info: resetting state file %s because unexpected cached matching condition detected and --reset-state in use\n", file)
fmt.Printf("info: resetting state file %s because unexpected cached matching condition detected and --reset-state in use\n", file)
}
} else {
return 0, fmt.Errorf("Error: state file for %s has unexpected cached matching condition:: Expr: '%s'. Either use --reset-state option, or manually delete state file '%s'", file, state.MatchExpr, stateFile)
return 0, fmt.Errorf("error: state file for %s has unexpected cached matching condition:: Expr: '%s'. Either use --reset-state option, or manually delete state file '%s'", file, state.MatchExpr, stateFile)
}
}

Expand Down Expand Up @@ -545,7 +544,7 @@ func processLogFile(file string, enc *json.Encoder) (int, error) {
}

if err := setState(state, stateFile); err != nil {
return 0, fmt.Errorf("Error setting state: %s", err)
return 0, fmt.Errorf("error setting state: %s", err)
}
return numResults, nil
}
Expand Down
59 changes: 29 additions & 30 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -213,7 +212,7 @@ func TestCheckArgs(t *testing.T) {
status, err = checkArgs(event)
assert.NoError(t, err)
assert.Equal(t, 0, status)
td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(td)
plugin.StateDir = td
Expand All @@ -237,7 +236,7 @@ func TestCheckArgs(t *testing.T) {
}

func TestState(t *testing.T) {
td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
defer os.RemoveAll(td)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -333,7 +332,7 @@ func TestExecuteCheckWithDisableEvent(t *testing.T) {
plugin.MatchExpr = "test"
plugin.WarningThreshold = 1
plugin.WarningOnly = true
td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -342,7 +341,7 @@ func TestExecuteCheckWithDisableEvent(t *testing.T) {
assert.Equal(t, 1, status)
plugin.CriticalThreshold = 1
plugin.CriticalOnly = true
ctd, err := ioutil.TempDir("", "")
ctd, err := os.MkdirTemp("", "")
defer os.RemoveAll(ctd)
assert.NoError(t, err)
plugin.StateDir = ctd
Expand All @@ -356,7 +355,7 @@ func TestExecuteCheckWithNoEvent(t *testing.T) {
plugin.DisableEvent = false
plugin.LogFile = "./testingdata/test.log"
plugin.MatchExpr = "test"
td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -371,7 +370,7 @@ func TestExecuteCheckWithNoEventAndFileError(t *testing.T) {
plugin.DisableEvent = false
plugin.LogFile = "./testingdata/test.log"
plugin.MatchExpr = "test"
td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -398,7 +397,7 @@ func TestExecuteWithEvent(t *testing.T) {
plugin.MatchExpr = "test"

// no events api defined error
td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -407,7 +406,7 @@ func TestExecuteWithEvent(t *testing.T) {
assert.Equal(t, 1, status)

// no name template error
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -418,7 +417,7 @@ func TestExecuteWithEvent(t *testing.T) {
assert.Equal(t, 1, status)

// 404 events api status error
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -427,7 +426,7 @@ func TestExecuteWithEvent(t *testing.T) {
status, err = executeCheck(event)
assert.NoError(t, err)
assert.Equal(t, 1, status)
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -437,7 +436,7 @@ func TestExecuteWithEvent(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, status)
plugin.DryRun = true
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -458,7 +457,7 @@ func TestProcessLogFile(t *testing.T) {
plugin.LogFile = "./testingdata/test.log"
plugin.MatchExpr = "test"

td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -468,7 +467,7 @@ func TestProcessLogFile(t *testing.T) {
enc := json.NewEncoder(eventBuf)

// test for good match
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -481,7 +480,7 @@ func TestProcessLogFile(t *testing.T) {
assert.Equal(t, 1, matches)

// test for abs log file path err
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -496,7 +495,7 @@ func TestProcessLogFile(t *testing.T) {

// test for IgnoreFirstRun
plugin.IgnoreInitialRun = true
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -506,7 +505,7 @@ func TestProcessLogFile(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, matches)
plugin.IgnoreInitialRun = false
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand Down Expand Up @@ -547,7 +546,7 @@ func TestProcessLogFile(t *testing.T) {
assert.NoError(t, err)

// test for state file write error
td, err = ioutil.TempDir("", "")
td, err = os.MkdirTemp("", "")
defer os.RemoveAll(td)
assert.NoError(t, err)
plugin.StateDir = td
Expand All @@ -568,12 +567,12 @@ func TestProcessLogFileRotatedFileVerboseTrue(t *testing.T) {
plugin.DisableEvent = true
plugin.MatchExpr = "brown"

td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(td)
plugin.StateDir = td

logdir, err := ioutil.TempDir("", "")
logdir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(logdir)

Expand All @@ -584,7 +583,7 @@ func TestProcessLogFileRotatedFileVerboseTrue(t *testing.T) {
_, err = f.WriteString("what now brown cow\n")
assert.NoError(t, err)
f.Close()
_, err = ioutil.ReadFile(plugin.LogFile)
_, err = os.ReadFile(plugin.LogFile)
assert.NoError(t, err)

eventBuf := new(bytes.Buffer)
Expand All @@ -609,7 +608,7 @@ func TestProcessLogFileRotatedFileVerboseTrue(t *testing.T) {
_, err = f.WriteString("the brown cow\n")
assert.NoError(t, err)
f.Close()
_, err = ioutil.ReadFile(plugin.LogFile)
_, err = os.ReadFile(plugin.LogFile)
assert.NoError(t, err)
logs, err = buildLogArray()
assert.NoError(t, err)
Expand All @@ -624,7 +623,7 @@ func TestProcessLogFileRotatedFileVerboseTrue(t *testing.T) {
_, err = f.WriteString("brown cows yeah!\n")
assert.NoError(t, err)
f.Close()
_, err = ioutil.ReadFile(plugin.LogFile)
_, err = os.ReadFile(plugin.LogFile)
assert.NoError(t, err)
matches, err = processLogFile(logs[0], enc)
assert.NoError(t, err)
Expand All @@ -639,12 +638,12 @@ func TestProcessLogFileRotatedFileVerboseFalse(t *testing.T) {
plugin.DisableEvent = true
plugin.MatchExpr = "brown"

td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(td)
plugin.StateDir = td

logdir, err := ioutil.TempDir("", "")
logdir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(logdir)

Expand All @@ -655,7 +654,7 @@ func TestProcessLogFileRotatedFileVerboseFalse(t *testing.T) {
_, err = f.WriteString("what now brown cow\n")
assert.NoError(t, err)
f.Close()
_, err = ioutil.ReadFile(plugin.LogFile)
_, err = os.ReadFile(plugin.LogFile)
assert.NoError(t, err)

eventBuf := new(bytes.Buffer)
Expand All @@ -680,7 +679,7 @@ func TestProcessLogFileRotatedFileVerboseFalse(t *testing.T) {
_, err = f.WriteString("the brown cow\n")
assert.NoError(t, err)
f.Close()
_, err = ioutil.ReadFile(plugin.LogFile)
_, err = os.ReadFile(plugin.LogFile)
assert.NoError(t, err)
logs, err = buildLogArray()
assert.NoError(t, err)
Expand All @@ -695,7 +694,7 @@ func TestProcessLogFileRotatedFileVerboseFalse(t *testing.T) {
_, err = f.WriteString("brown cows yeah!\n")
assert.NoError(t, err)
f.Close()
_, err = ioutil.ReadFile(plugin.LogFile)
_, err = os.ReadFile(plugin.LogFile)
assert.NoError(t, err)
matches, err = processLogFile(logs[0], enc)
assert.NoError(t, err)
Expand All @@ -712,12 +711,12 @@ func TestProcessLogFileWithNegativeCachedOffset(t *testing.T) {
plugin.DisableEvent = true
plugin.MatchExpr = "brown"

td, err := ioutil.TempDir("", "")
td, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(td)
plugin.StateDir = td

logdir, err := ioutil.TempDir("", "")
logdir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(logdir)

Expand Down

0 comments on commit 72f4348

Please sign in to comment.