From 4b2bfc3e8fe144401e8bf5bf42690d311c0ded34 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 8 Jan 2021 16:46:11 +0100 Subject: [PATCH] Implement review suggestions --- api/endpoints_debug.go | 2 +- utils/debug/debug.go | 32 ++++++++++++++++---------------- utils/osdetail/binmeta.go | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/api/endpoints_debug.go b/api/endpoints_debug.go index 93d440c..f1e1078 100644 --- a/api/endpoints_debug.go +++ b/api/endpoints_debug.go @@ -65,7 +65,7 @@ func printStack(_ *Request) (msg string, err error) { // debugInfo returns the debugging information for support requests. func debugInfo(ar *Request) (data []byte, err error) { // Create debug information helper. - di := new(debug.DebugInfo) + di := new(debug.Info) di.Style = ar.Request.URL.Query().Get("style") // Add debug information. diff --git a/utils/debug/debug.go b/utils/debug/debug.go index 7b3084c..0c1c420 100644 --- a/utils/debug/debug.go +++ b/utils/debug/debug.go @@ -13,40 +13,40 @@ import ( "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, // so you can also use all these functions too. -type DebugInfo struct { +type Info struct { bytes.Buffer Style string } -// DebugInfoFlag defines possible options for adding sections to a DebugInfo. -type DebugInfoFlag int +// InfoFlag defines possible options for adding sections to a Info. +type InfoFlag int const ( // NoFlags does nothing. - NoFlags DebugInfoFlag = 0 + NoFlags InfoFlag = 0 // 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, // except for the last. - AddContentLineBreaks DebugInfoFlag = 2 + AddContentLineBreaks InfoFlag = 2 ) -func useCodeSection(flags DebugInfoFlag) bool { +func useCodeSection(flags InfoFlag) bool { return flags&UseCodeSection > 0 } -func addContentLineBreaks(flags DebugInfoFlag) bool { +func addContentLineBreaks(flags InfoFlag) bool { 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. -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. if di.Len() > 0 { 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. -func (di *DebugInfo) AddVersionInfo() { +func (di *Info) AddVersionInfo() { di.AddSection( "Version "+info.Version(), UseCodeSection, @@ -93,7 +93,7 @@ func (di *DebugInfo) AddVersionInfo() { } // 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. info, err := host.InfoWithContext(ctx) if err != nil { @@ -126,7 +126,7 @@ func (di *DebugInfo) AddPlatformInfo(ctx context.Context) { } // AddGoroutineStack adds the current goroutine stack. -func (di *DebugInfo) AddGoroutineStack() { +func (di *Info) AddGoroutineStack() { buf := new(bytes.Buffer) err := pprof.Lookup("goroutine").WriteTo(buf, 1) if err != nil { @@ -147,7 +147,7 @@ func (di *DebugInfo) AddGoroutineStack() { } // AddLastReportedModuleError adds the last reported module error, if one exists. -func (di *DebugInfo) AddLastReportedModuleError() { +func (di *Info) AddLastReportedModuleError() { me := modules.GetLastReportedError() if me == nil { di.AddSection("No Module Error", NoFlags) @@ -162,7 +162,7 @@ func (di *DebugInfo) AddLastReportedModuleError() { } // AddLastUnexpectedLogs adds the last 10 unexpected log lines, if any. -func (di *DebugInfo) AddLastUnexpectedLogs() { +func (di *Info) AddLastUnexpectedLogs() { lines := log.GetLastUnexpectedLogs() // Check if there is anything at all. diff --git a/utils/osdetail/binmeta.go b/utils/osdetail/binmeta.go index 391be99..7ab9ccd 100644 --- a/utils/osdetail/binmeta.go +++ b/utils/osdetail/binmeta.go @@ -11,7 +11,7 @@ var ( nameOnly = regexp.MustCompile("^[A-Za-z0-9]+$") delimitersAtStart = 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 @@ -70,7 +70,7 @@ func cleanFileDescription(fileDescr string) string { // Clean out and `"` and `'`. 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.