-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
139 lines (115 loc) 路 3.01 KB
/
Copy pathmain.go
File metadata and controls
139 lines (115 loc) 路 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
goversion "github.com/caarlos0/go-version"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v2"
"github.com/grishy/go-avahi-cname/cmd"
)
const (
forceExitTimeout = 5 * time.Second
appName = "go-avahi-cname"
)
// Version information set during build.
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
os.Exit(runMain())
}
func runMain() int {
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
go handleGracefulShutdown(ctx)
if err := run(ctx); err != nil {
fmt.Println("Error:")
fmt.Printf(" > %+v\n", err)
return 1
}
// To avoid graceful shutdown timeout
return 0
}
// run starts and configures the CLI application.
func run(ctx context.Context) error {
cli.VersionPrinter = func(_ *cli.Context) {
fmt.Print(buildVersion().String())
}
app := &cli.App{
Name: appName,
Usage: "Create local domain names using Avahi daemon",
Version: version,
Description: `A tool that helps you create local domain names for your computer by using the Avahi daemon.
It works in two ways:
1. Automatic mode (use 'subdomain' command):
Any subdomain you try to use (like myapp.computer.local) will automatically point to your computer
2. Manual mode (use 'cname' command):
You can create your own domain names that point to your computer and keep them active
Need help? Visit https://github.com/grishy/go-avahi-cname`,
Authors: []*cli.Author{{
Name: "Sergei G.",
Email: "mail@grishy.dev",
}},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "enable debug logging",
EnvVars: []string{"DEBUG"},
Value: false,
},
},
Before: setupLogger,
Commands: []*cli.Command{
cmd.Cname(ctx),
cmd.Subdomain(ctx),
},
}
return app.Run(os.Args)
}
// handleGracefulShutdown manages graceful shutdown with timeout.
func handleGracefulShutdown(ctx context.Context) {
<-ctx.Done()
slog.Info("initiating graceful shutdown...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), forceExitTimeout)
// Wait for cleanup or timeout
<-shutdownCtx.Done()
cancel() // To make linter happy
if shutdownCtx.Err() == context.DeadlineExceeded {
slog.Error("failed to shutdown gracefully, forcing exit")
os.Exit(1)
}
}
// setupLogger configures the global structured logger with appropriate settings.
func setupLogger(c *cli.Context) error {
w := os.Stdout
level := slog.LevelInfo
if c.Bool("debug") {
level = slog.LevelDebug
}
slog.SetDefault(slog.New(
tint.NewHandler(w, &tint.Options{
Level: level,
NoColor: !isatty.IsTerminal(w.Fd()),
TimeFormat: time.TimeOnly,
}),
))
return nil
}
// buildVersion constructs version information for the application.
func buildVersion() goversion.Info {
return goversion.GetVersionInfo(
func(i *goversion.Info) {
i.GitCommit = commit
i.BuildDate = date
i.GitVersion = version
},
)
}