From 04b9ef69a1b6069bf4a3a4b8ca4ddfd5b9e86240 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Mon, 15 Nov 2021 10:37:56 -0700 Subject: [PATCH] run gofumpt --- config/config.go | 2 +- environment/allocations.go | 4 ++-- environment/docker.go | 6 ++++-- environment/docker/environment.go | 1 - loggers/cli/cli.go | 8 +++++--- remote/errors.go | 3 +-- remote/http_test.go | 1 - router/websocket/listeners.go | 1 - router/websocket/websocket.go | 2 +- server/errors.go | 6 ++---- server/filesystem/archive.go | 2 +- server/filesystem/filesystem.go | 14 +++++++------- server/filesystem/path.go | 4 ++-- server/filesystem/path_test.go | 2 +- server/install.go | 7 +++---- server/listeners.go | 1 - system/utils.go | 6 ++++-- 17 files changed, 34 insertions(+), 36 deletions(-) diff --git a/config/config.go b/config/config.go index 91cf03b..e01bf73 100644 --- a/config/config.go +++ b/config/config.go @@ -456,7 +456,7 @@ func FromFile(path string) error { return err } - if err := yaml.Unmarshal([]byte(b), c); err != nil { + if err := yaml.Unmarshal(b, c); err != nil { return err } diff --git a/environment/allocations.go b/environment/allocations.go index 42f7d98..71fbeb8 100644 --- a/environment/allocations.go +++ b/environment/allocations.go @@ -31,7 +31,7 @@ type Allocations struct { // // You'll want to use DockerBindings() if you need to re-map 127.0.0.1 to the Docker interface. func (a *Allocations) Bindings() nat.PortMap { - var out = nat.PortMap{} + out := nat.PortMap{} for ip, ports := range a.Mappings { for _, port := range ports { @@ -94,7 +94,7 @@ func (a *Allocations) DockerBindings() nat.PortMap { // To accomplish this, we'll just get the values from "DockerBindings" and then set them // to empty structs. Because why not. func (a *Allocations) Exposed() nat.PortSet { - var out = nat.PortSet{} + out := nat.PortSet{} for port := range a.DockerBindings() { out[port] = struct{}{} diff --git a/environment/docker.go b/environment/docker.go index 63bcabb..56f0295 100644 --- a/environment/docker.go +++ b/environment/docker.go @@ -14,8 +14,10 @@ import ( "github.com/pterodactyl/wings/config" ) -var _conce sync.Once -var _client *client.Client +var ( + _conce sync.Once + _client *client.Client +) // Docker returns a docker client to be used throughout the codebase. Once a // client has been created it will be returned for all subsequent calls to this diff --git a/environment/docker/environment.go b/environment/docker/environment.go index cacf6f7..eac9eb3 100644 --- a/environment/docker/environment.go +++ b/environment/docker/environment.go @@ -114,7 +114,6 @@ func (e *Environment) Events() *events.EventBus { // ID auto-assigned when the container is created. func (e *Environment) Exists() (bool, error) { _, err := e.client.ContainerInspect(context.Background(), e.Id) - if err != nil { // If this error is because the container instance wasn't found via Docker we // can safely ignore the error and just return false. diff --git a/loggers/cli/cli.go b/loggers/cli/cli.go index afccf03..d2e425d 100644 --- a/loggers/cli/cli.go +++ b/loggers/cli/cli.go @@ -15,9 +15,11 @@ import ( "github.com/mattn/go-colorable" ) -var Default = New(os.Stderr, true) -var bold = color2.New(color2.Bold) -var boldred = color2.New(color2.Bold, color2.FgRed) +var ( + Default = New(os.Stderr, true) + bold = color2.New(color2.Bold) + boldred = color2.New(color2.Bold, color2.FgRed) +) var Strings = [...]string{ log.DebugLevel: "DEBUG", diff --git a/remote/errors.go b/remote/errors.go index 08c2e97..a28e948 100644 --- a/remote/errors.go +++ b/remote/errors.go @@ -57,8 +57,7 @@ func (re *RequestError) StatusCode() int { return re.response.StatusCode } -type SftpInvalidCredentialsError struct { -} +type SftpInvalidCredentialsError struct{} func (ice SftpInvalidCredentialsError) Error() string { return "the credentials provided were invalid" diff --git a/remote/http_test.go b/remote/http_test.go index 3ecfef0..9e16d44 100644 --- a/remote/http_test.go +++ b/remote/http_test.go @@ -87,7 +87,6 @@ func TestPost(t *testing.T) { } c, _ := createTestClient(func(rw http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPost, r.Method) - }) r, err := c.Post(context.Background(), "/test", test) assert.NoError(t, err) diff --git a/router/websocket/listeners.go b/router/websocket/listeners.go index c3efc47..656dbee 100644 --- a/router/websocket/listeners.go +++ b/router/websocket/listeners.go @@ -35,7 +35,6 @@ func (h *Handler) registerListenerEvents(ctx context.Context) { go h.listenForExpiration(ctx) } - // ListenForExpiration checks the time to expiration on the JWT every 30 seconds // until the token has expired. If we are within 3 minutes of the token expiring, // send a notice over the socket that it is expiring soon. If it has expired, diff --git a/router/websocket/websocket.go b/router/websocket/websocket.go index 1a2487a..e9a4a63 100644 --- a/router/websocket/websocket.go +++ b/router/websocket/websocket.go @@ -369,7 +369,7 @@ func (h *Handler) HandleInbound(ctx context.Context, m Message) error { } case SendServerLogsEvent: { - ctx, cancel := context.WithTimeout(context.Background(), time.Second * 5) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() if running, _ := h.server.Environment.IsRunning(ctx); !running { return nil diff --git a/server/errors.go b/server/errors.go index cd14314..bd97862 100644 --- a/server/errors.go +++ b/server/errors.go @@ -12,8 +12,7 @@ var ( ErrServerIsRestoring = errors.New("server is currently being restored") ) -type crashTooFrequent struct { -} +type crashTooFrequent struct{} func (e *crashTooFrequent) Error() string { return "server has crashed too soon after the last detected crash" @@ -25,8 +24,7 @@ func IsTooFrequentCrashError(err error) bool { return ok } -type serverDoesNotExist struct { -} +type serverDoesNotExist struct{} func (e *serverDoesNotExist) Error() string { return "server does not exist on remote system" diff --git a/server/filesystem/archive.go b/server/filesystem/archive.go index 22aaebc..b5fec63 100644 --- a/server/filesystem/archive.go +++ b/server/filesystem/archive.go @@ -45,7 +45,7 @@ type Archive struct { // Create creates an archive at dst with all of the files defined in the // included files struct. func (a *Archive) Create(dst string) error { - f, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + f, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) if err != nil { return err } diff --git a/server/filesystem/filesystem.go b/server/filesystem/filesystem.go index b507709..7ece4de 100644 --- a/server/filesystem/filesystem.go +++ b/server/filesystem/filesystem.go @@ -85,7 +85,7 @@ func (fs *Filesystem) Touch(p string, flag int) (*os.File, error) { if err != nil { return nil, err } - f, err := os.OpenFile(cleaned, flag, 0644) + f, err := os.OpenFile(cleaned, flag, 0o644) if err == nil { return f, nil } @@ -97,7 +97,7 @@ func (fs *Filesystem) Touch(p string, flag int) (*os.File, error) { if _, err := os.Stat(filepath.Dir(cleaned)); errors.Is(err, os.ErrNotExist) { // Create the path leading up to the file we're trying to create, setting the final perms // on it as we go. - if err := os.MkdirAll(filepath.Dir(cleaned), 0755); err != nil { + if err := os.MkdirAll(filepath.Dir(cleaned), 0o755); err != nil { return nil, errors.Wrap(err, "server/filesystem: touch: failed to create directory tree") } if err := fs.Chown(filepath.Dir(cleaned)); err != nil { @@ -107,7 +107,7 @@ func (fs *Filesystem) Touch(p string, flag int) (*os.File, error) { o := &fileOpener{} // Try to open the file now that we have created the pathing necessary for it, and then // Chown that file so that the permissions don't mess with things. - f, err = o.open(cleaned, flag, 0644) + f, err = o.open(cleaned, flag, 0o644) if err != nil { return nil, errors.Wrap(err, "server/filesystem: touch: failed to open file with wait") } @@ -181,7 +181,7 @@ func (fs *Filesystem) CreateDirectory(name string, p string) error { if err != nil { return err } - return os.MkdirAll(cleaned, 0755) + return os.MkdirAll(cleaned, 0o755) } // Moves (or renames) a file or directory. @@ -210,7 +210,7 @@ func (fs *Filesystem) Rename(from string, to string) error { // Ensure that the directory we're moving into exists correctly on the system. Only do this if // we're not at the root directory level. if d != fs.Path() { - if mkerr := os.MkdirAll(d, 0755); mkerr != nil { + if mkerr := os.MkdirAll(d, 0o755); mkerr != nil { return errors.WithMessage(mkerr, "failed to create directory structure for file rename") } } @@ -377,7 +377,7 @@ func (fs *Filesystem) TruncateRootDirectory() error { if err := os.RemoveAll(fs.Path()); err != nil { return err } - if err := os.Mkdir(fs.Path(), 0755); err != nil { + if err := os.Mkdir(fs.Path(), 0o755); err != nil { return err } atomic.StoreInt64(&fs.diskUsed, 0) @@ -485,7 +485,7 @@ func (fs *Filesystem) ListDirectory(p string) ([]Stat, error) { defer wg.Done() var m *mimetype.MIME - var d = "inode/directory" + d := "inode/directory" if !f.IsDir() { cleanedp := filepath.Join(cleaned, f.Name()) if f.Mode()&os.ModeSymlink != 0 { diff --git a/server/filesystem/path.go b/server/filesystem/path.go index 6e5e735..8a439c3 100644 --- a/server/filesystem/path.go +++ b/server/filesystem/path.go @@ -115,8 +115,8 @@ func (fs *Filesystem) ParallelSafePath(paths []string) ([]string, error) { var cleaned []string // Simple locker function to avoid racy appends to the array of cleaned paths. - var m = new(sync.Mutex) - var push = func(c string) { + m := new(sync.Mutex) + push := func(c string) { m.Lock() cleaned = append(cleaned, c) m.Unlock() diff --git a/server/filesystem/path_test.go b/server/filesystem/path_test.go index 9c288f2..46760c7 100644 --- a/server/filesystem/path_test.go +++ b/server/filesystem/path_test.go @@ -107,7 +107,7 @@ func TestFilesystem_Blocks_Symlinks(t *testing.T) { panic(err) } - if err := os.Mkdir(filepath.Join(rfs.root, "/malicious_dir"), 0777); err != nil { + if err := os.Mkdir(filepath.Join(rfs.root, "/malicious_dir"), 0o777); err != nil { panic(err) } diff --git a/server/install.go b/server/install.go index 1cd978b..b450609 100644 --- a/server/install.go +++ b/server/install.go @@ -215,11 +215,11 @@ func (ip *InstallationProcess) tempDir() string { func (ip *InstallationProcess) writeScriptToDisk() error { // Make sure the temp directory root exists before trying to make a directory within it. The // ioutil.TempDir call expects this base to exist, it won't create it for you. - if err := os.MkdirAll(ip.tempDir(), 0700); err != nil { + if err := os.MkdirAll(ip.tempDir(), 0o700); err != nil { return errors.WithMessage(err, "could not create temporary directory for install process") } - f, err := os.OpenFile(filepath.Join(ip.tempDir(), "install.sh"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + f, err := os.OpenFile(filepath.Join(ip.tempDir(), "install.sh"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644) if err != nil { return errors.WithMessage(err, "failed to write server installation script to disk before mount") } @@ -350,7 +350,7 @@ func (ip *InstallationProcess) AfterExecute(containerId string) error { return err } - f, err := os.OpenFile(ip.GetLogPath(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + f, err := os.OpenFile(ip.GetLogPath(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) if err != nil { return err } @@ -516,7 +516,6 @@ func (ip *InstallationProcess) StreamOutput(ctx context.Context, id string) erro ShowStderr: true, Follow: true, }) - if err != nil { return err } diff --git a/server/listeners.go b/server/listeners.go index 26ecaf8..0847f3f 100644 --- a/server/listeners.go +++ b/server/listeners.go @@ -59,7 +59,6 @@ func (s *Server) StartEventListeners() { err := t.Increment(func() { s.PublishConsoleOutputFromDaemon("Your server is outputting too much data and is being throttled.") }) - // An error is only returned if the server has breached the thresholds set. if err != nil { // If the process is already stopping, just let it continue with that action rather than attempting diff --git a/system/utils.go b/system/utils.go index 681ac2a..785b505 100644 --- a/system/utils.go +++ b/system/utils.go @@ -15,8 +15,10 @@ import ( "emperror.dev/errors" ) -var cr = []byte(" \r") -var crr = []byte("\r\n") +var ( + cr = []byte(" \r") + crr = []byte("\r\n") +) // FirstNotEmpty returns the first string passed in that is not an empty value. func FirstNotEmpty(v ...string) string {