Skip to content

chore: format go code using gofumpt #172

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

Merged
merged 11 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 26 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,29 @@ jobs:

- name: Check for unstaged files
run: git diff --exit-code
fmt:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Echo Go Cache Paths
id: go-cache-paths
run: |
echo "GOCACHE=$(go env GOCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
echo "GOMODCACHE=$(go env GOMODCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT

- name: Go Build Cache
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.GOCACHE }}
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.**', '**.go') }}

- uses: actions/setup-go@v3
with:
go-version: "~1.21"

- name: Check format
run: test -z $(go run mvdan.cc/gofumpt -l .)
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
GOARCH := $(shell go env GOARCH)
PWD=$(shell pwd)

fmt: *.go
go run mvdan.cc/gofumpt -w .

develop:
./scripts/develop.sh

Expand Down
6 changes: 3 additions & 3 deletions devcontainer/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *Spec) Compile(fs billy.Filesystem, devcontainerDir, scratchDir string,
if s.Image != "" {
// We just write the image to a file and return it.
dockerfilePath := filepath.Join(scratchDir, "Dockerfile")
file, err := fs.OpenFile(dockerfilePath, os.O_CREATE|os.O_WRONLY, 0644)
file, err := fs.OpenFile(dockerfilePath, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, fmt.Errorf("open dockerfile: %w", err)
}
Expand Down Expand Up @@ -228,7 +228,7 @@ func (s *Spec) compileFeatures(fs billy.Filesystem, devcontainerDir, scratchDir
}

featuresDir := filepath.Join(scratchDir, "features")
err := fs.MkdirAll(featuresDir, 0644)
err := fs.MkdirAll(featuresDir, 0o644)
if err != nil {
return "", nil, fmt.Errorf("create features directory: %w", err)
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func (s *Spec) compileFeatures(fs billy.Filesystem, devcontainerDir, scratchDir
featureSha := md5.Sum([]byte(featureRefRaw))
featureName := filepath.Base(featureRef)
featureDir := filepath.Join(featuresDir, fmt.Sprintf("%s-%x", featureName, featureSha[:4]))
if err := fs.MkdirAll(featureDir, 0644); err != nil {
if err := fs.MkdirAll(featureDir, 0o644); err != nil {
return "", nil, err
}
spec, err := features.Extract(fs, devcontainerDir, featureDir, featureRefRaw)
Expand Down
4 changes: 2 additions & 2 deletions devcontainer/devcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ func TestCompileDevContainer(t *testing.T) {
},
}
dcDir := "/workspaces/coder/.devcontainer"
err := fs.MkdirAll(dcDir, 0755)
err := fs.MkdirAll(dcDir, 0o755)
require.NoError(t, err)
file, err := fs.OpenFile(filepath.Join(dcDir, "Dockerfile"), os.O_CREATE|os.O_WRONLY, 0644)
file, err := fs.OpenFile(filepath.Join(dcDir, "Dockerfile"), os.O_CREATE|os.O_WRONLY, 0o644)
require.NoError(t, err)
_, err = io.WriteString(file, "FROM localhost:5000/envbuilder-test-ubuntu:latest")
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions devcontainer/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func extractFromImage(fs billy.Filesystem, directory, reference string) error {
path := filepath.Join(directory, header.Name)
switch header.Typeflag {
case tar.TypeDir:
err = fs.MkdirAll(path, 0755)
err = fs.MkdirAll(path, 0o755)
if err != nil {
return fmt.Errorf("mkdir %s: %w", path, err)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func Extract(fs billy.Filesystem, devcontainerDir, directory, reference string)
if ok {
// For some reason the filesystem abstraction doesn't support chmod.
// https://github.com/src-d/go-billy/issues/56
err = chmodder.Chmod(installScriptPath, 0755)
err = chmodder.Chmod(installScriptPath, 0o755)
}
if err != nil {
return nil, fmt.Errorf("chmod install.sh: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions envbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func Run(ctx context.Context, options Options) error {
if err != nil {
return fmt.Errorf("parse docker config: %w", err)
}
err = os.WriteFile(filepath.Join(MagicDir, "config.json"), decoded, 0644)
err = os.WriteFile(filepath.Join(MagicDir, "config.json"), decoded, 0o644)
if err != nil {
return fmt.Errorf("write docker config: %w", err)
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func Run(ctx context.Context, options Options) error {

defaultBuildParams := func() (*devcontainer.Compiled, error) {
dockerfile := filepath.Join(MagicDir, "Dockerfile")
file, err := options.Filesystem.OpenFile(dockerfile, os.O_CREATE|os.O_WRONLY, 0644)
file, err := options.Filesystem.OpenFile(dockerfile, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -705,7 +705,7 @@ func Run(ctx context.Context, options Options) error {
endStage("👤 Updated the ownership of the workspace!")
}

err = os.MkdirAll(options.WorkspaceFolder, 0755)
err = os.MkdirAll(options.WorkspaceFolder, 0o755)
if err != nil {
return fmt.Errorf("create workspace folder: %w", err)
}
Expand Down Expand Up @@ -962,7 +962,7 @@ func createPostStartScript(path string, postStartCommand devcontainer.LifecycleS
}
defer postStartScript.Close()

if err := postStartScript.Chmod(0755); err != nil {
if err := postStartScript.Chmod(0o755); err != nil {
return err
}

Expand Down
12 changes: 6 additions & 6 deletions envbuilder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestFindDevcontainerJSON(t *testing.T) {

// given
fs := memfs.New()
err := fs.MkdirAll("/workspace/.devcontainer", 0600)
err := fs.MkdirAll("/workspace/.devcontainer", 0o600)
require.NoError(t, err)

// when
Expand All @@ -50,7 +50,7 @@ func TestFindDevcontainerJSON(t *testing.T) {

// given
fs := memfs.New()
err := fs.MkdirAll("/workspace/.devcontainer", 0600)
err := fs.MkdirAll("/workspace/.devcontainer", 0o600)
require.NoError(t, err)
fs.Create("/workspace/.devcontainer/devcontainer.json")

Expand All @@ -71,7 +71,7 @@ func TestFindDevcontainerJSON(t *testing.T) {

// given
fs := memfs.New()
err := fs.MkdirAll("/workspace/experimental-devcontainer", 0600)
err := fs.MkdirAll("/workspace/experimental-devcontainer", 0o600)
require.NoError(t, err)
fs.Create("/workspace/experimental-devcontainer/devcontainer.json")

Expand All @@ -93,7 +93,7 @@ func TestFindDevcontainerJSON(t *testing.T) {

// given
fs := memfs.New()
err := fs.MkdirAll("/workspace/.devcontainer", 0600)
err := fs.MkdirAll("/workspace/.devcontainer", 0o600)
require.NoError(t, err)
fs.Create("/workspace/.devcontainer/experimental.json")

Expand All @@ -115,7 +115,7 @@ func TestFindDevcontainerJSON(t *testing.T) {

// given
fs := memfs.New()
err := fs.MkdirAll("/workspace", 0600)
err := fs.MkdirAll("/workspace", 0o600)
require.NoError(t, err)
fs.Create("/workspace/devcontainer.json")

Expand All @@ -136,7 +136,7 @@ func TestFindDevcontainerJSON(t *testing.T) {

// given
fs := memfs.New()
err := fs.MkdirAll("/workspace/.devcontainer/sample", 0600)
err := fs.MkdirAll("/workspace/.devcontainer/sample", 0o600)
require.NoError(t, err)
fs.Create("/workspace/.devcontainer/sample/devcontainer.json")

Expand Down
2 changes: 1 addition & 1 deletion git.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func CloneRepo(ctx context.Context, opts CloneRepoOptions) (bool, error) {
}
}

err = opts.Storage.MkdirAll(opts.Path, 0755)
err = opts.Storage.MkdirAll(opts.Path, 0o755)
if err != nil {
return false, fmt.Errorf("mkdir %q: %w", opts.Path, err)
}
Expand Down
2 changes: 1 addition & 1 deletion git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestCloneRepo(t *testing.T) {

func mustRead(t *testing.T, fs billy.Filesystem, path string) string {
t.Helper()
f, err := fs.OpenFile(path, os.O_RDONLY, 0644)
f, err := fs.OpenFile(path, os.O_RDONLY, 0o644)
require.NoError(t, err)
content, err := io.ReadAll(f)
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ require (
gvisor.dev/gvisor v0.0.0-20240301031223-3172bc04679b // indirect
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a // indirect
inet.af/peercred v0.0.0-20210906144145-0893ea02156a // indirect
mvdan.cc/gofumpt v0.6.0 // indirect
nhooyr.io/websocket v1.8.7 // indirect
storj.io/drpc v0.0.33-0.20230420154621-9716137f6037 // indirect
tailscale.com v1.46.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBd
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA=
github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa h1:RDBNVkRviHZtvDvId8XSGPu3rmpmSe+wKRcEWNgsfWU=
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa/go.mod h1:KnogPXtdwXqoenmZCw6S+25EAm2MkxbG0deNDu4cbSA=
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
Expand Down Expand Up @@ -735,6 +736,7 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rootless-containers/rootlesskit v1.1.0 h1:cRaRIYxY8oce4eE/zeAUZhgKu/4tU1p9YHN4+suwV7M=
github.com/rootless-containers/rootlesskit v1.1.0/go.mod h1:H+o9ndNe7tS91WqU0/+vpvc+VaCd7TCIWaJjnV0ujUo=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -1123,6 +1125,8 @@ inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a h1:1XCVEdxrvL6c0TGOhecLuB7U9z
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a/go.mod h1:e83i32mAQOW1LAqEIweALsuK2Uw4mhQadA5r7b0Wobo=
inet.af/peercred v0.0.0-20210906144145-0893ea02156a h1:qdkS8Q5/i10xU2ArJMKYhVa1DORzBfYS/qA2UK2jheg=
inet.af/peercred v0.0.0-20210906144145-0893ea02156a/go.mod h1:FjawnflS/udxX+SvpsMgZfdqx2aykOlkISeAsADi5IU=
mvdan.cc/gofumpt v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
mvdan.cc/gofumpt v0.6.0/go.mod h1:4L0wf+kgIPZtcCWXynNS2e6bhmj73umwnuXSZarixzA=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
Expand Down
4 changes: 2 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestBuildIgnoreVarRunSecrets(t *testing.T) {
},
})
dir := t.TempDir()
err := os.WriteFile(filepath.Join(dir, "secret"), []byte("test"), 0644)
err := os.WriteFile(filepath.Join(dir, "secret"), []byte("test"), 0o644)
require.NoError(t, err)
ctr, err := runEnvbuilder(t, options{
env: []string{
Expand Down Expand Up @@ -360,6 +360,7 @@ func TestBuildFromDevcontainerInSubfolder(t *testing.T) {
output := execContainer(t, ctr, "echo hello")
require.Equal(t, "hello", strings.TrimSpace(output))
}

func TestBuildFromDevcontainerInRoot(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -772,7 +773,6 @@ func setupPassthroughRegistry(t *testing.T, image string, auth *registryAuth) st
}

proxy.ServeHTTP(w, r)

}))
return fmt.Sprintf("%s/%s", strings.TrimPrefix(srv.URL, "http://"), image)
}
Expand Down
3 changes: 1 addition & 2 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestCLIOutput(t *testing.T) {
require.NoError(t, err)

if *updateCLIOutputGoldenFiles {
err = os.WriteFile("testdata/options.golden", b.Stdout.Bytes(), 0644)
err = os.WriteFile("testdata/options.golden", b.Stdout.Bytes(), 0o644)
require.NoError(t, err)
t.Logf("updated golden file: testdata/options.golden")
} else {
Expand All @@ -111,7 +111,6 @@ func runCLI() envbuilder.Options {
i := cmd.Invoke().WithOS()
fakeIO(i)
err := i.Run()

if err != nil {
panic("failed to run CLI: " + err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/docsgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
mkd := "\n## Environment Variables\n\n" + options.Markdown()
modifiedContent := readmeContent[:startIndex+len(startSection)] + mkd + readmeContent[endIndex:]

err = os.WriteFile(readmePath, []byte(modifiedContent), 0644)
err = os.WriteFile(readmePath, []byte(modifiedContent), 0o644)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion testutil/gittest/gittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func NewRepo(t *testing.T, fs billy.Filesystem, commits ...CommitFunc) *git.Repo
// WriteFile writes a file to the filesystem.
func WriteFile(t *testing.T, fs billy.Filesystem, path, content string) {
t.Helper()
file, err := fs.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
file, err := fs.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
require.NoError(t, err)
_, err = file.Write([]byte(content))
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion testutil/registrytest/registrytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func WriteContainer(t *testing.T, serverURL, containerRef, mediaType string, fil
require.NoError(t, err)
}
err := wtr.WriteHeader(&tar.Header{
Mode: 0777,
Mode: 0o777,
Name: name,
Typeflag: tar.TypeReg,
Size: int64(len(data)),
Expand Down
7 changes: 7 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build tools

package envbuilder

import (
_ "mvdan.cc/gofumpt"
)
Loading