Skip to content

Commit

Permalink
Merge pull request #3096 from oasisprotocol/kostko/feature/gofumpt
Browse files Browse the repository at this point in the history
go: Use gofumpt instead of gofmt to format Go
  • Loading branch information
kostko authored Jul 10, 2020
2 parents d52975a + ffc1f7e commit c56a606
Show file tree
Hide file tree
Showing 163 changed files with 497 additions and 534 deletions.
1 change: 1 addition & 0 deletions .changelog/3095.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go: Use gofumpt instead of gofmt to format Go
8 changes: 6 additions & 2 deletions docker/development/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ FROM ubuntu:18.04
ARG GO_VERSION=1.14.3
ARG GO_PROTOC_VERSION=3.6.1
ARG GO_PROTOC_GEN_GO_VERSION=1.21.0
ARG GOLANGCILINT_VERSION=1.23.6
ARG GOLANGCILINT_VERSION=1.28.2
ARG GOCOVMERGE_VERSION=b5bfa59ec0adc420475f97f89b58045c721d761c
ARG GOFUMPT_VERSION=abc0db2c416aca0f60ea33c23c76665f6e7ba0b6
ARG RUST_NIGHTLY_VERSION=2020-06-09

ARG DEBIAN_FRONTEND=noninteractive
Expand Down Expand Up @@ -64,7 +65,10 @@ RUN wget https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz && \
curl -sfL -o nancy https://github.com/sonatype-nexus-community/nancy/releases/download/v0.0.39/nancy-linux.amd64-v0.0.39 && \
echo 'eb3a93d7db24d115e6e67a2b17dba402978f856c891fa6f0ff09ad17ac53ebb5 nancy' | sha256sum -c && \
mv nancy /go/bin/nancy && \
chmod +x /go/bin/nancy
chmod +x /go/bin/nancy && \
# Install gofumpt for code formatting.
GO111MODULE=on go get mvdan.cc/gofumpt@${GOFUMPT_VERSION} && \
GO111MODULE=on go get mvdan.cc/gofumpt/gofumports@${GOFUMPT_VERSION}

# Install bubblewrap (we need version 0.3.3 which is not available for 18.04).
RUN wget http://archive.ubuntu.com/ubuntu/pool/main/b/bubblewrap/bubblewrap_0.3.3-2_amd64.deb && \
Expand Down
14 changes: 14 additions & 0 deletions docs/setup/prerequisites.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ Core:
rustup target add x86_64-fortanix-unknown-sgx
```
* (**OPTIONAL**) [gofumpt and gofumports].
Required if you plan to change any of the Go code in order for automated code
formatting (`make fmt`) to work.
Download and install it with:
```
export GOFUMPT_VERSION=abc0db2c416aca0f60ea33c23c76665f6e7ba0b6
GO111MODULE=on ${OASIS_GO:-go} get mvdan.cc/gofumpt@${GOFUMPT_VERSION}
GO111MODULE=on ${OASIS_GO:-go} get mvdan.cc/gofumpt/gofumports@${GOFUMPT_VERSION}
```
* (**OPTIONAL**) [protoc-gen-go].
Download and install it with:
Expand Down Expand Up @@ -173,6 +186,7 @@ where the code has been checked out.
[rust-toolchain-precedence]:
https://github.com/rust-lang/rustup/blob/master/README.md#override-precedence
[Fortanix Rust EDP]: https://edp.fortanix.com
[gofumpt and gofumports]: https://github.com/mvdan/gofumpt
[protoc-gen-go]: https://github.com/golang/protobuf
## Using the development Docker image
Expand Down
2 changes: 1 addition & 1 deletion go/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ linters:
- errcheck
- goconst
- gocyclo
- gofmt
- gofumpt
- golint
- gas
- typecheck
Expand Down
6 changes: 4 additions & 2 deletions go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ all: build
generate:
@$(ECHO) "$(MAGENTA)*** Running go generate...$(OFF)"
@$(GO) generate ./...
@$(MAKE) fmt

# Build.
# List of Go binaries to build.
Expand Down Expand Up @@ -48,8 +49,9 @@ $(test-vectors-targets):

# Format code.
fmt:
@$(ECHO) "$(CYAN)*** Running go fmt...$(OFF)"
@$(GO) fmt ./...
@$(ECHO) "$(CYAN)*** Running Go formatters...$(OFF)"
@gofumpt -s -w .
@gofumports -w -local github.com/oasisprotocol/oasis-core .

# Lint.
lint:
Expand Down
1 change: 1 addition & 0 deletions go/common/crypto/mrae/deoxysii/asymmetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/oasisprotocol/deoxysii"

"github.com/oasisprotocol/oasis-core/go/common/crypto/mrae/api"
)

Expand Down
2 changes: 1 addition & 1 deletion go/common/crypto/signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (

pubPEMType = "ED25519 PUBLIC KEY"
sigPEMType = "ED25519 SIGNATURE"
filePerm = 0600
filePerm = 0o600
)

var (
Expand Down
2 changes: 1 addition & 1 deletion go/common/crypto/signature/signers/file/file_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
const (
privateKeyPemType = "ED25519 PRIVATE KEY"

filePerm = 0600
filePerm = 0o600

// SignerName is the name used to identify the file backed signer.
SignerName = "file"
Expand Down
4 changes: 2 additions & 2 deletions go/common/crypto/tls/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ func Save(certPath, keyPath string, cert *tls.Certificate) error {
return err
}

if err = ioutil.WriteFile(keyPath, keyPEM, 0600); err != nil {
if err = ioutil.WriteFile(keyPath, keyPEM, 0o600); err != nil {
return fmt.Errorf("tls: failed to write private key: %w", err)
}

if err = ioutil.WriteFile(certPath, certPEM, 0644); err != nil {
if err = ioutil.WriteFile(certPath, certPEM, 0o644); err != nil { // nolint: gosec
return fmt.Errorf("tls: failed to write certificate: %w", err)
}

Expand Down
12 changes: 8 additions & 4 deletions go/common/encoding/bech32/bech32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ func TestBIP173(t *testing.T) {
// HRP character out of range.
{"\x801eym55h", false, "decoding bech32 failed: invalid character in string: '\u0080'"},
// Overall max length exceeded.
{"an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
{
"an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
false,
"decoding bech32 failed: invalid bech32 string length 91"},
"decoding bech32 failed: invalid bech32 string length 91",
},
// No separator character.
{"pzry9x0s0muk", false, "decoding bech32 failed: invalid index of 1"},
// Empty HRP.
{"1pzry9x0s0muk", false, "decoding bech32 failed: invalid index of 1"},
// Invalid data character.
{"x1b4n0q5v", false,
"decoding bech32 failed: failed converting data to bytes: invalid character not part of charset: 98"},
{
"x1b4n0q5v", false,
"decoding bech32 failed: failed converting data to bytes: invalid character not part of charset: 98",
},
// Too short checksum.
{"li1dgmt3", false, "decoding bech32 failed: invalid index of 1"},
// Invalid character in checksum.
Expand Down
2 changes: 1 addition & 1 deletion go/common/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
const (
entityFilename = "entity.json"

fileMode = 0600
fileMode = 0o600
)

var (
Expand Down
14 changes: 7 additions & 7 deletions go/common/fuzz/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ type InterfaceFuzzer struct {

methodList []int

typeOverrides map[string]func()interface{}
typeOverrides map[string]func() interface{}
}

// OverrideType registers a custom callback for creating instances of a given type.
func (i *InterfaceFuzzer) OverrideType(typeName string, factory func()interface{}) {
func (i *InterfaceFuzzer) OverrideType(typeName string, factory func() interface{}) {
i.typeOverrides[typeName] = factory
}

Expand Down Expand Up @@ -110,11 +110,11 @@ FilterLoop:
func NewInterfaceFuzzer(instance interface{}) *InterfaceFuzzer {
val := reflect.ValueOf(instance)
ret := &InterfaceFuzzer{
instance: instance,
typeObject: val.Type(),
valObject: val,
typeOverrides: map[string]func()interface{}{
"context.Context": func()interface{}{
instance: instance,
typeObject: val.Type(),
valObject: val,
typeOverrides: map[string]func() interface{}{
"context.Context": func() interface{} {
return context.Background()
},
},
Expand Down
2 changes: 0 additions & 2 deletions go/common/grpc/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func UnaryServerInterceptor(authFunc AuthenticationFunction) grpc.UnaryServerInt
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {

overrideSrv, ok := info.Server.(ServerAuth)
if !ok {
// Server doesn't implement Authentication.
Expand Down Expand Up @@ -61,7 +60,6 @@ func StreamServerInterceptor(authFunc AuthenticationFunction) grpc.StreamServerI
stream grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler) error {

overrideSrv, ok := srv.(ServerAuth)
if !ok {
// Server doesn't implement Authentication.
Expand Down
1 change: 0 additions & 1 deletion go/common/grpc/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,4 @@ func testAuth(t *testing.T, testCase *testCase) {
}

}

}
2 changes: 1 addition & 1 deletion go/common/grpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func errorToGrpc(err error) error {
Code: int32(status.Code(err)),
Message: err.Error(),
Details: []*any.Any{
&any.Any{
{
// Double serialization seems ugly, but there is no way around
// it as the format for errors is predefined.
Value: cbor.Marshal(&grpcError{Module: module, Code: code}),
Expand Down
1 change: 0 additions & 1 deletion go/common/grpc/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func (p *proxy) handler(srv interface{}, stream grpc.ServerStream) error {
p.upstreamConn,
method,
)

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion go/common/grpc/testing/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func (s *pingServer) WatchPings(ctx context.Context, query *PingQuery) (<-chan *
case <-ctx.Done():
return
}

}
}()
typedCh := make(chan *PingResponse)
Expand Down
8 changes: 3 additions & 5 deletions go/common/grpc/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"google.golang.org/grpc"
)

var (
// ErrServiceClosed is the error returned when the wrapper receives a message for a service whose
// interceptor has been removed.
ErrServiceClosed = errors.New("grpc/wrapper: received message for wrapped service with deregistered wrapper")
)
// ErrServiceClosed is the error returned when the wrapper receives a message for a service whose
// interceptor has been removed.
var ErrServiceClosed = errors.New("grpc/wrapper: received message for wrapped service with deregistered wrapper")

type wrappedResponse struct {
resp interface{}
Expand Down
2 changes: 1 addition & 1 deletion go/common/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func LoadOrGenerate(dataDir string, signerFactory signature.SignerFactory, persi
return doLoadOrGenerate(dataDir, signerFactory, true, persistTLS)
}

func doLoadOrGenerate(dataDir string, signerFactory signature.SignerFactory, shouldGenerate bool, persistTLS bool) (*Identity, error) {
func doLoadOrGenerate(dataDir string, signerFactory signature.SignerFactory, shouldGenerate, persistTLS bool) (*Identity, error) {
var signers []signature.Signer
for _, v := range []struct {
role signature.SignerRole
Expand Down
10 changes: 4 additions & 6 deletions go/common/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ const (
ListingPathIndex uint32 = 0
)

var (
// ListingDerivationPath is the path used to list and connect to devices by address.
ListingDerivationPath = []uint32{
PathPurposeBIP44, ListingPathCoinType, ListingPathAccount, ListingPathChange, ListingPathIndex,
}
)
// ListingDerivationPath is the path used to list and connect to devices by address.
var ListingDerivationPath = []uint32{
PathPurposeBIP44, ListingPathCoinType, ListingPathAccount, ListingPathChange, ListingPathIndex,
}

// Device is a Ledger device.
type Device = ledger.LedgerOasis
Expand Down
2 changes: 1 addition & 1 deletion go/common/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Mkdir creates a directory iff it does not exist, and otherwise
// ensures that the filesystem permissions are sufficiently restrictive.
func Mkdir(d string) error {
const permDir = os.FileMode(0700)
const permDir = os.FileMode(0o700)

fi, err := os.Lstat(d)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion go/common/pubsub/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ func testSubscribeEx(t *testing.T) {
require.NotNil(t, sub.ch, "Subscription, inner channel")
require.Equal(t, sub.ch, callbackCh, "Callback channel != Subscription, inner channel")
}

}

func testNewBrokerEx(t *testing.T) {
Expand Down
74 changes: 39 additions & 35 deletions go/common/sgx/aesm/aesm_proto.pb.go

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

Loading

0 comments on commit c56a606

Please sign in to comment.