Skip to content

Commit 627cab7

Browse files
committed
refactor: improve constant naming and error handling
1 parent d844929 commit 627cab7

4 files changed

Lines changed: 23 additions & 21 deletions

File tree

avahi/publisher.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
)
1111

1212
const (
13-
// https://github.com/lathiat/avahi/blob/v0.8/avahi-common/defs.h#L343
14-
AVAHI_DNS_CLASS_IN = uint16(0x01)
15-
// https://github.com/lathiat/avahi/blob/v0.8/avahi-common/defs.h#L331
16-
AVAHI_DNS_TYPE_CNAME = uint16(0x05)
13+
// AvahiDNSClassIn from https://github.com/lathiat/avahi/blob/v0.8/avahi-common/defs.h#L343
14+
AvahiDNSClassIn = uint16(0x01)
15+
// AvahiDNSTypeCName from https://github.com/lathiat/avahi/blob/v0.8/avahi-common/defs.h#L331
16+
AvahiDNSTypeCName = uint16(0x05)
1717
)
1818

1919
type Publisher struct {
@@ -91,8 +91,8 @@ func (p *Publisher) PublishCNAMES(cnames []string, ttl uint32) error {
9191
avahi.ProtoUnspec,
9292
uint32(0), // From Avahi Python bindings https://gist.github.com/gdamjan/3168336#file-avahi-alias-py-L42
9393
cname,
94-
AVAHI_DNS_CLASS_IN,
95-
AVAHI_DNS_TYPE_CNAME,
94+
AvahiDNSClassIn,
95+
AvahiDNSTypeCName,
9696
ttl,
9797
p.rdataField,
9898
)

cmd/cname.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"log/slog"
78
"time"
@@ -117,7 +118,7 @@ func Cname(ctx context.Context) *cli.Command {
117118
cnames := cCtx.Args().Slice()
118119

119120
if len(cnames) == 0 {
120-
return fmt.Errorf("at least one CNAME should be provided")
121+
return errors.New("at least one CNAME should be provided")
121122
}
122123

123124
slog.Info("creating publisher")

cmd/subdomain.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func reader(ctx context.Context, conn *net.UDPConn) chan *dnsMsg {
6666
}
6767
slog.Debug("received UDP message", "bytes", bytesRead, "from", remoteAddress)
6868

69-
if err := dnsMsg.msg.Unpack(buf[:bytesRead]); err != nil {
70-
dnsMsg.err = errors.Join(dnsMsg.err, fmt.Errorf("failed to unpack message: %w", err))
69+
if unpackErr := dnsMsg.msg.Unpack(buf[:bytesRead]); unpackErr != nil {
70+
dnsMsg.err = errors.Join(dnsMsg.err, fmt.Errorf("failed to unpack message: %w", unpackErr))
7171
msgCh <- dnsMsg
7272
continue
7373
}
@@ -81,10 +81,11 @@ func reader(ctx context.Context, conn *net.UDPConn) chan *dnsMsg {
8181
}
8282

8383
// selectQuestion filters and selects questions with the given FQDN suffix.
84-
func selectQuestion(fqdn string, qs []dns.Question) (res []string) {
84+
func selectQuestion(fqdn string, qs []dns.Question) []string {
8585
suffix := strings.ToLower("." + fqdn)
8686
slog.Debug("filtering DNS questions", "suffix", suffix, "questions", len(qs))
8787

88+
res := make([]string, 0, len(qs))
8889
for _, q := range qs {
8990
slog.Debug("processing question", "name", q.Name, "type", dns.TypeToString[q.Qtype])
9091

@@ -114,8 +115,8 @@ func runSubdomain(ctx context.Context, publisher *avahi.Publisher, fqdn string,
114115
<-ctx.Done()
115116
fmt.Println() // Add new line after ^C
116117
slog.Info("closing connection")
117-
if err := conn.Close(); err != nil {
118-
slog.Error("failed to close connection", "error", err)
118+
if closeErr := conn.Close(); closeErr != nil {
119+
slog.Error("failed to close connection", "error", closeErr)
119120
}
120121
}()
121122

@@ -132,8 +133,8 @@ func runSubdomain(ctx context.Context, publisher *avahi.Publisher, fqdn string,
132133

133134
if len(found) > 0 {
134135
slog.Debug("publishing matching CNAMEs", "count", len(found))
135-
if err := publisher.PublishCNAMES(found, ttl); err != nil {
136-
return fmt.Errorf("failed to publish CNAMEs: %w", err)
136+
if publishErr := publisher.PublishCNAMES(found, ttl); publishErr != nil {
137+
return fmt.Errorf("failed to publish CNAMEs: %w", publishErr)
137138
}
138139
}
139140
}

main.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222
appName = "go-avahi-cname"
2323
)
2424

25-
// Version information set during build
25+
// Version information set during build.
2626
var (
2727
version = "dev"
2828
commit = "none"
@@ -48,9 +48,9 @@ func runMain() int {
4848
return 0
4949
}
5050

51-
// run starts and configures the CLI application
51+
// run starts and configures the CLI application.
5252
func run(ctx context.Context) error {
53-
cli.VersionPrinter = func(c *cli.Context) {
53+
cli.VersionPrinter = func(_ *cli.Context) {
5454
fmt.Print(buildVersion().String())
5555
}
5656

@@ -61,7 +61,7 @@ func run(ctx context.Context) error {
6161
Description: `A tool that helps you create local domain names for your computer by using the Avahi daemon.
6262
6363
It works in two ways:
64-
1. Automatic mode (use 'subdomain' command):
64+
1. Automatic mode (use 'subdomain' command):
6565
Any subdomain you try to use (like myapp.computer.local) will automatically point to your computer
6666
6767
2. Manual mode (use 'cname' command):
@@ -91,7 +91,7 @@ Need help? Visit https://github.com/grishy/go-avahi-cname`,
9191
return app.Run(os.Args)
9292
}
9393

94-
// handleGracefulShutdown manages graceful shutdown with timeout
94+
// handleGracefulShutdown manages graceful shutdown with timeout.
9595
func handleGracefulShutdown(ctx context.Context) {
9696
<-ctx.Done()
9797
slog.Info("initiating graceful shutdown...")
@@ -108,7 +108,7 @@ func handleGracefulShutdown(ctx context.Context) {
108108
}
109109
}
110110

111-
// setupLogger configures the global structured logger with appropriate settings
111+
// setupLogger configures the global structured logger with appropriate settings.
112112
func setupLogger(c *cli.Context) error {
113113
w := os.Stdout
114114
level := slog.LevelInfo
@@ -127,7 +127,7 @@ func setupLogger(c *cli.Context) error {
127127
return nil
128128
}
129129

130-
// buildVersion constructs version information for the application
130+
// buildVersion constructs version information for the application.
131131
func buildVersion() goversion.Info {
132132
return goversion.GetVersionInfo(
133133
func(i *goversion.Info) {

0 commit comments

Comments
 (0)