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

Use gofumpt if available, and enable gofumpt linter #3798

Merged
merged 2 commits into from
Nov 4, 2022
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ linters:
- depguard
- dogsled
- gocyclo
- gofumpt
- goimports
- gosec
- gosimple
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ mind when nudging others to comply.

The rules:

1. All code should be formatted with `gofmt -s`.
1. All code should be formatted with `gofumpt` (preferred) or `gofmt -s`.
2. All code should pass the default levels of
[`golint`](https://github.com/golang/lint).
3. All code should follow the guidelines covered in [Effective
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
# Sets the name of the company that produced the windows binary.
PACKAGER_NAME ?=

# The repository doesn't have a go.mod, but "go list", and "gotestsum"
# expect to be run from a module.
GO111MODULE=auto
export GO111MODULE

all: binary

_:=$(shell ./scripts/warn-outside-container $(MAKECMDGOALS))
Expand Down Expand Up @@ -45,8 +50,12 @@ shellcheck: ## run shellcheck validation
find scripts/ contrib/completion/bash -type f | grep -v scripts/winresources | grep -v '.*.ps1' | xargs shellcheck

.PHONY: fmt
fmt: ## run gofmt
go list -f {{.Dir}} ./... | xargs gofmt -w -s -d
fmt: ## run gofumpt (if present) or gofmt
@if command -v gofumpt > /dev/null; then \
gofumpt -w -d -lang=1.19 . ; \
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I need to add an explicit -lang=1.19, otherwise it doesn't pick up the go version for which to format. Probably because we don't have a go.mod

else \
go list -f {{.Dir}} ./... | xargs gofmt -w -s -d ; \
fi

.PHONY: binary
binary: ## build executable for Linux
Expand Down
8 changes: 4 additions & 4 deletions cli-plugins/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func TestGetPlugin(t *testing.T) {
dir := fs.NewDir(t, t.Name(),
fs.WithFile("docker-bbb", `
#!/bin/sh
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0777)),
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
fs.WithFile("docker-aaa", `
#!/bin/sh
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0777)),
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
)
defer dir.Remove()

Expand All @@ -109,10 +109,10 @@ func TestListPluginsIsSorted(t *testing.T) {
dir := fs.NewDir(t, t.Name(),
fs.WithFile("docker-bbb", `
#!/bin/sh
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0777)),
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
fs.WithFile("docker-aaa", `
#!/bin/sh
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0777)),
echo '{"SchemaVersion":"0.1.0"}'`, fs.WithMode(0o777)),
)
defer dir.Remove()

Expand Down
2 changes: 1 addition & 1 deletion cli/command/context/export-import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestExportExistingFile(t *testing.T) {
contextFile := filepath.Join(t.TempDir(), "exported")
cli := makeFakeCli(t)
cli.ErrBuffer().Reset()
assert.NilError(t, os.WriteFile(contextFile, []byte{}, 0644))
assert.NilError(t, os.WriteFile(contextFile, []byte{}, 0o644))
err := RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
assert.Assert(t, os.IsExist(err))
}
2 changes: 1 addition & 1 deletion cli/command/context/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func writeTo(dockerCli command.Cli, reader io.Reader, dest string) error {
}
writer = dockerCli.Out()
} else {
f, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
f, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func runBuild(dockerCli command.Cli, options buildOptions) error {
if imageID == "" {
return errors.Errorf("Server did not provide an image ID. Cannot write %s", options.imageIDFile)
}
if err := os.WriteFile(options.imageIDFile, []byte(imageID), 0666); err != nil {
if err := os.WriteFile(options.imageIDFile, []byte(imageID), 0o666); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/image/build/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCl
randomName: func(_ string, _ *tar.Header, _ io.Reader) (*tar.Header, []byte, error) {
header := &tar.Header{
Name: randomName,
Mode: 0600,
Mode: 0o600,
ModTime: now,
Typeflag: tar.TypeReg,
AccessTime: now,
Expand All @@ -397,7 +397,7 @@ func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCl
if h == nil {
h = &tar.Header{
Name: ".dockerignore",
Mode: 0600,
Mode: 0o600,
ModTime: now,
Typeflag: tar.TypeReg,
AccessTime: now,
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/build/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func createTestTempDir(t *testing.T) string {
func createTestTempFile(t *testing.T, dir, filename, contents string) string {
t.Helper()
filePath := filepath.Join(dir, filename)
err := os.WriteFile(filePath, []byte(contents), 0777)
err := os.WriteFile(filePath, []byte(contents), 0o777)
assert.NilError(t, err)
return filePath
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/image/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ func TestRunBuildFromLocalGitHubDir(t *testing.T) {
t.Setenv("DOCKER_BUILDKIT", "0")

buildDir := filepath.Join(t.TempDir(), "github.com", "docker", "no-such-repository")
err := os.MkdirAll(buildDir, 0777)
err := os.MkdirAll(buildDir, 0o777)
assert.NilError(t, err)
err = os.WriteFile(filepath.Join(buildDir, "Dockerfile"), []byte("FROM busybox\n"), 0644)
err = os.WriteFile(filepath.Join(buildDir, "Dockerfile"), []byte("FROM busybox\n"), 0o644)
assert.NilError(t, err)

client := test.NewFakeCli(&fakeClient{})
Expand Down
4 changes: 2 additions & 2 deletions cli/command/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestSetConfigsWithCredSpecAndConfigs(t *testing.T) {
// these are the default field values
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
}), "expected configRefs to contain bar config")
}
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestSetConfigsOnlyConfigs(t *testing.T) {
// these are the default field values
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
}))
}
Expand Down
6 changes: 3 additions & 3 deletions cli/command/service/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ func TestUpdateGetUpdatedConfigs(t *testing.T) {
Name: "foo",
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
},
"barRef": {
Expand All @@ -1076,7 +1076,7 @@ func TestUpdateGetUpdatedConfigs(t *testing.T) {
Name: "bar",
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
},
"bazRef": {
Expand All @@ -1086,7 +1086,7 @@ func TestUpdateGetUpdatedConfigs(t *testing.T) {
Name: "baz",
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
},
"credRef": {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/trust/key_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

const (
nonOwnerReadWriteMask = 0077
nonOwnerReadWriteMask = 0o077
)

type keyLoadOptions struct {
Expand Down
10 changes: 5 additions & 5 deletions cli/command/trust/key_load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,35 +175,35 @@ func TestLoadKeyTooPermissive(t *testing.T) {
func testLoadKeyTooPermissive(t *testing.T, privKeyFixture []byte) {
privKeyDir := t.TempDir()
privKeyFilepath := filepath.Join(privKeyDir, "privkey477.pem")
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0477))
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o477))

// import the key to our keyStorageDir
_, err := getPrivKeyBytesFromPath(privKeyFilepath)
expected := fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
assert.Error(t, err, expected)

privKeyFilepath = filepath.Join(privKeyDir, "privkey667.pem")
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0677))
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o677))

_, err = getPrivKeyBytesFromPath(privKeyFilepath)
expected = fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
assert.Error(t, err, expected)

privKeyFilepath = filepath.Join(privKeyDir, "privkey777.pem")
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0777))
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o777))

_, err = getPrivKeyBytesFromPath(privKeyFilepath)
expected = fmt.Sprintf("private key file %s must not be readable or writable by others", privKeyFilepath)
assert.Error(t, err, expected)

privKeyFilepath = filepath.Join(privKeyDir, "privkey400.pem")
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0400))
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o400))

_, err = getPrivKeyBytesFromPath(privKeyFilepath)
assert.NilError(t, err)

privKeyFilepath = filepath.Join(privKeyDir, "privkey600.pem")
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0600))
assert.NilError(t, os.WriteFile(privKeyFilepath, privKeyFixture, 0o600))

_, err = getPrivKeyBytesFromPath(privKeyFilepath)
assert.NilError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/trust/signer_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func ingestPublicKeys(pubKeyPaths []string) ([]data.PublicKey, error) {
pubKeys := []data.PublicKey{}
for _, pubKeyPath := range pubKeyPaths {
// Read public key bytes from PEM file, limit to 1 KiB
pubKeyFile, err := os.OpenFile(pubKeyPath, os.O_RDONLY, 0666)
pubKeyFile, err := os.OpenFile(pubKeyPath, os.O_RDONLY, 0o666)
if err != nil {
return nil, errors.Wrap(err, "unable to read public key from file")
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func TestValidateOutputPath(t *testing.T) {
basedir := t.TempDir()
dir := filepath.Join(basedir, "dir")
notexist := filepath.Join(basedir, "notexist")
err := os.MkdirAll(dir, 0755)
err := os.MkdirAll(dir, 0o755)
assert.NilError(t, err)
file := filepath.Join(dir, "file")
err = os.WriteFile(file, []byte("hi"), 0644)
err = os.WriteFile(file, []byte("hi"), 0o644)
assert.NilError(t, err)
testcases := []struct {
path string
Expand Down
2 changes: 1 addition & 1 deletion cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func convertFileObject(
}
mode := config.Mode
if mode == nil {
mode = uint32Ptr(0444)
mode = uint32Ptr(0o444)
}

return swarmReferenceObject{
Expand Down
14 changes: 7 additions & 7 deletions cli/compose/convert/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func TestConvertFileObject(t *testing.T) {
Target: "target",
UID: "user",
GID: "group",
Mode: uint32Ptr(0644),
Mode: uint32Ptr(0o644),
}
swarmRef, err := convertFileObject(namespace, config, lookupConfig)
assert.NilError(t, err)
Expand All @@ -438,7 +438,7 @@ func TestConvertFileObject(t *testing.T) {
Name: config.Target,
UID: config.UID,
GID: config.GID,
Mode: os.FileMode(0644),
Mode: os.FileMode(0o644),
},
}
assert.Check(t, is.DeepEqual(expected, swarmRef))
Expand All @@ -463,7 +463,7 @@ func TestConvertFileObjectDefaults(t *testing.T) {
Name: config.Source,
UID: "0",
GID: "0",
Mode: os.FileMode(0444),
Mode: os.FileMode(0o444),
},
}
assert.Check(t, is.DeepEqual(expected, swarmRef))
Expand Down Expand Up @@ -511,7 +511,7 @@ func TestConvertServiceSecrets(t *testing.T) {
Name: "bar_secret",
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
},
{
Expand All @@ -520,7 +520,7 @@ func TestConvertServiceSecrets(t *testing.T) {
Name: "foo_secret",
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
},
}
Expand Down Expand Up @@ -570,7 +570,7 @@ func TestConvertServiceConfigs(t *testing.T) {
Name: "bar_config",
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
},
{
Expand All @@ -583,7 +583,7 @@ func TestConvertServiceConfigs(t *testing.T) {
Name: "foo_config",
UID: "0",
GID: "0",
Mode: 0444,
Mode: 0o444,
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
Target: "/my_config",
UID: "103",
GID: "103",
Mode: uint32Ptr(0440),
Mode: uint32Ptr(0o440),
},
},
ContainerName: "my-web-container",
Expand Down Expand Up @@ -342,7 +342,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
Target: "my_secret",
UID: "103",
GID: "103",
Mode: uint32Ptr(0440),
Mode: uint32Ptr(0o440),
},
},
SecurityOpt: []string{
Expand Down
Loading