From 32b75262ada0cf45fbe6eb1c425bdb5c748a86ff Mon Sep 17 00:00:00 2001 From: Gusted Date: Tue, 3 Aug 2021 20:35:55 +0200 Subject: [PATCH] refactor(ifelse): shorten logic where possible --- cmd/maddyctl/clitools/clitools.go | 11 +++++------ cmd/maddyctl/imap.go | 12 ++---------- framework/config/endpoint.go | 7 +++---- internal/auth/external/helperauth.go | 3 +-- internal/endpoint/imap/imap.go | 6 +----- internal/endpoint/smtp/smtp_test.go | 5 +---- internal/smtpconn/smtputf8_test.go | 6 +----- internal/table/file.go | 8 ++------ maddy.go | 5 +---- 9 files changed, 17 insertions(+), 46 deletions(-) diff --git a/cmd/maddyctl/clitools/clitools.go b/cmd/maddyctl/clitools/clitools.go index 16dd9c2..2da6998 100644 --- a/cmd/maddyctl/clitools/clitools.go +++ b/cmd/maddyctl/clitools/clitools.go @@ -109,11 +109,10 @@ func ReadPassword(prompt string) (string, error) { fmt.Println() return string(buf), nil - } else { - if !stdinScanner.Scan() { - return "", stdinScanner.Err() - } - - return stdinScanner.Text(), nil } + if !stdinScanner.Scan() { + return "", stdinScanner.Err() + } + + return stdinScanner.Text(), nil } diff --git a/cmd/maddyctl/imap.go b/cmd/maddyctl/imap.go index e5215b5..02fe067 100644 --- a/cmd/maddyctl/imap.go +++ b/cmd/maddyctl/imap.go @@ -146,11 +146,7 @@ func mboxesRemove(be module.Storage, ctx *cli.Context) error { } } - if err := u.DeleteMailbox(name); err != nil { - return err - } - - return nil + return u.DeleteMailbox(name) } func mboxesRename(be module.Storage, ctx *cli.Context) error { @@ -264,11 +260,7 @@ func msgsRemove(be module.Storage, ctx *cli.Context) error { } mboxB := mbox.(*imapsql.Mailbox) - if err := mboxB.DelMessages(ctx.Bool("uid"), seq); err != nil { - return err - } - - return nil + return mboxB.DelMessages(ctx.Bool("uid"), seq) } func msgsCopy(be module.Storage, ctx *cli.Context) error { diff --git a/framework/config/endpoint.go b/framework/config/endpoint.go index 36ead2f..649a117 100644 --- a/framework/config/endpoint.go +++ b/framework/config/endpoint.go @@ -70,17 +70,16 @@ func (e Endpoint) String() string { func (e Endpoint) Network() string { if e.Scheme == "unix" { return "unix" - } else { - return "tcp" } + return "tcp" } func (e Endpoint) Address() string { if e.Scheme == "unix" { return e.Path - } else { - return net.JoinHostPort(e.Host, e.Port) } + return net.JoinHostPort(e.Host, e.Port) + } func (e Endpoint) IsTLS() bool { diff --git a/internal/auth/external/helperauth.go b/internal/auth/external/helperauth.go index a43c479..901d57d 100644 --- a/internal/auth/external/helperauth.go +++ b/internal/auth/external/helperauth.go @@ -48,9 +48,8 @@ func AuthUsingHelper(binaryPath, accountName, password string) error { return fmt.Errorf("helperauth: %w: %v", err, string(exitErr.Stderr)) } return module.ErrUnknownCredentials - } else { - return fmt.Errorf("helperauth: process wait: %w", err) } + return fmt.Errorf("helperauth: process wait: %w", err) } return nil } diff --git a/internal/endpoint/imap/imap.go b/internal/endpoint/imap/imap.go index 3d1eb0b..93cc4bf 100644 --- a/internal/endpoint/imap/imap.go +++ b/internal/endpoint/imap/imap.go @@ -151,11 +151,7 @@ func (endp *Endpoint) Init(cfg *config.Map) error { }) } - if err := endp.setupListeners(addresses); err != nil { - return err - } - - return nil + return endp.setupListeners(addresses) } func (endp *Endpoint) setupListeners(addresses []config.Endpoint) error { diff --git a/internal/endpoint/smtp/smtp_test.go b/internal/endpoint/smtp/smtp_test.go index a3f28c1..b825d15 100644 --- a/internal/endpoint/smtp/smtp_test.go +++ b/internal/endpoint/smtp/smtp_test.go @@ -135,11 +135,8 @@ func submitMsgOpts(t *testing.T, cl *smtp.Client, from string, rcpts []string, o if _, err := data.Write([]byte(msg)); err != nil { return err } - if err := data.Close(); err != nil { - return err - } - return nil + return data.Close() } func TestSMTPDelivery(t *testing.T) { diff --git a/internal/smtpconn/smtputf8_test.go b/internal/smtpconn/smtputf8_test.go index 8c6a7a7..22acf41 100644 --- a/internal/smtpconn/smtputf8_test.go +++ b/internal/smtpconn/smtputf8_test.go @@ -45,11 +45,7 @@ func doTestDelivery(t *testing.T, conn *C, from string, to []string, opts smtp.M hdr := textproto.Header{} hdr.Add("B", "2") hdr.Add("A", "1") - if err := conn.Data(context.Background(), hdr, strings.NewReader("foobar\n")); err != nil { - return err - } - - return nil + return conn.Data(context.Background(), hdr, strings.NewReader("foobar\n")) } func TestSMTPUTF8(t *testing.T) { diff --git a/internal/table/file.go b/internal/table/file.go index c21ddf3..aec8930 100644 --- a/internal/table/file.go +++ b/internal/table/file.go @@ -184,7 +184,7 @@ func readFile(path string, out map[string][]string) error { } for scnr.Scan() { - lineCounter += 1 + lineCounter++ if strings.HasPrefix(scnr.Text(), "#") { continue } @@ -207,11 +207,7 @@ func readFile(path string, out map[string][]string) error { out[from] = append(out[from], to) } - if err := scnr.Err(); err != nil { - return err - } - - return nil + return scnr.Err() } func (f *File) Lookup(_ context.Context, val string) (string, bool, error) { diff --git a/maddy.go b/maddy.go index 795048e..d646fda 100644 --- a/maddy.go +++ b/maddy.go @@ -241,10 +241,7 @@ func ensureDirectoryWritable(path string) error { return err } testFile.Close() - if err := os.Remove(testFile.Name()); err != nil { - return err - } - return nil + return os.Remove(testFile.Name()) } func ReadGlobals(cfg []config.Node) (map[string]interface{}, []config.Node, error) {