Skip to content

Commit

Permalink
Merge pull request #26 from robgonnella/fix-linting
Browse files Browse the repository at this point in the history
Fix linting errors
  • Loading branch information
robgonnella authored Dec 27, 2023
2 parents e7285ff + 1979379 commit 3af15b0
Show file tree
Hide file tree
Showing 30 changed files with 188 additions and 77 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ jobs:
- name: Build
run: make

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55.2
skip-pkg-cache: true

- name: Test
run: make test
22 changes: 22 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
linters:
enable:
- revive
linters-settings:
revive:
rules:
- name: exported
issues:
exclude-rules:
- path: '(.+)_test\.go'
linters:
- errcheck
- revive
- path: '(.+)test_util/util.go'
linters:
- errcheck
- revive
include:
- EXC0012 # EXC0012 revive: Annoying issue about not having a comment. The rare codebase has such comments
- EXC0013 # EXC0013 revive: Annoying issue about not having a comment. The rare codebase has such comments
- EXC0014 # EXC0014 revive: Annoying issue about not having a comment. The rare codebase has such comments
- EXC0015 # EXC0015 revive: Annoying issue about not having a comment. The rare codebase has such comments
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ release: $(zips)

.PHONY: lint
lint:
golint -set_exit_status ./...
golangci-lint run

.PHONY: test
test:
Expand Down Expand Up @@ -109,7 +109,9 @@ test-report:
.PHONY: deps
deps:
go install go.uber.org/mock/mockgen@latest
go install golang.org/x/lint/golint@latest
curl \
-sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
sh -s -- -b $(shell go env GOPATH)/bin v1.55.2

# generate mocks
.PHONY: mock
Expand Down
5 changes: 0 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ require (
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.4.0
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.5
)

require (
Expand All @@ -26,14 +24,11 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackpal/gateway v1.0.13 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/oui v0.0.0-20150225163751-35b4deb627f8 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jackpal/gateway v1.0.13 h1:fJccMvawxx0k7S1q7Fy/SXFE0R3hMXkMuw8y9SofWAk=
github.com/jackpal/gateway v1.0.13/go.mod h1:6c8LjW+FVESFmwxaXySkt7fU98Yv806ADS3OY6Cvh2U=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/klauspost/oui v0.0.0-20150225163751-35b4deb627f8 h1:8vTSNy6M0xiuAOmKh271gD8sr6mM+5RzXAiqIUL0KmE=
github.com/klauspost/oui v0.0.0-20150225163751-35b4deb627f8/go.mod h1:iaF36Fc2UmrXJ7AGL+fEZU9WWuZiB+4dp9tQtADeZ6A=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand All @@ -58,8 +54,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
Expand Down Expand Up @@ -182,7 +176,3 @@ gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.4 h1:IqXwXi8M/ZlPzH/947tn5uik3aYQslP9BVveoax0nV0=
gorm.io/driver/sqlite v1.5.4/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
1 change: 1 addition & 0 deletions internal/config/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
Interface string `json:"interface"`
}

// Configs represents our collection of json configs
type Configs struct {
Configs []*Config `json:"configs"`
}
Expand Down
9 changes: 6 additions & 3 deletions internal/config/repo-json.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ type JSONRepo struct {
}

// NewJSONRepo returns a new ops repo for flat yaml file
func NewJSONRepo(configPath string) *JSONRepo {
func NewJSONRepo(configPath string) (*JSONRepo, error) {
repo := &JSONRepo{
configPath: configPath,
configs: []*Config{},
mux: sync.Mutex{},
}

repo.load()
if err := repo.load(); err != nil {
return nil, err
}

return repo
return repo, nil
}

// Get returns a config from the db
Expand Down Expand Up @@ -141,6 +143,7 @@ func (r *JSONRepo) Delete(id string) error {
return r.write()
}

// GetByInterface returns config associated with specific interface name
func (r *JSONRepo) GetByInterface(ifaceName string) (*Config, error) {
r.mux.Lock()
defer r.mux.Unlock()
Expand Down
36 changes: 35 additions & 1 deletion internal/config/repo_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_test

import (
"encoding/json"
"os"
"testing"

Expand All @@ -23,12 +24,45 @@ func assertEqualConf(t *testing.T, expected, actual *config.Config) {

func TestConfigYamlRepo(t *testing.T) {
testConfigFile := "config.json"
file, err := os.Create(testConfigFile)

assert.NoError(t, err)

defer func() {
os.RemoveAll(testConfigFile)
}()

repo := config.NewJSONRepo(testConfigFile)
conf := config.Config{
ID: "1",
Name: "myConfig",
SSH: config.SSHConfig{
User: "user",
Identity: "./id_rsa",
Port: "22",
Overrides: []config.SSHOverride{},
},
}

data, err := json.Marshal(conf)

assert.NoError(t, err)

_, err = file.WriteString(string(data))
file.Close()

assert.NoError(t, err)

repo, err := config.NewJSONRepo(testConfigFile)

assert.NoError(t, err)

t.Run("returns error if cannot instantiate instance", func(st *testing.T) {
repo, err := config.NewJSONRepo("nope.json")

assert.Nil(st, repo)
assert.Error(t, err)

})

t.Run("returns record not found error", func(st *testing.T) {
_, err := repo.Get("10")
Expand Down
2 changes: 2 additions & 0 deletions internal/config/service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package config

// nolint:revive
// ConfigService is an implementation of the config.Service interface
type ConfigService struct {
repo Repo
Expand All @@ -20,6 +21,7 @@ func (s *ConfigService) GetAll() ([]*Config, error) {
return s.repo.GetAll()
}

// GetByInterface returns config associated with given interface name
func (s *ConfigService) GetByInterface(ifaceName string) (*Config, error) {
return s.repo.GetByInterface(ifaceName)
}
Expand Down
12 changes: 9 additions & 3 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/robgonnella/ops/internal/logger"
)

// ScannerFactory is a function that returns a new instance of a Scanner
type ScannerFactory func(netInfo network.Network, conf config.Config) (discovery.Scanner, error)

// Core represents our core data structure through which the ui can interact
Expand Down Expand Up @@ -64,6 +65,7 @@ func (c *Core) Conf() config.Config {
return *c.conf
}

// NetworkInfo returns the core's network interface
func (c *Core) NetworkInfo() network.Network {
return c.networkInfo
}
Expand Down Expand Up @@ -164,8 +166,8 @@ func (c *Core) Monitor() error {
evtChan := make(chan event.Event)
if c.debug {
c.registeredListeners = append(c.registeredListeners,
c.eventManager.RegisterListener(discovery.DiscoveryArpUpdateEvent, evtChan),
c.eventManager.RegisterListener(discovery.DiscoverySynUpdateEvent, evtChan),
c.eventManager.RegisterListener(discovery.ArpUpdateEvent, evtChan),
c.eventManager.RegisterListener(discovery.SynUpdateEvent, evtChan),
c.eventManager.RegisterListener(event.FatalErrorEventType, evtChan),
)

Expand Down Expand Up @@ -204,5 +206,9 @@ func (c *Core) Monitor() error {

// StartDaemon starts the network monitoring processes in a goroutine
func (c *Core) StartDaemon() {
go c.Monitor()
go func() {
if err := c.Monitor(); err != nil {
c.eventManager.ReportFatalError(err)
}
}()
}
4 changes: 2 additions & 2 deletions internal/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ func TestCore(t *testing.T) {
})

expectedEvent := event.Event{
Type: discovery.DiscoverySynUpdateEvent,
Type: discovery.SynUpdateEvent,
Payload: discovery.DiscoveryResult{
Type: discovery.DiscoverySynUpdateEvent,
Type: discovery.SynUpdateEvent,
ID: mac.String(),
Hostname: details.Hostname,
IP: "127.0.0.1",
Expand Down
8 changes: 7 additions & 1 deletion internal/core/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ func getDefaultConfig(networkInfo network.Network) *config.Config {
// CreateNewAppCore creates and returns a new instance of *core.Core
func CreateNewAppCore(networkInfo network.Network, eventManager event.Manager, debug bool) (*Core, error) {
configPath := viper.Get("config-path").(string)
configRepo := config.NewJSONRepo(configPath)

configRepo, err := config.NewJSONRepo(configPath)

if err != nil {
return nil, err
}

configService := config.NewConfigService(configRepo)

conf, err := configService.GetByInterface(networkInfo.Interface().Name)
Expand Down
1 change: 1 addition & 0 deletions internal/discovery/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Port struct {
Status PortStatus
}

// nolint:revive
// DiscoveryResult represents our discovered device on the network
type DiscoveryResult struct {
Type string
Expand Down
21 changes: 14 additions & 7 deletions internal/discovery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
)

const (
DiscoveryArpUpdateEvent = "DISCOVERY_ARP_UPDATE"
DiscoverySynUpdateEvent = "DISCOVERY_SYN_UPDATE"
// ArpUpdateEvent represents an ARP update event
ArpUpdateEvent = "DISCOVERY_ARP_UPDATE"
// SynUpdateEvent represents an SYN update event
SynUpdateEvent = "DISCOVERY_SYN_UPDATE"
)

// ScannerService implements the Service interface for monitoring a network
Expand Down Expand Up @@ -69,11 +71,16 @@ func (s *ScannerService) Stop() {
s.scanner.Stop()
}

// SetConfigAndScanner sets the config and scanner to use when performing network discovery
func (s *ScannerService) SetConfigAndScanner(conf config.Config, netScanner Scanner) {
if s.monitoring {
s.pause()
defer func() {
go s.pollNetwork()
go func() {
if err := s.pollNetwork(); err != nil {
s.eventManager.ReportFatalError(err)
}
}()
}()
}
s.conf = conf
Expand Down Expand Up @@ -115,7 +122,7 @@ func (s *ScannerService) pollNetwork() error {
case scanner.ARPResult:
res := r.Payload.(*scanner.ArpScanResult)
dr := &DiscoveryResult{
Type: DiscoveryArpUpdateEvent,
Type: ArpUpdateEvent,
ID: res.MAC.String(),
IP: res.IP.String(),
Hostname: "Unknown",
Expand All @@ -131,7 +138,7 @@ func (s *ScannerService) pollNetwork() error {
case scanner.SYNResult:
res := r.Payload.(*scanner.SynScanResult)
dr := &DiscoveryResult{
Type: DiscoverySynUpdateEvent,
Type: SynUpdateEvent,
ID: res.MAC.String(),
IP: res.IP.String(),
Hostname: "",
Expand Down Expand Up @@ -182,7 +189,7 @@ func (s *ScannerService) getConfiguredSSHPort(result *DiscoveryResult) *string {
}

func (s *ScannerService) handleArpDiscoveryResult(result *DiscoveryResult) {
if result.Type != DiscoveryArpUpdateEvent {
if result.Type != ArpUpdateEvent {
return
}

Expand All @@ -207,7 +214,7 @@ func (s *ScannerService) handleArpDiscoveryResult(result *DiscoveryResult) {

// handle results found during polling
func (s *ScannerService) handleSynDiscoveryResult(result *DiscoveryResult) {
if result.Type != DiscoverySynUpdateEvent {
if result.Type != SynUpdateEvent {
return
}

Expand Down
12 changes: 6 additions & 6 deletions internal/discovery/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func TestDiscoveryService(t *testing.T) {
wg.Add(1)

expectedEvt := event.Event{
Type: discovery.DiscoverySynUpdateEvent,
Type: discovery.SynUpdateEvent,
Payload: discovery.DiscoveryResult{
Type: discovery.DiscoverySynUpdateEvent,
Type: discovery.SynUpdateEvent,
ID: mac.String(),
Hostname: "Unknown",
IP: "127.0.0.1",
Expand Down Expand Up @@ -155,9 +155,9 @@ func TestDiscoveryService(t *testing.T) {
wg.Add(1)

expectedEvt := event.Event{
Type: discovery.DiscoverySynUpdateEvent,
Type: discovery.SynUpdateEvent,
Payload: discovery.DiscoveryResult{
Type: discovery.DiscoverySynUpdateEvent,
Type: discovery.SynUpdateEvent,
ID: mac.String(),
Hostname: "Unknown",
IP: "127.0.0.1",
Expand Down Expand Up @@ -234,9 +234,9 @@ func TestDiscoveryService(t *testing.T) {
mockScanner.EXPECT().Stop()

expectedEvt := event.Event{
Type: discovery.DiscoverySynUpdateEvent,
Type: discovery.SynUpdateEvent,
Payload: discovery.DiscoveryResult{
Type: discovery.DiscoverySynUpdateEvent,
Type: discovery.SynUpdateEvent,
ID: mac.String(),
Hostname: "fancy-hostname",
IP: "127.0.0.1",
Expand Down
Loading

0 comments on commit 3af15b0

Please sign in to comment.