From aba42f9fa643c6c4fa01f2fac7b2bc7e13acbce4 Mon Sep 17 00:00:00 2001 From: Wade Simmons Date: Tue, 30 Jun 2020 18:53:30 -0400 Subject: [PATCH] enforce the use of goimports (#248) * enforce the use of goimports Instead of enforcing `gofmt`, enforce `goimports`, which also asserts a separate section for non-builtin packages. * run `goimports` everywhere * exclude generated .pb.go files --- .github/workflows/gofmt.yml | 16 ++++++++++++++-- cert/cert.go | 4 ++-- cmd/nebula-cert/main_test.go | 3 ++- cmd/nebula-cert/print.go | 3 ++- cmd/nebula-cert/print_test.go | 5 +++-- cmd/nebula-cert/verify.go | 3 ++- cmd/nebula-cert/verify_test.go | 7 ++++--- cmd/nebula-service/main.go | 3 ++- cmd/nebula-service/service.go | 2 +- cmd/nebula/main.go | 2 +- config.go | 7 ++++--- config_test.go | 3 ++- firewall.go | 11 +++++------ handshake_ix.go | 3 +-- header_test.go | 3 ++- lighthouse_test.go | 2 +- logger_test.go | 3 ++- main.go | 7 ++++--- outside.go | 10 +++------- outside_test.go | 5 +++-- punchy_test.go | 3 ++- ssh.go | 5 +++-- sshd/command.go | 3 ++- sshd/server.go | 3 ++- sshd/session.go | 5 +++-- stats.go | 11 ++++++----- timeout_test.go | 3 ++- tun_darwin.go | 3 ++- 28 files changed, 82 insertions(+), 56 deletions(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index bada507..a7603ea 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -23,10 +23,22 @@ jobs: - name: Check out code into the Go module directory uses: actions/checkout@v1 + - uses: actions/cache@v1 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-gofmt-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-gofmt- + + - name: Install goimports + run: | + go get golang.org/x/tools/cmd/goimports + go build golang.org/x/tools/cmd/goimports + - name: gofmt run: | - if [ "$(find . -iname '*.go' | xargs gofmt -l)" ] + if [ "$(find . -iname '*.go' | grep -v '\.pb\.go$' | xargs ./goimports -l)" ] then - find . -iname '*.go' | xargs gofmt -d + find . -iname '*.go' | grep -v '\.pb\.go$' | xargs ./goimports -d exit 1 fi diff --git a/cert/cert.go b/cert/cert.go index b868b4f..b2d2e0b 100644 --- a/cert/cert.go +++ b/cert/cert.go @@ -1,18 +1,18 @@ package cert import ( + "bytes" "crypto" "crypto/rand" "crypto/sha256" "encoding/binary" "encoding/hex" + "encoding/json" "encoding/pem" "fmt" "net" "time" - "bytes" - "encoding/json" "github.com/golang/protobuf/proto" "golang.org/x/crypto/curve25519" "golang.org/x/crypto/ed25519" diff --git a/cmd/nebula-cert/main_test.go b/cmd/nebula-cert/main_test.go index 0c3360e..07c7c07 100644 --- a/cmd/nebula-cert/main_test.go +++ b/cmd/nebula-cert/main_test.go @@ -3,10 +3,11 @@ package main import ( "bytes" "errors" - "github.com/stretchr/testify/assert" "io" "os" "testing" + + "github.com/stretchr/testify/assert" ) //TODO: all flag parsing continueOnError will print to stderr on its own currently diff --git a/cmd/nebula-cert/print.go b/cmd/nebula-cert/print.go index ebb3433..fd0fcf5 100644 --- a/cmd/nebula-cert/print.go +++ b/cmd/nebula-cert/print.go @@ -4,11 +4,12 @@ import ( "encoding/json" "flag" "fmt" - "github.com/slackhq/nebula/cert" "io" "io/ioutil" "os" "strings" + + "github.com/slackhq/nebula/cert" ) type printFlags struct { diff --git a/cmd/nebula-cert/print_test.go b/cmd/nebula-cert/print_test.go index 7f8899a..bed5116 100644 --- a/cmd/nebula-cert/print_test.go +++ b/cmd/nebula-cert/print_test.go @@ -2,12 +2,13 @@ package main import ( "bytes" - "github.com/slackhq/nebula/cert" - "github.com/stretchr/testify/assert" "io/ioutil" "os" "testing" "time" + + "github.com/slackhq/nebula/cert" + "github.com/stretchr/testify/assert" ) func Test_printSummary(t *testing.T) { diff --git a/cmd/nebula-cert/verify.go b/cmd/nebula-cert/verify.go index d6a7174..51b9a93 100644 --- a/cmd/nebula-cert/verify.go +++ b/cmd/nebula-cert/verify.go @@ -3,12 +3,13 @@ package main import ( "flag" "fmt" - "github.com/slackhq/nebula/cert" "io" "io/ioutil" "os" "strings" "time" + + "github.com/slackhq/nebula/cert" ) type verifyFlags struct { diff --git a/cmd/nebula-cert/verify_test.go b/cmd/nebula-cert/verify_test.go index eb12a9b..afd97e4 100644 --- a/cmd/nebula-cert/verify_test.go +++ b/cmd/nebula-cert/verify_test.go @@ -3,13 +3,14 @@ package main import ( "bytes" "crypto/rand" - "github.com/slackhq/nebula/cert" - "github.com/stretchr/testify/assert" - "golang.org/x/crypto/ed25519" "io/ioutil" "os" "testing" "time" + + "github.com/slackhq/nebula/cert" + "github.com/stretchr/testify/assert" + "golang.org/x/crypto/ed25519" ) func Test_verifySummary(t *testing.T) { diff --git a/cmd/nebula-service/main.go b/cmd/nebula-service/main.go index b5ea062..8b9b9ea 100644 --- a/cmd/nebula-service/main.go +++ b/cmd/nebula-service/main.go @@ -3,9 +3,10 @@ package main import ( "flag" "fmt" + "os" + "github.com/sirupsen/logrus" "github.com/slackhq/nebula" - "os" ) // A version string that can be set with diff --git a/cmd/nebula-service/service.go b/cmd/nebula-service/service.go index e96bbf4..c04e7d8 100644 --- a/cmd/nebula-service/service.go +++ b/cmd/nebula-service/service.go @@ -2,12 +2,12 @@ package main import ( "fmt" - "github.com/sirupsen/logrus" "log" "os" "path/filepath" "github.com/kardianos/service" + "github.com/sirupsen/logrus" "github.com/slackhq/nebula" ) diff --git a/cmd/nebula/main.go b/cmd/nebula/main.go index 9e23aca..5697592 100644 --- a/cmd/nebula/main.go +++ b/cmd/nebula/main.go @@ -3,9 +3,9 @@ package main import ( "flag" "fmt" - "github.com/sirupsen/logrus" "os" + "github.com/sirupsen/logrus" "github.com/slackhq/nebula" ) diff --git a/config.go b/config.go index 558c4c5..7b9df19 100644 --- a/config.go +++ b/config.go @@ -3,9 +3,6 @@ package nebula import ( "errors" "fmt" - "github.com/imdario/mergo" - "github.com/sirupsen/logrus" - "gopkg.in/yaml.v2" "io/ioutil" "net" "os" @@ -17,6 +14,10 @@ import ( "strings" "syscall" "time" + + "github.com/imdario/mergo" + "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" ) type Config struct { diff --git a/config_test.go b/config_test.go index 91485af..dc617b4 100644 --- a/config_test.go +++ b/config_test.go @@ -1,12 +1,13 @@ package nebula import ( - "github.com/stretchr/testify/assert" "io/ioutil" "os" "path/filepath" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestConfig_Load(t *testing.T) { diff --git a/firewall.go b/firewall.go index 8639aec..fd25098 100644 --- a/firewall.go +++ b/firewall.go @@ -1,19 +1,18 @@ package nebula import ( + "crypto/sha256" "encoding/binary" + "encoding/hex" "encoding/json" + "errors" "fmt" "net" - "sync" - "time" - - "crypto/sha256" - "encoding/hex" - "errors" "reflect" "strconv" "strings" + "sync" + "time" "github.com/rcrowley/go-metrics" "github.com/slackhq/nebula/cert" diff --git a/handshake_ix.go b/handshake_ix.go index e44df11..f529dbd 100644 --- a/handshake_ix.go +++ b/handshake_ix.go @@ -1,11 +1,10 @@ package nebula import ( + "bytes" "sync/atomic" "time" - "bytes" - "github.com/flynn/noise" "github.com/golang/protobuf/proto" ) diff --git a/header_test.go b/header_test.go index 02f71e0..b2df090 100644 --- a/header_test.go +++ b/header_test.go @@ -1,9 +1,10 @@ package nebula import ( - "github.com/stretchr/testify/assert" "reflect" "testing" + + "github.com/stretchr/testify/assert" ) type headerTest struct { diff --git a/lighthouse_test.go b/lighthouse_test.go index d5ce880..19b2891 100644 --- a/lighthouse_test.go +++ b/lighthouse_test.go @@ -4,7 +4,7 @@ import ( "net" "testing" - proto "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/proto" "github.com/stretchr/testify/assert" ) diff --git a/logger_test.go b/logger_test.go index 2cd82d4..1594fb9 100644 --- a/logger_test.go +++ b/logger_test.go @@ -2,9 +2,10 @@ package nebula import ( "errors" + "testing" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" - "testing" ) type TestLogWriter struct { diff --git a/main.go b/main.go index 470804e..a89f0e8 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,6 @@ package nebula import ( "encoding/binary" "fmt" - "github.com/sirupsen/logrus" - "github.com/slackhq/nebula/sshd" - "gopkg.in/yaml.v2" "net" "os" "os/signal" @@ -13,6 +10,10 @@ import ( "strings" "syscall" "time" + + "github.com/sirupsen/logrus" + "github.com/slackhq/nebula/sshd" + "gopkg.in/yaml.v2" ) // The caller should provide a real logger, we have one just in case diff --git a/outside.go b/outside.go index add7c62..166bd5d 100644 --- a/outside.go +++ b/outside.go @@ -2,18 +2,14 @@ package nebula import ( "encoding/binary" + "errors" + "fmt" + "time" "github.com/flynn/noise" "github.com/golang/protobuf/proto" "github.com/sirupsen/logrus" "github.com/slackhq/nebula/cert" - // "github.com/google/gopacket" - // "github.com/google/gopacket/layers" - // "encoding/binary" - "errors" - "fmt" - "time" - "golang.org/x/net/ipv4" ) diff --git a/outside_test.go b/outside_test.go index c4dee27..6dd8bdc 100644 --- a/outside_test.go +++ b/outside_test.go @@ -1,10 +1,11 @@ package nebula import ( - "github.com/stretchr/testify/assert" - "golang.org/x/net/ipv4" "net" "testing" + + "github.com/stretchr/testify/assert" + "golang.org/x/net/ipv4" ) func Test_newPacket(t *testing.T) { diff --git a/punchy_test.go b/punchy_test.go index 98ed608..145dbe0 100644 --- a/punchy_test.go +++ b/punchy_test.go @@ -1,9 +1,10 @@ package nebula import ( - "github.com/stretchr/testify/assert" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestNewPunchyFromConfig(t *testing.T) { diff --git a/ssh.go b/ssh.go index 9b1c88d..2c071c2 100644 --- a/ssh.go +++ b/ssh.go @@ -5,8 +5,6 @@ import ( "encoding/json" "flag" "fmt" - "github.com/sirupsen/logrus" - "github.com/slackhq/nebula/sshd" "io/ioutil" "net" "os" @@ -14,6 +12,9 @@ import ( "runtime/pprof" "strings" "syscall" + + "github.com/sirupsen/logrus" + "github.com/slackhq/nebula/sshd" ) type sshListHostMapFlags struct { diff --git a/sshd/command.go b/sshd/command.go index a1568fe..8296ef4 100644 --- a/sshd/command.go +++ b/sshd/command.go @@ -4,9 +4,10 @@ import ( "errors" "flag" "fmt" - "github.com/armon/go-radix" "sort" "strings" + + "github.com/armon/go-radix" ) // CommandFlags is a function called before help or command execution to parse command line flags diff --git a/sshd/server.go b/sshd/server.go index c93f597..7f6da3b 100644 --- a/sshd/server.go +++ b/sshd/server.go @@ -2,10 +2,11 @@ package sshd import ( "fmt" + "net" + "github.com/armon/go-radix" "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" - "net" ) type SSHServer struct { diff --git a/sshd/session.go b/sshd/session.go index 2ba1c0f..864678a 100644 --- a/sshd/session.go +++ b/sshd/session.go @@ -2,13 +2,14 @@ package sshd import ( "fmt" + "sort" + "strings" + "github.com/anmitsu/go-shlex" "github.com/armon/go-radix" "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/terminal" - "sort" - "strings" ) type session struct { diff --git a/stats.go b/stats.go index dfc1931..fec1189 100644 --- a/stats.go +++ b/stats.go @@ -3,15 +3,16 @@ package nebula import ( "errors" "fmt" - "github.com/cyberdelia/go-metrics-graphite" - mp "github.com/nbrownus/go-metrics-prometheus" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/rcrowley/go-metrics" "log" "net" "net/http" "time" + + graphite "github.com/cyberdelia/go-metrics-graphite" + mp "github.com/nbrownus/go-metrics-prometheus" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/rcrowley/go-metrics" ) func startStats(c *Config, configTest bool) error { diff --git a/timeout_test.go b/timeout_test.go index 6b862a4..2f4ceb1 100644 --- a/timeout_test.go +++ b/timeout_test.go @@ -1,9 +1,10 @@ package nebula import ( - "github.com/stretchr/testify/assert" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestNewTimerWheel(t *testing.T) { diff --git a/tun_darwin.go b/tun_darwin.go index aff7afc..cccc489 100644 --- a/tun_darwin.go +++ b/tun_darwin.go @@ -4,10 +4,11 @@ package nebula import ( "fmt" - "github.com/songgao/water" "net" "os/exec" "strconv" + + "github.com/songgao/water" ) type Tun struct {