-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: expose startup message customization hooks #3824
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
base: main
Are you sure you want to change the base?
Changes from all commits
6d8cea6
83288cc
484e2dc
9df2f79
2212e2f
9abe1b0
800a86f
fb6bde4
b2bfd52
25503d4
686c9e7
dc04c45
98445ba
20b14d7
a22dcdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -168,6 +168,64 @@ func main() { | |||||||||
| </TabItem> | ||||||||||
| </Tabs> | ||||||||||
|
|
||||||||||
| ### ListenData | ||||||||||
|
|
||||||||||
| `ListenData` exposes runtime metadata about the listener: | ||||||||||
|
|
||||||||||
| | Field | Type | Description | | ||||||||||
| | --- | --- | --- | | ||||||||||
| | `Host` | `string` | Resolved hostname or IP address. | | ||||||||||
| | `Port` | `string` | The bound port. | | ||||||||||
| | `TLS` | `bool` | Indicates whether TLS is enabled. | | ||||||||||
| | `Version` | `string` | Fiber version reported in the startup banner. | | ||||||||||
| | `AppName` | `string` | Application name from the configuration. | | ||||||||||
| | `HandlerCount` | `int` | Total registered handler count. | | ||||||||||
| | `ProcessCount` | `int` | Number of processes Fiber will use. | | ||||||||||
| | `PID` | `int` | Current process identifier. | | ||||||||||
| | `Prefork` | `bool` | Whether prefork is enabled. | | ||||||||||
| | `ChildPIDs` | `[]int` | Child process identifiers when preforking. | | ||||||||||
| | `ColorScheme` | [`Colors`](https://github.com/gofiber/fiber/blob/main/color.go) | Active color scheme for the startup message. | | ||||||||||
|
|
||||||||||
| ### Startup message customization | ||||||||||
|
|
||||||||||
| Use `OnPreStartupMessage` to tweak the banner before Fiber prints it, and `OnPostStartupMessage` to run logic after the banner is printed (or skipped): | ||||||||||
|
|
||||||||||
| - Assign `sm.Header` to override the ASCII art banner. Leave it empty to use the default. | ||||||||||
| - Provide `sm.PrimaryInfo` and/or `sm.SecondaryInfo` maps to replace the primary (server URL, handler counts, etc.) and secondary (prefork status, PID, process count) sections. | ||||||||||
| - Set `sm.PreventDefault = true` to suppress the built-in banner without affecting other hooks. | ||||||||||
| - `PostStartupMessageData` reports whether the banner was skipped via the `Disabled`, `IsChild`, and `Prevented` flags. | ||||||||||
|
|
||||||||||
| ```go title="Customize the startup message" | ||||||||||
| package main | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "fmt" | ||||||||||
| "os" | ||||||||||
|
|
||||||||||
| "github.com/gofiber/fiber/v3" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| func main() { | ||||||||||
| app := fiber.New() | ||||||||||
|
|
||||||||||
| app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error { | ||||||||||
| sm.Header = "FOOBER " + sm.Version + "\n-------" | ||||||||||
| sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")} | ||||||||||
| sm.SecondaryInfo = fiber.Map{"Prefork": sm.Prefork} | ||||||||||
|
Comment on lines
+213
to
+214
|
||||||||||
| sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")} | |
| sm.SecondaryInfo = fiber.Map{"Prefork": sm.Prefork} | |
| sm.AddInfo("git_hash", "Git hash", os.Getenv("GIT_HASH"), fiber.InfoPrimary) | |
| sm.AddInfo("prefork", "Prefork", sm.Prefork, fiber.InfoSecondary) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation for startup message customization is outdated. The description mentions sm.PrimaryInfo and sm.SecondaryInfo, and the example code uses them, but these fields do not exist on the PreStartupMessageData struct. The new API uses methods like AddInfo, AddWarning, and AddError. The documentation and example should be updated to reflect the correct API usage, as shown in the pull request description.
- Use `sm.AddInfo()`, `sm.AddWarning()`, and `sm.AddError()` to add, update, or remove startup message entries.
- Assign `sm.Header` to override the ASCII art banner. Leave it empty to use the default.
- Set `sm.PreventDefault = true` to suppress the built-in banner without affecting other hooks.
- `PostStartupMessageData` reports whether the banner was skipped via the `Disabled`, `IsChild`, and `Prevented` flags.
```go title="Customize the startup message"
package main
import (
"fmt"
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
sm.Header = "My Awesome App v" + sm.Version + "\n----------------------"
// You can add custom info, warnings, or errors to the startup message.
sm.AddInfo("custom_info", "Custom Info", "This is a custom message.")
sm.AddWarning("custom_warning", "Custom Warning", "This is a custom warning.")
// Entries can be prioritized. Higher priority entries are shown first.
sm.AddError("custom_error", "Custom Error", "This is a custom error.", 10)
return nil
})
app.Hooks().OnPostStartupMessage(func(sm fiber.PostStartupMessageData) error {
if !sm.Disabled && !sm.IsChild && !sm.Prevented {
fmt.Println("startup completed")
}
return nil
})
app.Listen(":5000")
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs: correct startup customization API usage
Replace references to PrimaryInfo/SecondaryInfo with the provided entry APIs and adjust the example.
-Use `OnPreStartupMessage` to tweak the banner before Fiber prints it, and `OnPostStartupMessage` to run logic after the banner is printed (or skipped):
-
-- Assign `sm.Header` to override the ASCII art banner. Leave it empty to use the default.
-- Provide `sm.PrimaryInfo` and/or `sm.SecondaryInfo` maps to replace the primary (server URL, handler counts, etc.) and secondary (prefork status, PID, process count) sections.
-- Set `sm.PreventDefault = true` to suppress the built-in banner without affecting other hooks.
-- `PostStartupMessageData` reports whether the banner was skipped via the `Disabled`, `IsChild`, and `Prevented` flags.
+Use `OnPreStartupMessage` to tweak the banner before Fiber prints it, and `OnPostStartupMessage` to run logic after the banner is printed (or skipped):
+
+- Set `sm.Header` to override the ASCII art/banner. Leave empty to keep the default.
+- Add lines with `sm.AddInfo`, `sm.AddWarning`, or `sm.AddError`. Use the optional `priority` arg to influence ordering (higher priority first).
+- Set `sm.PreventDefault = true` to suppress the built‑in banner.
+- `PostStartupMessageData` reports if the banner was disabled, printed by a child, or prevented via `Disabled`, `IsChild`, and `Prevented`.
@@
- app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
- sm.Header = "FOOBER " + sm.Version + "\n-------"
- sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")}
- sm.SecondaryInfo = fiber.Map{"Prefork": sm.Prefork}
- return nil
- })
+ app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
+ sm.Header = "FOOBER " + sm.Version + "\n-------"
+ sm.AddInfo("git_hash", "Git hash", os.Getenv("GIT_HASH"))
+ sm.AddInfo("prefork", "Prefork", fmt.Sprint(sm.Prefork))
+ return nil
+ })
@@
- if !sm.Disabled && !sm.IsChild && !sm.Prevented {
- fmt.Println("startup completed")
- }
+ if !sm.Disabled && !sm.IsChild && !sm.Prevented {
+ fmt.Println("startup completed")
+ }
return nil
})Optionally document entry ordering/priority briefly.
🤖 Prompt for AI Agents
In docs/api/hooks.md around lines 171–227, the startup customization example and
description still reference sm.PrimaryInfo and sm.SecondaryInfo which are
incorrect; update the prose and code sample to use the hook's provided entry
APIs (e.g. call the API methods to add primary and secondary entries instead of
assigning maps), adjust the example to show calling those entry methods to set a
"Git hash" primary entry and a "Prefork" secondary entry, and optionally add one
short sentence noting that entries are rendered in the order they are added
(earlier entries have higher display priority).
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -295,6 +295,29 @@ app.Listen("app.sock", fiber.ListenerConfig{ | |||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| - Expanded `ListenData` with versioning, handler, process, and PID metadata, plus dedicated startup message hooks for customization. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| ```go | ||||||||||||||||||||||||||||||
| app := fiber.New() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error { | ||||||||||||||||||||||||||||||
| sm.Header = "FOOBER " + sm.Version + "\n-------" | ||||||||||||||||||||||||||||||
| sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")} | ||||||||||||||||||||||||||||||
| sm.SecondaryInfo = fiber.Map{"Process count": sm.ProcessCount} | ||||||||||||||||||||||||||||||
|
Comment on lines
+305
to
+306
|
||||||||||||||||||||||||||||||
| sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")} | |
| sm.SecondaryInfo = fiber.Map{"Process count": sm.ProcessCount} | |
| sm.AddInfo("git_hash", "Git hash", os.Getenv("GIT_HASH"), fiber.PriorityHigh) | |
| sm.AddInfo("process_count", "Process count", sm.ProcessCount, fiber.PriorityLow) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example code for the new startup message hooks uses sm.PrimaryInfo and sm.SecondaryInfo, which are from an older implementation and no longer exist on PreStartupMessageData. The new API uses methods like AddInfo. This example should be updated to reflect the correct usage.
| app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error { | |
| sm.Header = "FOOBER " + sm.Version + "\n-------" | |
| sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")} | |
| sm.SecondaryInfo = fiber.Map{"Process count": sm.ProcessCount} | |
| // Set sm.PreventDefault = true to suppress the default banner entirely. | |
| return nil | |
| }) | |
| app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error { | |
| sm.Header = "FOOBER " + sm.Version + "\n-------" | |
| sm.AddInfo("git_hash", "Git hash", os.Getenv("GIT_HASH")) | |
| sm.AddInfo("process_count", "Process count", fmt.Sprintf("%d", sm.ProcessCount)) | |
| // Set sm.PreventDefault = true to suppress the default banner entirely. | |
| return nil | |
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align example with actual API (no PrimaryInfo/SecondaryInfo fields)
PreStartupMessageData exposes AddInfo/AddWarning/AddError and Header/PreventDefault, not PrimaryInfo/SecondaryInfo. Update the snippet to compile and reflect the API.
app := fiber.New()
app.Hooks().OnPreStartupMessage(func(sm *fiber.PreStartupMessageData) error {
- sm.Header = "FOOBER " + sm.Version + "\n-------"
- sm.PrimaryInfo = fiber.Map{"Git hash": os.Getenv("GIT_HASH")}
- sm.SecondaryInfo = fiber.Map{"Process count": sm.ProcessCount}
+ sm.Header = "FOOBER " + sm.Version + "\n-------"
+ sm.AddInfo("git_hash", "Git hash", os.Getenv("GIT_HASH"))
+ sm.AddInfo("proc_count", "Process count", fmt.Sprint(sm.ProcessCount))
// Set sm.PreventDefault = true to suppress the default banner entirely.
return nil
})
app.Hooks().OnPostStartupMessage(func(sm fiber.PostStartupMessageData) error {
if !sm.Disabled && !sm.IsChild && !sm.Prevented {
- log.Println("startup completed")
+ fmt.Println("startup completed")
}
return nil
})
go app.Listen(":3000")🤖 Prompt for AI Agents
In docs/whats_new.md around lines 298 to 319, the PreStartupMessageData example
uses nonexistent PrimaryInfo/SecondaryInfo fields and will not compile; replace
those with calls to AddInfo/AddWarning/AddError and set Header/PreventDefault as
shown by the API, e.g. populate the metadata using sm.AddInfo(key, value) (or
AddWarning/AddError) and remove PrimaryInfo/SecondaryInfo assignments, and
ensure the PostStartupMessageData checks use the actual fields
(Disabled/IsChild/Prevented) as in the snippet so the example matches the real
API.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,9 @@ | ||||||||||||||||||||||||||||||||||||||||
| package fiber | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||
| "sort" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| "github.com/gofiber/fiber/v3/log" | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
@@ -15,6 +18,10 @@ | |||||||||||||||||||||||||||||||||||||||
| OnGroupNameHandler = OnGroupHandler | ||||||||||||||||||||||||||||||||||||||||
| // OnListenHandler runs when the application begins listening and receives the listener details. | ||||||||||||||||||||||||||||||||||||||||
| OnListenHandler = func(ListenData) error | ||||||||||||||||||||||||||||||||||||||||
| // OnPreStartupMessageHandler runs before Fiber prints the startup banner. | ||||||||||||||||||||||||||||||||||||||||
| OnPreStartupMessageHandler = func(*PreStartupMessageData) error | ||||||||||||||||||||||||||||||||||||||||
| // OnPostStartupMessageHandler runs after Fiber prints (or skips) the startup banner. | ||||||||||||||||||||||||||||||||||||||||
| OnPostStartupMessageHandler = func(PostStartupMessageData) error | ||||||||||||||||||||||||||||||||||||||||
| // OnPreShutdownHandler runs before the application shuts down. | ||||||||||||||||||||||||||||||||||||||||
| OnPreShutdownHandler = func() error | ||||||||||||||||||||||||||||||||||||||||
| // OnPostShutdownHandler runs after shutdown and receives the shutdown result. | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -36,17 +43,183 @@ | |||||||||||||||||||||||||||||||||||||||
| onGroup []OnGroupHandler | ||||||||||||||||||||||||||||||||||||||||
| onGroupName []OnGroupNameHandler | ||||||||||||||||||||||||||||||||||||||||
| onListen []OnListenHandler | ||||||||||||||||||||||||||||||||||||||||
| onPreStartup []OnPreStartupMessageHandler | ||||||||||||||||||||||||||||||||||||||||
| onPostStartup []OnPostStartupMessageHandler | ||||||||||||||||||||||||||||||||||||||||
| onPreShutdown []OnPreShutdownHandler | ||||||||||||||||||||||||||||||||||||||||
| onPostShutdown []OnPostShutdownHandler | ||||||||||||||||||||||||||||||||||||||||
| onFork []OnForkHandler | ||||||||||||||||||||||||||||||||||||||||
| onMount []OnMountHandler | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // ListenData is a struct to use it with OnListenHandler | ||||||||||||||||||||||||||||||||||||||||
| type StartupMessageLevel int | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||||||||||||||
| // StartupMessageLevelInfo represents informational startup message entries. | ||||||||||||||||||||||||||||||||||||||||
| StartupMessageLevelInfo StartupMessageLevel = iota | ||||||||||||||||||||||||||||||||||||||||
| // StartupMessageLevelWarning represents warning startup message entries. | ||||||||||||||||||||||||||||||||||||||||
| StartupMessageLevelWarning | ||||||||||||||||||||||||||||||||||||||||
| // StartupMessageLevelError represents error startup message entries. | ||||||||||||||||||||||||||||||||||||||||
| StartupMessageLevelError | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // startupMessageEntry represents a single line of startup message information. | ||||||||||||||||||||||||||||||||||||||||
| type startupMessageEntry struct { | ||||||||||||||||||||||||||||||||||||||||
| key string | ||||||||||||||||||||||||||||||||||||||||
| title string | ||||||||||||||||||||||||||||||||||||||||
| value string | ||||||||||||||||||||||||||||||||||||||||
| priority int | ||||||||||||||||||||||||||||||||||||||||
| level StartupMessageLevel | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // ListenData contains the listener metadata provided to OnListenHandler. | ||||||||||||||||||||||||||||||||||||||||
| type ListenData struct { | ||||||||||||||||||||||||||||||||||||||||
| Host string | ||||||||||||||||||||||||||||||||||||||||
| Port string | ||||||||||||||||||||||||||||||||||||||||
| TLS bool | ||||||||||||||||||||||||||||||||||||||||
| ColorScheme Colors | ||||||||||||||||||||||||||||||||||||||||
| Host string | ||||||||||||||||||||||||||||||||||||||||
| Port string | ||||||||||||||||||||||||||||||||||||||||
| Version string | ||||||||||||||||||||||||||||||||||||||||
| AppName string | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| ChildPIDs []int | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| HandlerCount int | ||||||||||||||||||||||||||||||||||||||||
| ProcessCount int | ||||||||||||||||||||||||||||||||||||||||
| PID int | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| TLS bool | ||||||||||||||||||||||||||||||||||||||||
| Prefork bool | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // PreStartupMessageData contains metadata exposed to OnPreStartupMessage hooks. | ||||||||||||||||||||||||||||||||||||||||
| type PreStartupMessageData struct { | ||||||||||||||||||||||||||||||||||||||||
| *ListenData | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| entries []startupMessageEntry | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Header string | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| PreventDefault bool | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+93
to
+101
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix struct field alignment for PreStartupMessageData Govet/betteralign flags padding waste. Reorder fields to reduce padding and satisfy the pipeline. -type PreStartupMessageData struct {
- *ListenData
- entries []startupMessageEntry
- Header string
- PreventDefault bool
-}
+type PreStartupMessageData struct {
+ *ListenData
+ Header string
+ entries []startupMessageEntry
+ PreventDefault bool
+}As per coding guidelines (betteralign). 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Actions: golangci-lint[error] 93-93: golangci-lint (go vet) error: fieldalignment: struct with 40 pointer bytes could be 32 (govet) 🪛 GitHub Check: lint[failure] 93-93: 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (sm *PreStartupMessageData) AddInfo(key, title, value string, priority ...int) { | ||||||||||||||||||||||||||||||||||||||||
| pri := -1 | ||||||||||||||||||||||||||||||||||||||||
| if len(priority) > 0 { | ||||||||||||||||||||||||||||||||||||||||
| pri = priority[0] | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sm.addEntry(key, title, value, pri, StartupMessageLevelInfo) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (sm *PreStartupMessageData) AddWarning(key, title, value string, priority ...int) { | ||||||||||||||||||||||||||||||||||||||||
| pri := -1 | ||||||||||||||||||||||||||||||||||||||||
| if len(priority) > 0 { | ||||||||||||||||||||||||||||||||||||||||
| pri = priority[0] | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sm.addEntry(key, title, value, pri, StartupMessageLevelWarning) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (sm *PreStartupMessageData) AddError(key, title, value string, priority ...int) { | ||||||||||||||||||||||||||||||||||||||||
| pri := -1 | ||||||||||||||||||||||||||||||||||||||||
| if len(priority) > 0 { | ||||||||||||||||||||||||||||||||||||||||
| pri = priority[0] | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sm.addEntry(key, title, value, pri, StartupMessageLevelError) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (sm *PreStartupMessageData) EntryKeys() []string { | ||||||||||||||||||||||||||||||||||||||||
| keys := make([]string, 0, len(sm.entries)) | ||||||||||||||||||||||||||||||||||||||||
| for _, entry := range sm.entries { | ||||||||||||||||||||||||||||||||||||||||
| keys = append(keys, entry.key) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| return keys | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (sm *PreStartupMessageData) ResetEntries() { | ||||||||||||||||||||||||||||||||||||||||
| sm.entries = sm.entries[:0] | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (sm *PreStartupMessageData) addEntry(key, title, value string, priority int, level StartupMessageLevel) { | ||||||||||||||||||||||||||||||||||||||||
| if sm.entries == nil { | ||||||||||||||||||||||||||||||||||||||||
| sm.entries = make([]startupMessageEntry, 0) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| for i, entry := range sm.entries { | ||||||||||||||||||||||||||||||||||||||||
| if entry.key == key { | ||||||||||||||||||||||||||||||||||||||||
| sm.entries[i].value = value | ||||||||||||||||||||||||||||||||||||||||
| sm.entries[i].title = title | ||||||||||||||||||||||||||||||||||||||||
| sm.entries[i].level = level | ||||||||||||||||||||||||||||||||||||||||
| sm.entries[i].priority = priority | ||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sm.entries = append(sm.entries, startupMessageEntry{key: key, title: title, value: value, level: level, priority: priority}) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (sm *PreStartupMessageData) DeleteEntry(key string) { | ||||||||||||||||||||||||||||||||||||||||
| if sm.entries == nil { | ||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| for i, entry := range sm.entries { | ||||||||||||||||||||||||||||||||||||||||
| if entry.key == key { | ||||||||||||||||||||||||||||||||||||||||
| sm.entries = append(sm.entries[:i], sm.entries[i+1:]...) | ||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func newPreStartupMessageData(listenData ListenData) *PreStartupMessageData { | ||||||||||||||||||||||||||||||||||||||||
| clone := listenData | ||||||||||||||||||||||||||||||||||||||||
| if len(listenData.ChildPIDs) > 0 { | ||||||||||||||||||||||||||||||||||||||||
| clone.ChildPIDs = append([]int(nil), listenData.ChildPIDs...) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return &PreStartupMessageData{ListenData: &clone} | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // PostStartupMessageData contains metadata exposed to OnPostStartupMessage hooks. | ||||||||||||||||||||||||||||||||||||||||
| type PostStartupMessageData struct { | ||||||||||||||||||||||||||||||||||||||||
| *ListenData | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Disabled bool | ||||||||||||||||||||||||||||||||||||||||
| IsChild bool | ||||||||||||||||||||||||||||||||||||||||
| Prevented bool | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func newPostStartupMessageData(listenData ListenData, disabled, isChild, prevented bool) PostStartupMessageData { | ||||||||||||||||||||||||||||||||||||||||
| clone := listenData | ||||||||||||||||||||||||||||||||||||||||
| if len(listenData.ChildPIDs) > 0 { | ||||||||||||||||||||||||||||||||||||||||
| clone.ChildPIDs = append([]int(nil), listenData.ChildPIDs...) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return PostStartupMessageData{ | ||||||||||||||||||||||||||||||||||||||||
| ListenData: &clone, | ||||||||||||||||||||||||||||||||||||||||
| Disabled: disabled, | ||||||||||||||||||||||||||||||||||||||||
| IsChild: isChild, | ||||||||||||||||||||||||||||||||||||||||
| Prevented: prevented, | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func mapToEntries(values Map) ([]startupMessageEntry, bool) { | ||||||||||||||||||||||||||||||||||||||||
| if len(values) == 0 { | ||||||||||||||||||||||||||||||||||||||||
| return nil, false | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| keys := make([]string, 0, len(values)) | ||||||||||||||||||||||||||||||||||||||||
| for key := range values { | ||||||||||||||||||||||||||||||||||||||||
| keys = append(keys, key) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sort.Strings(keys) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| entries := make([]startupMessageEntry, 0, len(values)) | ||||||||||||||||||||||||||||||||||||||||
| for _, key := range keys { | ||||||||||||||||||||||||||||||||||||||||
| entries = append(entries, startupMessageEntry{key: key, value: fmt.Sprint(values[key])}) | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return entries, true | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+205
to
223
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+205
to
223
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove or silence unused mapToEntries to fix lint failure
Option A (remove): -func mapToEntries(values Map) ([]startupMessageEntry, bool) {
- if len(values) == 0 {
- return nil, false
- }
- keys := make([]string, 0, len(values))
- for key := range values {
- keys = append(keys, key)
- }
- sort.Strings(keys)
- entries := make([]startupMessageEntry, 0, len(values))
- for _, key := range keys {
- entries = append(entries, startupMessageEntry{key: key, value: fmt.Sprint(values[key])})
- }
- return entries, true
-}Option B (silence temporarily): - func mapToEntries(values Map) ([]startupMessageEntry, bool) {
+ // mapToEntries converts a map to sorted entries.
+ // nolint:unused // Will be wired by listen.go banner mapping follow-up.
+ func mapToEntries(values Map) ([]startupMessageEntry, bool) {Choose A if no immediate use is planned. As per coding guidelines. 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: lint[failure] 205-205: 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func newHooks(app *App) *Hooks { | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -57,6 +230,8 @@ | |||||||||||||||||||||||||||||||||||||||
| onGroupName: make([]OnGroupNameHandler, 0), | ||||||||||||||||||||||||||||||||||||||||
| onName: make([]OnNameHandler, 0), | ||||||||||||||||||||||||||||||||||||||||
| onListen: make([]OnListenHandler, 0), | ||||||||||||||||||||||||||||||||||||||||
| onPreStartup: make([]OnPreStartupMessageHandler, 0), | ||||||||||||||||||||||||||||||||||||||||
| onPostStartup: make([]OnPostStartupMessageHandler, 0), | ||||||||||||||||||||||||||||||||||||||||
| onPreShutdown: make([]OnPreShutdownHandler, 0), | ||||||||||||||||||||||||||||||||||||||||
| onPostShutdown: make([]OnPostShutdownHandler, 0), | ||||||||||||||||||||||||||||||||||||||||
| onFork: make([]OnForkHandler, 0), | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -107,6 +282,20 @@ | |||||||||||||||||||||||||||||||||||||||
| h.app.mutex.Unlock() | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // OnPreStartupMessage is a hook to execute user functions before the startup message is printed. | ||||||||||||||||||||||||||||||||||||||||
| func (h *Hooks) OnPreStartupMessage(handler ...OnPreStartupMessageHandler) { | ||||||||||||||||||||||||||||||||||||||||
| h.app.mutex.Lock() | ||||||||||||||||||||||||||||||||||||||||
| h.onPreStartup = append(h.onPreStartup, handler...) | ||||||||||||||||||||||||||||||||||||||||
| h.app.mutex.Unlock() | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // OnPostStartupMessage is a hook to execute user functions after the startup message is printed (or skipped). | ||||||||||||||||||||||||||||||||||||||||
| func (h *Hooks) OnPostStartupMessage(handler ...OnPostStartupMessageHandler) { | ||||||||||||||||||||||||||||||||||||||||
| h.app.mutex.Lock() | ||||||||||||||||||||||||||||||||||||||||
| h.onPostStartup = append(h.onPostStartup, handler...) | ||||||||||||||||||||||||||||||||||||||||
| h.app.mutex.Unlock() | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // OnPreShutdown is a hook to execute user functions before Shutdown. | ||||||||||||||||||||||||||||||||||||||||
| func (h *Hooks) OnPreShutdown(handler ...OnPreShutdownHandler) { | ||||||||||||||||||||||||||||||||||||||||
| h.app.mutex.Lock() | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -209,6 +398,26 @@ | |||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (h *Hooks) executeOnPreStartupMessageHooks(data *PreStartupMessageData) error { | ||||||||||||||||||||||||||||||||||||||||
| for _, handler := range h.onPreStartup { | ||||||||||||||||||||||||||||||||||||||||
| if err := handler(data); err != nil { | ||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (h *Hooks) executeOnPostStartupMessageHooks(data PostStartupMessageData) error { | ||||||||||||||||||||||||||||||||||||||||
| for _, handler := range h.onPostStartup { | ||||||||||||||||||||||||||||||||||||||||
| if err := handler(data); err != nil { | ||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (h *Hooks) executeOnPreShutdownHooks() { | ||||||||||||||||||||||||||||||||||||||||
| for _, v := range h.onPreShutdown { | ||||||||||||||||||||||||||||||||||||||||
| if err := v(); err != nil { | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation incorrectly describes
PrimaryInfoandSecondaryInfofields that don't exist. The actual API uses methodsAddInfo(),AddWarning(),AddError(),ResetEntries(), andDeleteEntry()to manage startup entries. Update this documentation to reflect the actual implementation.