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

Fix linting errors #20

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 .github/workflows/test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ jobs:
- name: Install dependencies
run: sudo apt update && sudo apt install -y make libpcap-dev

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

- name: Run Test
run: |
make test
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ jobs:
- name: Build
run: make

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

- name: Test
run: make test
20 changes: 20 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
linters:
enable:
- revive
linters-settings:
revive:
rules:
- name: exported
issues:
exclude-rules:
- path: '(.+)_test\.go'
linters:
- errcheck
- path: '(.+)test-helper.go'
linters:
- errcheck
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 @@ -89,7 +89,7 @@ mock:

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

.PHONY: test
test:
Expand All @@ -116,7 +116,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

# remove buid directory and installed executable
.PHONY: clean
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# go-lanscan
![Coverage](https://img.shields.io/badge/Coverage-92.6%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-92.5%25-brightgreen)

A network cli and golang package that allows you to perform arp and syn
scanning on a local area network.
Expand Down
18 changes: 12 additions & 6 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func printConfiguration(
listenPort uint16,
timing string,
vendorInfo,
printJson,
printJSON,
arpOnly,
progress bool,
outFile string,
Expand Down Expand Up @@ -78,7 +78,7 @@ func printConfiguration(

configTable.AppendRow(table.Row{
"json",
printJson,
printJSON,
})

configTable.AppendRow(table.Row{
Expand All @@ -91,15 +91,21 @@ func printConfiguration(
progress,
})

configTable.AppendRow(table.Row{
"outFile",
outFile,
})

configTable.Render()
}

// Root returns root command for cli
func Root(
runner core.Runner,
userNet network.Network,
vendorRepo oui.VendorRepo,
) (*cobra.Command, error) {
var printJson bool
var printJSON bool
var noProgress bool
var ports []string
var timing string
Expand Down Expand Up @@ -177,7 +183,7 @@ func Root(
portLen,
noProgress,
arpOnly,
printJson,
printJSON,
outFile,
)

Expand All @@ -191,7 +197,7 @@ func Root(
listenPort,
timing,
vendorInfo,
printJson,
printJSON,
arpOnly,
!noProgress,
outFile,
Expand All @@ -203,7 +209,7 @@ func Root(
}

cmd.Flags().StringVar(&timing, "timing", "100µs", "set time between packet sends - the faster you send the less accurate the result will be")
cmd.Flags().BoolVar(&printJson, "json", false, "output json instead of table text")
cmd.Flags().BoolVar(&printJSON, "json", false, "output json instead of table text")
cmd.Flags().BoolVar(&arpOnly, "arp-only", false, "only perform arp scanning (skip syn scanning)")
cmd.Flags().BoolVar(&noProgress, "no-progress", false, "disable all output except for final results")
cmd.Flags().StringSliceVarP(&ports, "ports", "p", []string{"1-65535"}, "target ports")
Expand Down
18 changes: 13 additions & 5 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/rs/zerolog"
)

// DeviceResult represents a discovered network device
type DeviceResult struct {
IP net.IP `json:"ip"`
MAC net.HardwareAddr `json:"mac"`
Expand All @@ -28,6 +29,7 @@ type DeviceResult struct {
OpenPorts []scanner.Port `json:"openPorts"`
}

// Serializable returns a serializable version of DeviceResult
func (r *DeviceResult) Serializable() interface{} {
return struct {
IP string `json:"ip"`
Expand All @@ -44,10 +46,12 @@ func (r *DeviceResult) Serializable() interface{} {
}
}

// Results data structure for holding discovered network devices
type Results struct {
Devices []*DeviceResult `json:"devices"`
}

// MarshalJSON returns marshaled JSON of Results
func (r *Results) MarshalJSON() ([]byte, error) {
data := []interface{}{}

Expand All @@ -58,9 +62,10 @@ func (r *Results) MarshalJSON() ([]byte, error) {
return json.Marshal(data)
}

// Core implements the Runner interface for performing network scanning
type Core struct {
arpOnly bool
printJson bool
printJSON bool
noProgress bool
outFile string
portLen int
Expand All @@ -75,6 +80,7 @@ type Core struct {
log logger.Logger
}

// New returns a new instance of Core
func New() *Core {
return &Core{
requestNotifier: make(chan *scanner.Request),
Expand All @@ -83,13 +89,14 @@ func New() *Core {
}
}

// Initialize initializes the Core before performing network scanning
func (c *Core) Initialize(
coreScanner scanner.Scanner,
targetLen int,
portLen int,
noProgress bool,
arpOnly bool,
printJson bool,
printJSON bool,
outFile string,
) {
pw := progressWriter()
Expand All @@ -116,10 +123,11 @@ func (c *Core) Initialize(
c.synTracker = &progress.Tracker{Message: "starting syn scan"}
c.noProgress = noProgress
c.arpOnly = arpOnly
c.printJson = printJson
c.printJSON = printJSON
c.outFile = outFile
}

// Run executes a network scan
func (c *Core) Run() error {
start := time.Now()

Expand Down Expand Up @@ -256,7 +264,7 @@ func (c *Core) printArpResults() {
c.mux.RLock()
defer c.mux.RUnlock()

if c.printJson {
if c.printJSON {
data, err := c.results.MarshalJSON()

if err != nil {
Expand Down Expand Up @@ -300,7 +308,7 @@ func (c *Core) printSynResults() {
c.mux.RLock()
defer c.mux.RUnlock()

if c.printJson {
if c.printJSON {
data, err := c.results.MarshalJSON()

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import "github.com/robgonnella/go-lanscan/pkg/scanner"

//go:generate mockgen -destination=../mock/core/core.go -package=mock_core . Runner

// Runner interface for performing network scanning
type Runner interface {
Initialize(
coreScanner scanner.Scanner,
targetLen int,
portLen int,
noProgress bool,
arpOnly bool,
printJson bool,
printJSON bool,
outFile string,
)
Run() error
Expand Down
1 change: 1 addition & 0 deletions internal/info/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

package info

// VERSION the version of this library
var VERSION = "v1.14.0"
11 changes: 7 additions & 4 deletions internal/logger/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ func init() {
}
}

func NewDebugLogger() DebugLogger {
return debugLogger
}

// DebugLogger represents a Logger implementation that is only turned on
// when build with the "debug" tag
type DebugLogger struct {
zl *zerolog.Logger
}

// NewDebugLogger returns a instance of DebugLogger
func NewDebugLogger() DebugLogger {
return debugLogger
}

// Info wrapper around zerolog Info
func (l DebugLogger) Info() *zerolog.Event {
return l.zl.Info()
Expand Down
2 changes: 1 addition & 1 deletion internal/mock/scripts/bump-version/version/version.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion internal/scripts/bump-version/version/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"path/filepath"
)

// TemplateGenerator implements the VersionGenerator interface using templates
type TemplateGenerator struct {
outFile string
outDir string
templatePath string
templateName string
}

// NewTemplateGenerator returns a new instance of TemplateGenerator
func NewTemplateGenerator(outFile, templatePath string) *TemplateGenerator {
return &TemplateGenerator{
outFile: outFile,
Expand All @@ -24,7 +26,8 @@ func NewTemplateGenerator(outFile, templatePath string) *TemplateGenerator {
}
}

func (t *TemplateGenerator) Generate(data VersionData) error {
// Generate implements the Generate interface method using templates
func (t *TemplateGenerator) Generate(data Data) error {
if err := os.MkdirAll(t.outDir, 0751); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/scripts/bump-version/version/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestTemplateGenerator(t *testing.T) {

generator := version.NewTemplateGenerator(outFile, templatePath)

data := version.VersionData{
data := version.Data{
VERSION: "v2.2.2",
}

Expand Down
5 changes: 5 additions & 0 deletions internal/scripts/bump-version/version/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ package version

import "os/exec"

// Git implementation of the VersionControl interface using git
type Git struct{}

// NewGit returns a new instance of Git
func NewGit() *Git {
return &Git{}
}

// Add implements the Add method using git
func (g *Git) Add(filePath string) error {
cmd := exec.Command("git", "add", filePath)
return cmd.Run()
}

// Commit implements the Commit method using git
func (g *Git) Commit(message string) error {
cmd := exec.Command("git", "commit", "-m", message)
return cmd.Run()
}

// Tag implements the tag method using git
func (g *Git) Tag(version string) error {
cmd := exec.Command("git", "tag", "-m", version, version)
return cmd.Run()
Expand Down
10 changes: 8 additions & 2 deletions internal/scripts/bump-version/version/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

package version

type VersionData struct {
// Data represents the version data passed to template generator
type Data struct {
VERSION string
}

//go:generate mockgen -destination=../../../mock/scripts/bump-version/version/version.go -package=mock_version . VersionControl,VersionGenerator

// nolint:revive
// VersionControl interface representing a version control system
type VersionControl interface {
Add(filePath string) error
Commit(message string) error
Tag(version string) error
}

// nolint:revive
// VersionGenerator interface representing a generator of version files
// for this library
type VersionGenerator interface {
Generate(data VersionData) error
Generate(data Data) error
}
3 changes: 3 additions & 0 deletions internal/scripts/bump-version/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"fmt"
)

// BumpData represents the data required to perform a version bump for this
// library
type BumpData struct {
Version string
OutFile string
TemplatePath string
}

// Bump executes a version bump for this library
func Bump(data BumpData, vg VersionGenerator, vc VersionControl) error {
if string(data.Version[0]) != "v" {
return errors.New("version must begin with a \"v\"")
Expand Down
Loading