Skip to content

Commit

Permalink
chore: gofumpt
Browse files Browse the repository at this point in the history
Format all Go files with gofumpt.

https://github.com/mvdan/gofumpt

	gofumpt -extra -w .
  • Loading branch information
uhthomas committed Feb 26, 2024
1 parent 5e33c33 commit 9d124df
Show file tree
Hide file tree
Showing 71 changed files with 703 additions and 678 deletions.
40 changes: 20 additions & 20 deletions collector/cmd/collector-metrics/collector-metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ package main
import (
"encoding/json"
"fmt"
"github.com/analogj/scrutiny/collector/pkg/collector"
"github.com/analogj/scrutiny/collector/pkg/config"
"github.com/analogj/scrutiny/collector/pkg/errors"
"github.com/analogj/scrutiny/webapp/backend/pkg/version"
"github.com/sirupsen/logrus"
"io"
"log"
"os"
"strings"
"time"

"github.com/analogj/scrutiny/collector/pkg/collector"
"github.com/analogj/scrutiny/collector/pkg/config"
"github.com/analogj/scrutiny/collector/pkg/errors"
"github.com/analogj/scrutiny/webapp/backend/pkg/version"
"github.com/sirupsen/logrus"

utils "github.com/analogj/go-util/utils"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)

var goos string
var goarch string
var (
goos string
goarch string
)

func main() {

config, err := config.Create()
if err != nil {
fmt.Printf("FATAL: %+v\n", err)
Expand All @@ -36,10 +38,10 @@ func main() {
configFilePath = configFilePathAlternative
}

//we're going to load the config file manually, since we need to validate it.
err = config.ReadConfig(configFilePath) // Find and read the config file
if _, ok := err.(errors.ConfigFileMissingError); ok { // Handle errors reading the config file
//ignore "could not find config file"
// we're going to load the config file manually, since we need to validate it.
err = config.ReadConfig(configFilePath) // Find and read the config file
if _, ok := err.(errors.ConfigFileMissingError); ok { // Handle errors reading the config file
// ignore "could not find config file"
} else if err != nil {
os.Exit(1)
}
Expand Down Expand Up @@ -69,7 +71,6 @@ OPTIONS:
},
},
Before: func(c *cli.Context) error {

collectorMetrics := "AnalogJ/scrutiny/metrics"

var versionInfo string
Expand Down Expand Up @@ -102,12 +103,12 @@ OPTIONS:
if c.IsSet("config") {
err = config.ReadConfig(c.String("config")) // Find and read the config file
if err != nil { // Handle errors reading the config file
//ignore "could not find config file"
// ignore "could not find config file"
fmt.Printf("Could not find config file at specified path: %s", c.String("config"))
return err
}
}
//override config with flags if set
// override config with flags if set
if c.IsSet("host-id") {
config.Set("host.id", c.String("host-id")) // set/override the host-id using CLI.
}
Expand All @@ -121,8 +122,8 @@ OPTIONS:
}

if c.IsSet("api-endpoint") {
//if the user is providing an api-endpoint with a basepath (eg. http://localhost:8080/scrutiny),
//we need to ensure the basepath has a trailing slash, otherwise the url.Parse() path concatenation doesnt work.
// if the user is providing an api-endpoint with a basepath (eg. http://localhost:8080/scrutiny),
// we need to ensure the basepath has a trailing slash, otherwise the url.Parse() path concatenation doesnt work.
apiEndpoint := strings.TrimSuffix(c.String("api-endpoint"), "/") + "/"
config.Set("api.endpoint", apiEndpoint)
}
Expand All @@ -142,7 +143,6 @@ OPTIONS:
collectorLogger,
config.GetString("api.endpoint"),
)

if err != nil {
return err
}
Expand All @@ -159,7 +159,7 @@ OPTIONS:
Name: "api-endpoint",
Usage: "The api server endpoint",
EnvVars: []string{"COLLECTOR_API_ENDPOINT", "SCRUTINY_API_ENDPOINT"},
//SCRUTINY_API_ENDPOINT is deprecated, but kept for backwards compatibility
// SCRUTINY_API_ENDPOINT is deprecated, but kept for backwards compatibility
},

&cli.StringFlag{
Expand Down Expand Up @@ -205,7 +205,7 @@ func CreateLogger(appConfig config.Interface) (*logrus.Entry, *os.File, error) {
var logFile *os.File
var err error
if appConfig.IsSet("log.file") && len(appConfig.GetString("log.file")) > 0 {
logFile, err = os.OpenFile(appConfig.GetString("log.file"), os.O_CREATE|os.O_WRONLY, 0644)
logFile, err = os.OpenFile(appConfig.GetString("log.file"), os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
logger.Logger.Errorf("Failed to open log file %s for output: %s", appConfig.GetString("log.file"), err)
return nil, logFile, err
Expand Down
22 changes: 10 additions & 12 deletions collector/cmd/collector-selftest/collector-selftest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ package main

import (
"fmt"
"github.com/analogj/scrutiny/collector/pkg/collector"
"github.com/analogj/scrutiny/webapp/backend/pkg/version"
"github.com/sirupsen/logrus"
"io"
"log"
"os"
"time"

"github.com/analogj/scrutiny/collector/pkg/collector"
"github.com/analogj/scrutiny/webapp/backend/pkg/version"
"github.com/sirupsen/logrus"

utils "github.com/analogj/go-util/utils"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)

var goos string
var goarch string
var (
goos string
goarch string
)

func main() {

cli.CommandHelpTemplate = `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
Expand All @@ -45,7 +47,6 @@ OPTIONS:
},
},
Before: func(c *cli.Context) error {

collectorSelfTest := "AnalogJ/scrutiny/selftest"

var versionInfo string
Expand Down Expand Up @@ -75,7 +76,6 @@ OPTIONS:
Name: "run",
Usage: "Run the scrutiny self-test data collector",
Action: func(c *cli.Context) error {

collectorLogger := logrus.WithFields(logrus.Fields{
"type": "selftest",
})
Expand All @@ -87,7 +87,7 @@ OPTIONS:
}

if c.IsSet("log-file") {
logFile, err := os.OpenFile(c.String("log-file"), os.O_CREATE|os.O_WRONLY, 0644)
logFile, err := os.OpenFile(c.String("log-file"), os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
logrus.Errorf("Failed to open log file %s for output: %s", c.String("log-file"), err)
return err
Expand All @@ -96,12 +96,11 @@ OPTIONS:
logrus.SetOutput(io.MultiWriter(os.Stderr, logFile))
}

//TODO: pass in the collector, use configuration from collector-metrics
// TODO: pass in the collector, use configuration from collector-metrics
stCollector, err := collector.CreateSelfTestCollector(
collectorLogger,
c.String("api-endpoint"),
)

if err != nil {
return err
}
Expand Down Expand Up @@ -138,5 +137,4 @@ OPTIONS:
if err != nil {
log.Fatal(color.HiRedString("ERROR: %v", err))
}

}
6 changes: 3 additions & 3 deletions collector/pkg/collector/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package collector
import (
"bytes"
"encoding/json"
"github.com/sirupsen/logrus"
"net/http"
"time"

"github.com/sirupsen/logrus"
)

var httpClient = &http.Client{Timeout: 60 * time.Second}
Expand All @@ -15,7 +16,6 @@ type BaseCollector struct {
}

func (c *BaseCollector) getJson(url string, target interface{}) error {

r, err := httpClient.Get(url)
if err != nil {
return err
Expand All @@ -25,7 +25,7 @@ func (c *BaseCollector) getJson(url string, target interface{}) error {
return json.NewDecoder(r.Body).Decode(target)
}

func (c *BaseCollector) postJson(url string, body interface{}, target interface{}) error {
func (c *BaseCollector) postJson(url string, body, target interface{}) error {
requestBody, err := json.Marshal(body)
if err != nil {
return err
Expand Down
35 changes: 18 additions & 17 deletions collector/pkg/collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"os"
"os/exec"
"strings"

"github.com/analogj/scrutiny/collector/pkg/common/shell"
"github.com/analogj/scrutiny/collector/pkg/config"
"github.com/analogj/scrutiny/collector/pkg/detect"
"github.com/analogj/scrutiny/collector/pkg/errors"
"github.com/analogj/scrutiny/collector/pkg/models"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"net/url"
"os"
"os/exec"
"strings"
)

type MetricsCollector struct {
Expand Down Expand Up @@ -49,7 +50,7 @@ func (mc *MetricsCollector) Run() error {
}

apiEndpoint, _ := url.Parse(mc.apiEndpoint.String())
apiEndpoint, _ = apiEndpoint.Parse("api/devices/register") //this acts like filepath.Join()
apiEndpoint, _ = apiEndpoint.Parse("api/devices/register") // this acts like filepath.Join()

deviceRespWrapper := new(models.DeviceWrapper)

Expand All @@ -62,7 +63,7 @@ func (mc *MetricsCollector) Run() error {
return err
}

//filter any device with empty wwn (they are invalid)
// filter any device with empty wwn (they are invalid)
detectedStorageDevices := lo.Filter[models.Device](rawDetectedStorageDevices, func(dev models.Device, _ int) bool {
return len(dev.WWN) > 0
})
Expand All @@ -83,19 +84,19 @@ func (mc *MetricsCollector) Run() error {
return errors.ApiServerCommunicationError("An error occurred while retrieving filtered devices")
} else {
mc.logger.Debugln(deviceRespWrapper)
//var wg sync.WaitGroup
// var wg sync.WaitGroup
for _, device := range deviceRespWrapper.Data {
// execute collection in parallel go-routines
//wg.Add(1)
//go mc.Collect(&wg, device.WWN, device.DeviceName, device.DeviceType)
// wg.Add(1)
// go mc.Collect(&wg, device.WWN, device.DeviceName, device.DeviceType)
mc.Collect(device.WWN, device.DeviceName, device.DeviceType)

// TODO: we may need to sleep for between each call to smartctl -a
//time.Sleep(30 * time.Millisecond)
// time.Sleep(30 * time.Millisecond)
}

//mc.logger.Infoln("Main: Waiting for workers to finish")
//wg.Wait()
// mc.logger.Infoln("Main: Waiting for workers to finish")
// wg.Wait()
mc.logger.Infoln("Main: Completed")
}

Expand All @@ -113,9 +114,9 @@ func (mc *MetricsCollector) Validate() error {
return nil
}

//func (mc *MetricsCollector) Collect(wg *sync.WaitGroup, deviceWWN string, deviceName string, deviceType string) {
func (mc *MetricsCollector) Collect(deviceWWN string, deviceName string, deviceType string) {
//defer wg.Done()
// func (mc *MetricsCollector) Collect(wg *sync.WaitGroup, deviceWWN string, deviceName string, deviceType string) {
func (mc *MetricsCollector) Collect(deviceWWN, deviceName, deviceType string) {
// defer wg.Done()
if len(deviceWWN) == 0 {
mc.logger.Errorf("no device WWN detected for %s. Skipping collection for this device (no data association possible).\n", deviceName)
return
Expand All @@ -124,7 +125,7 @@ func (mc *MetricsCollector) Collect(deviceWWN string, deviceName string, deviceT

fullDeviceName := fmt.Sprintf("%s%s", detect.DevicePrefix(), deviceName)
args := strings.Split(mc.config.GetCommandMetricsSmartArgs(fullDeviceName), " ")
//only include the device type if its a non-standard one. In some cases ata drives are detected as scsi in docker, and metadata is lost.
// only include the device type if its a non-standard one. In some cases ata drives are detected as scsi in docker, and metadata is lost.
if len(deviceType) > 0 && deviceType != "scsi" && deviceType != "ata" {
args = append(args, "--device", deviceType)
}
Expand All @@ -145,7 +146,7 @@ func (mc *MetricsCollector) Collect(deviceWWN string, deviceName string, deviceT
}
return
} else {
//successful run, pass the results directly to webapp backend for parsing and processing.
// successful run, pass the results directly to webapp backend for parsing and processing.
mc.Publish(deviceWWN, resultBytes)
}
}
Expand Down
5 changes: 3 additions & 2 deletions collector/pkg/collector/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package collector

import (
"github.com/stretchr/testify/require"
"net/url"
"testing"

"github.com/stretchr/testify/require"
)

func TestApiEndpointParse(t *testing.T) {
Expand All @@ -19,7 +20,7 @@ func TestApiEndpointParse(t *testing.T) {
func TestApiEndpointParse_WithBasepathWithoutTrailingSlash(t *testing.T) {
baseURL, _ := url.Parse("http://localhost:8080/scrutiny")

//This testcase is unexpected and can cause issues. We need to ensure the apiEndpoint always has a trailing slash.
// This testcase is unexpected and can cause issues. We need to ensure the apiEndpoint always has a trailing slash.
url1, _ := baseURL.Parse("d/e")
require.Equal(t, "http://localhost:8080/d/e", url1.String())

Expand Down
3 changes: 2 additions & 1 deletion collector/pkg/collector/selftest.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package collector

import (
"github.com/sirupsen/logrus"
"net/url"

"github.com/sirupsen/logrus"
)

type SelfTestCollector struct {
Expand Down
4 changes: 2 additions & 2 deletions collector/pkg/common/shell/local_shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package shell
import (
"bytes"
"errors"
"github.com/sirupsen/logrus"
"io"
"os/exec"
"path"
"strings"

"github.com/sirupsen/logrus"
)

type localShell struct{}
Expand Down Expand Up @@ -41,5 +42,4 @@ func (s *localShell) Command(logger *logrus.Entry, cmdName string, cmdArgs []str

err := cmd.Run()
return stdBuffer.String(), err

}
Loading

0 comments on commit 9d124df

Please sign in to comment.