Skip to content

Commit

Permalink
code style cleanup and adding golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Hurt committed Mar 27, 2024
1 parent 6d75bde commit ad789c5
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 157 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: golangci-lint
on:
push:
branches:
- master
- main
pull_request:

permissions:
contents: read
pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.57
22 changes: 22 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Refer to golangci-lint's example config file for more options and information:
# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml

run:
timeout: 5m
modules-download-mode: readonly

linters:
enable:
- errcheck
- gofmt
- goimports
- gocyclo
- govet
- misspell
- revive
- staticcheck

issues:
exclude-use-default: false
max-issues-per-linter: 0
max-same-issues: 0
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build:
@build/build.sh

test:
@go test -v
@go test -v ./...

release:
@build/build.sh -r
Expand Down
127 changes: 63 additions & 64 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ RELEASE_BUILD=0

## simple usage example
showUsage() {
printf "Usage: $0 [-d|-c|-r]
-d Remove binary and vendor directory
-c Clean distribution files
-r Build and package release binaries\n\n"
exit 0
printf "Usage: %s [-d|-c|-r]
-d Remove binary and vendor directory
-c Clean distribution files
-r Build and package release binaries\n\n" "$0"
exit 0
}

## install gox if needed
ensureGox() {
which gox > /dev/null 2>&1
if [ $? -ne 0 ]; then
printf "Installing gox ... "
go get github.com/mitchellh/gox
fi
if ! which gox &> /dev/null; then
printf "Installing gox ... "
go install github.com/mitchellh/gox@v1
fi
}

## we require module support
Expand All @@ -31,86 +30,86 @@ should_exit=false

## read options
while getopts ":dcr" opt; do
case $opt in
d)
printf "Removing binary and vendor directory ... "
rm -rf "${BUILD_NAME}" vendor
printf "done.\n"
should_exit=true
;;
c)
printf "Cleaning dist directory ... "
rm -rf ./dist/
printf "done.\n"
should_exit=true
;;
r)
ensureGox
RELEASE_BUILD=1
;;
*)
showUsage
;;
esac
case $opt in
d)
printf "Removing binary and vendor directory ... "
rm -rf "${BUILD_NAME}" vendor
printf "done.\n"
should_exit=true
;;
c)
printf "Cleaning dist directory ... "
rm -rf ./dist/
printf "done.\n"
should_exit=true
;;
r)
ensureGox
RELEASE_BUILD=1
;;
*)
showUsage
;;
esac
done

## remove options
shift $((OPTIND-1))
shift $((OPTIND - 1))

## exiting?
if [ $should_exit == true ]; then
exit 0
exit 0
fi

## check release option
if [ $RELEASE_BUILD -eq 1 ]; then
## clean dist directory
rm -rf ./dist/
## clean dist directory
rm -rf ./dist/

## call gox to build our binaries
CGO_ENABLED=0 gox \
-osarch="linux/amd64 darwin/amd64 freebsd/amd64 openbsd/amd64 windows/amd64 windows/386" \
-ldflags="-X main.appVersion=${RELEASE_VERSION} -s -w" \
-output="./dist/${BUILD_NAME}-${RELEASE_VERSION}-{{.Arch}}-{{.OS}}/${BUILD_NAME}-${RELEASE_VERSION}" \
> /dev/null >&1
## call gox to build our binaries
CGO_ENABLED=0 gox \
-osarch="linux/amd64 darwin/amd64 freebsd/amd64 openbsd/amd64 windows/amd64 windows/386" \
-ldflags="-X main.appVersion=${RELEASE_VERSION} -s -w" \
-output="./dist/${BUILD_NAME}-${RELEASE_VERSION}-{{.Arch}}-{{.OS}}/${BUILD_NAME}-${RELEASE_VERSION}" \
> /dev/null >&1

## gox return
RETVAL=$?
## gox return
RETURN_VALUE=$?

else

## build it
CGO_ENABLED=0 go build -o "${BUILD_NAME}" \
-ldflags="-X main.appVersion=${RELEASE_VERSION} -s -w" \
> /dev/null >&1
## build it
CGO_ENABLED=0 go build -o "${BUILD_NAME}" \
-ldflags="-X main.appVersion=${RELEASE_VERSION} -s -w" \
> /dev/null >&1

## go build return
RETVAL=$?
## go build return
RETURN_VALUE=$?
fi

## check build status
if [ $RETVAL -ne 0 ]; then
printf "\nError during build!\n"
exit $RETVAL
if [ ${RETURN_VALUE} -ne 0 ]; then
printf "\nError during build!\n"
exit ${RETURN_VALUE}
fi

## check release option
if [ $RELEASE_BUILD -eq 1 ]; then
## package binaries
printf "Packaging ... "
## package binaries
printf "Packaging ... "

## package files
pushd ./dist/ > /dev/null >&1
find . -maxdepth 1 -type d -name \*-\* \
-exec tar -czf {}.tar.gz {} > /dev/null >&1 \; \
-exec zip -m -r {}.zip {} > /dev/null >&1 \;
## package files
pushd ./dist/ || exit 1 > /dev/null >&1
find . -maxdepth 1 -type d -name \*-\* \
-exec tar -czf {}.tar.gz {} \; \
-exec zip -m -r {}.zip {} \; > /dev/null >&1

## all done
printf "done.\nRelease files may be found in the ./dist/ directory.\n"
## all done
printf "done.\nRelease files may be found in the ./dist/ directory.\n"
else
## all done
printf "done.\nUsage: ./${BUILD_NAME} -h\n"
## all done
printf "done.\nUsage: ./%s -h\n" "${BUILD_NAME}"
fi

## exit same as build
exit $RETVAL
exit ${RETURN_VALUE}
91 changes: 14 additions & 77 deletions build/codeCheck.sh
Original file line number Diff line number Diff line change
@@ -1,79 +1,16 @@
#!/usr/bin/env bash

## ensure we have staticcheck
## https://github.com/golang/lint
if ! staticcheck=$(type -p "${GOPATH}/bin/staticcheck"); then
echo -n "Installing staticcheck ... "
go install honnef.co/go/tools/cmd/staticcheck@latest
echo "done"
staticcheck=$(type -p "${GOPATH}/bin/staticcheck")
fi

## ensure we have the misspell tool
## https://github.com/client9/misspell
if ! misspell=$(type -p "${GOPATH}/bin/misspell"); then
echo -n "Installing misspell ... "
go install github.com/client9/misspell/cmd/misspell@latest
echo "done"
misspell=$(type -p "${GOPATH}/bin/misspell")
fi

## ensure we have the gocyclo tool
## https://github.com/fzipp/gocyclo
if ! gocyclo=$(type -p "${GOPATH}/bin/gocyclo"); then
echo -n "Installing gocyclo ... "
go install go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
echo "done"
gocyclo=$(type -p "${GOPATH}/bin/gocyclo")
fi

## check formatting ignoring git and vendor
fmtTest=$(find . -name '*.go' -not -path './.git/*' -not -path './vendor/*' | xargs gofmt -l -s 2>&1)
if [ ! -z "$fmtTest" ]; then
echo "gofmt failed"
echo "$fmtTest"
exit 1
else
echo "gofmt succeeded"
fi

## run go vet ignoring vendor and the silly "Error" bug/feature
## https://github.com/golang/go/issues/6407
vetTest=$(go vet ./... 2>&1 | egrep -v '^vendor/|\s+vendor/|/vendor/|^exit\ status|\ possible\ formatting\ directive\ in\ Error\ call')
if [ ! -z "$vetTest" ]; then
echo "go vet failed"
echo "$vetTest"
exit 1
else
echo "go vet succeeded"
fi

## run staticcheck ignoring vendor
staticTest=$(${staticcheck} ./... 2>&1 | egrep -v '^vendor/|\s+vendor/|/vendor/')
if [ ! -z "$statitTest" ]; then
echo "staticcheck failed"
echo "$staticTest"
exit 1
else
echo "staticcheck succeeded"
fi

## check misspell ignoring git, vendor and 3rdparty
spellTest=$(find . -name '*' -not -path './.git/*' -not -path './vendor/*' -not -path './3rdparty/*' | xargs ${misspell} 2>&1 | echo)
if [ ! -z "$spellTest" ]; then
echo "misspell failed"
echo "$spellTest"
exit 1
else
echo "misspell succeeded"
fi

## check gocyclo ignoring git and vendor
cycloTest=$(find . -name '*.go' -not -path './.git/*' -not -path './vendor/*' | xargs ${gocyclo} -over 15 2>&1 | echo)
if [ ! -z "$cycloTest" ]; then
echo "gocyclo failed"
echo "$cycloTest"
exit 1
else
echo "gocyclo succeeded"
fi
## ensure golangci-lint - https://golangci-lint.run
golangci="$(go env GOPATH)/bin/golangci-lint"
if ! ${golangci} version &> /dev/null; then
echo "Installing golangci-lint binary to ${golangci}"
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
sh -s -- -b "$(go env GOPATH)/bin" v1.57.1
echo "done"
fi

## run golangci-lint - all config in .golangci.yml
lintRun=$(${golangci} run --show-stats ./...)
lintRet=$?
echo "golangci-lint ${lintRun}"
exit $lintRet
1 change: 1 addition & 0 deletions command/check/check.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package check provides the check cli command
package check

import (
Expand Down
1 change: 1 addition & 0 deletions command/update/update.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package update provides the update cli command
package update

import (
Expand Down
14 changes: 4 additions & 10 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ import (
"github.com/leprechau/ipman/command/update"
)

// package global logger
var logger *stdLog.Logger

// available commands
var cliCommands map[string]cli.CommandFactory
// command logger
var logger = stdLog.New(os.Stderr, "", stdLog.LstdFlags)

// init command factory
func init() {
// init logger
logger = stdLog.New(os.Stderr, "", stdLog.LstdFlags)

func initComands() map[string]cli.CommandFactory {
// register sub commands
cliCommands = map[string]cli.CommandFactory{
return map[string]cli.CommandFactory{
"check": func() (cli.Command, error) {
return &check.Command{
Self: os.Args[0],
Expand Down
1 change: 1 addition & 0 deletions common/backend.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package common contains code shared between packages
package common

import (
Expand Down
1 change: 1 addition & 0 deletions common/dns/backend.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package dns defines the DNS service backend interface
package dns

// RType represents a dns record type
Expand Down
3 changes: 2 additions & 1 deletion common/dns/godaddy/godaddy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package godaddy provides a DNS backend via the GoDaddy service
package godaddy

import (
Expand Down Expand Up @@ -105,7 +106,7 @@ func parseError(resp *http.Response) error {
var err error

// close body when done
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

// check response code and set object
if resp.StatusCode == http.StatusTooManyRequests {
Expand Down
4 changes: 4 additions & 0 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ import (

// ErrUnknownArg is returned when non-flag arguments are present after the command
var ErrUnknownArg = errors.New("Unknown non-flag argument(s) present after command")

// ErrUnknownIPBackend is returned when an unknown IP backend is specified on the cli
var ErrUnknownIPBackend = errors.New("Unknown IP backend specified for command")

// ErrUnknownDNSBackend is returned when an unknown DNS backend is specified on the cli
var ErrUnknownDNSBackend = errors.New("Unknown DNS backend specified for command")
1 change: 1 addition & 0 deletions common/ip/backend.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package ip defines the IP lookup backend interface
package ip

// IFlag represents an address family inet flag
Expand Down
Loading

0 comments on commit ad789c5

Please sign in to comment.