mirror of
https://github.com/safing/portbase
synced 2025-09-04 03:29:59 +00:00
Implement review suggestions
This commit is contained in:
parent
7c7340c2e7
commit
4b2bfc3e8f
3 changed files with 19 additions and 19 deletions
|
@ -65,7 +65,7 @@ func printStack(_ *Request) (msg string, err error) {
|
||||||
// debugInfo returns the debugging information for support requests.
|
// debugInfo returns the debugging information for support requests.
|
||||||
func debugInfo(ar *Request) (data []byte, err error) {
|
func debugInfo(ar *Request) (data []byte, err error) {
|
||||||
// Create debug information helper.
|
// Create debug information helper.
|
||||||
di := new(debug.DebugInfo)
|
di := new(debug.Info)
|
||||||
di.Style = ar.Request.URL.Query().Get("style")
|
di.Style = ar.Request.URL.Query().Get("style")
|
||||||
|
|
||||||
// Add debug information.
|
// Add debug information.
|
||||||
|
|
|
@ -13,40 +13,40 @@ import (
|
||||||
"github.com/shirou/gopsutil/host"
|
"github.com/shirou/gopsutil/host"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DebugInfo gathers debugging information and stores everything in a buffer in
|
// Info gathers debugging information and stores everything in a buffer in
|
||||||
// order to write it to somewhere later. It directly inherits a bytes.Buffer,
|
// order to write it to somewhere later. It directly inherits a bytes.Buffer,
|
||||||
// so you can also use all these functions too.
|
// so you can also use all these functions too.
|
||||||
type DebugInfo struct {
|
type Info struct {
|
||||||
bytes.Buffer
|
bytes.Buffer
|
||||||
Style string
|
Style string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DebugInfoFlag defines possible options for adding sections to a DebugInfo.
|
// InfoFlag defines possible options for adding sections to a Info.
|
||||||
type DebugInfoFlag int
|
type InfoFlag int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// NoFlags does nothing.
|
// NoFlags does nothing.
|
||||||
NoFlags DebugInfoFlag = 0
|
NoFlags InfoFlag = 0
|
||||||
|
|
||||||
// UseCodeSection wraps the section content in a markdown code section.
|
// UseCodeSection wraps the section content in a markdown code section.
|
||||||
UseCodeSection DebugInfoFlag = 1
|
UseCodeSection InfoFlag = 1
|
||||||
|
|
||||||
// AddContentLineBreaks adds a line breaks after each line of content,
|
// AddContentLineBreaks adds a line breaks after each line of content,
|
||||||
// except for the last.
|
// except for the last.
|
||||||
AddContentLineBreaks DebugInfoFlag = 2
|
AddContentLineBreaks InfoFlag = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
func useCodeSection(flags DebugInfoFlag) bool {
|
func useCodeSection(flags InfoFlag) bool {
|
||||||
return flags&UseCodeSection > 0
|
return flags&UseCodeSection > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func addContentLineBreaks(flags DebugInfoFlag) bool {
|
func addContentLineBreaks(flags InfoFlag) bool {
|
||||||
return flags&AddContentLineBreaks > 0
|
return flags&AddContentLineBreaks > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddSection adds a debug section to the DebugInfo. The result is directly
|
// AddSection adds a debug section to the Info. The result is directly
|
||||||
// written into the buffer.
|
// written into the buffer.
|
||||||
func (di *DebugInfo) AddSection(name string, flags DebugInfoFlag, content ...string) {
|
func (di *Info) AddSection(name string, flags InfoFlag, content ...string) {
|
||||||
// Check if we need a spacer.
|
// Check if we need a spacer.
|
||||||
if di.Len() > 0 {
|
if di.Len() > 0 {
|
||||||
di.WriteString("\n\n")
|
di.WriteString("\n\n")
|
||||||
|
@ -84,7 +84,7 @@ func (di *DebugInfo) AddSection(name string, flags DebugInfoFlag, content ...str
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddVersionInfo adds version information from the info pkg.
|
// AddVersionInfo adds version information from the info pkg.
|
||||||
func (di *DebugInfo) AddVersionInfo() {
|
func (di *Info) AddVersionInfo() {
|
||||||
di.AddSection(
|
di.AddSection(
|
||||||
"Version "+info.Version(),
|
"Version "+info.Version(),
|
||||||
UseCodeSection,
|
UseCodeSection,
|
||||||
|
@ -93,7 +93,7 @@ func (di *DebugInfo) AddVersionInfo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddPlatformInfo adds OS and platform information.
|
// AddPlatformInfo adds OS and platform information.
|
||||||
func (di *DebugInfo) AddPlatformInfo(ctx context.Context) {
|
func (di *Info) AddPlatformInfo(ctx context.Context) {
|
||||||
// Get information from the system.
|
// Get information from the system.
|
||||||
info, err := host.InfoWithContext(ctx)
|
info, err := host.InfoWithContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -126,7 +126,7 @@ func (di *DebugInfo) AddPlatformInfo(ctx context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddGoroutineStack adds the current goroutine stack.
|
// AddGoroutineStack adds the current goroutine stack.
|
||||||
func (di *DebugInfo) AddGoroutineStack() {
|
func (di *Info) AddGoroutineStack() {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
err := pprof.Lookup("goroutine").WriteTo(buf, 1)
|
err := pprof.Lookup("goroutine").WriteTo(buf, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -147,7 +147,7 @@ func (di *DebugInfo) AddGoroutineStack() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLastReportedModuleError adds the last reported module error, if one exists.
|
// AddLastReportedModuleError adds the last reported module error, if one exists.
|
||||||
func (di *DebugInfo) AddLastReportedModuleError() {
|
func (di *Info) AddLastReportedModuleError() {
|
||||||
me := modules.GetLastReportedError()
|
me := modules.GetLastReportedError()
|
||||||
if me == nil {
|
if me == nil {
|
||||||
di.AddSection("No Module Error", NoFlags)
|
di.AddSection("No Module Error", NoFlags)
|
||||||
|
@ -162,7 +162,7 @@ func (di *DebugInfo) AddLastReportedModuleError() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLastUnexpectedLogs adds the last 10 unexpected log lines, if any.
|
// AddLastUnexpectedLogs adds the last 10 unexpected log lines, if any.
|
||||||
func (di *DebugInfo) AddLastUnexpectedLogs() {
|
func (di *Info) AddLastUnexpectedLogs() {
|
||||||
lines := log.GetLastUnexpectedLogs()
|
lines := log.GetLastUnexpectedLogs()
|
||||||
|
|
||||||
// Check if there is anything at all.
|
// Check if there is anything at all.
|
||||||
|
|
|
@ -11,7 +11,7 @@ var (
|
||||||
nameOnly = regexp.MustCompile("^[A-Za-z0-9]+$")
|
nameOnly = regexp.MustCompile("^[A-Za-z0-9]+$")
|
||||||
delimitersAtStart = regexp.MustCompile("^[^A-Za-z0-9]+")
|
delimitersAtStart = regexp.MustCompile("^[^A-Za-z0-9]+")
|
||||||
delimitersOnly = regexp.MustCompile("^[^A-Za-z0-9]+$")
|
delimitersOnly = regexp.MustCompile("^[^A-Za-z0-9]+$")
|
||||||
cleanName = regexp.MustCompile(`["']`)
|
removeQuotes = strings.NewReplacer(`"`, ``, `'`, ``)
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenerateBinaryNameFromPath generates a more human readable binary name from
|
// GenerateBinaryNameFromPath generates a more human readable binary name from
|
||||||
|
@ -70,7 +70,7 @@ func cleanFileDescription(fileDescr string) string {
|
||||||
|
|
||||||
// Clean out and `"` and `'`.
|
// Clean out and `"` and `'`.
|
||||||
for i := range fields {
|
for i := range fields {
|
||||||
fields[i] = cleanName.ReplaceAllString(fields[i], "")
|
fields[i] = removeQuotes.Replace(fields[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is a 1 or 2 character delimiter field, only use fields before it.
|
// If there is a 1 or 2 character delimiter field, only use fields before it.
|
||||||
|
|
Loading…
Add table
Reference in a new issue