Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Convert sloghuman to key=value pair format
This format is consistent with Go's new official `slog`, `zerolog`
and many others. It is also far more readable than JSON, and a growing
number of libaries and tools are able to parse it.
  • Loading branch information
ammario committed May 7, 2023
commit 7ef97ddbd48c889ddaffc3c9816c9158faec460e
24 changes: 20 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
module cdr.dev/slog

go 1.13
go 1.20

require (
cloud.google.com/go v0.26.0
github.com/alecthomas/chroma v0.10.0
github.com/fatih/color v1.13.0
github.com/charmbracelet/lipgloss v0.7.1
github.com/google/go-cmp v0.5.3
github.com/muesli/termenv v0.15.1
go.opencensus.io v0.24.0
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/goleak v1.2.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/grpc v1.33.2 // indirect
google.golang.org/protobuf v1.25.0 // indirect
)
42 changes: 21 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand All @@ -39,21 +37,26 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs=
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
Expand Down Expand Up @@ -83,11 +86,10 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down Expand Up @@ -121,9 +123,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
162 changes: 57 additions & 105 deletions internal/entryhuman/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ package entryhuman

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"time"

"github.com/fatih/color"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"go.opencensus.io/trace"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/xerrors"
Expand All @@ -34,13 +32,20 @@ func StripTimestamp(ent string) (time.Time, string, error) {
// TimeFormat is a simplified RFC3339 format.
const TimeFormat = "2006-01-02 15:04:05.000"

func c(w io.Writer, attrs ...color.Attribute) *color.Color {
c := color.New(attrs...)
c.DisableColor()
var (
renderer = lipgloss.NewRenderer(os.Stdout, termenv.WithUnsafe())

loggerNameStyle = renderer.NewStyle().Foreground(lipgloss.Color("#A47DFF"))
keyStyle = renderer.NewStyle().Foreground(lipgloss.Color("#606366"))
multiLineKeyStyle = renderer.NewStyle().Foreground(lipgloss.Color("#79b8ff"))
)

func render(w io.Writer, st lipgloss.Style, s string) string {
if shouldColor(w) {
c.EnableColor()
ss := st.Render(s)
return ss
}
return c
return s
}

// Fmt returns a human readable format for ent.
Expand All @@ -50,36 +55,36 @@ func c(w io.Writer, attrs ...color.Attribute) *color.Color {
// We also do not indent the fields as go's test does that automatically
// for extra lines in a log so if we did it here, the fields would be indented
// twice in test logs. So the Stderr logger indents all the fields itself.
func Fmt(w io.Writer, ent slog.SinkEntry) string {
ents := c(w, color.Reset).Sprint("")
func Fmt(buf io.StringWriter, termW io.Writer, ent slog.SinkEntry,
) {
ts := ent.Time.Format(TimeFormat)
ents += ts + " "
buf.WriteString(ts + " ")

level := "[" + ent.Level.String() + "]"
level = c(w, levelColor(ent.Level)).Sprint(level)
ents += fmt.Sprintf("%v\t", level)
level := ent.Level.String()
if len(level) > 4 {
level = level[:4]
}
level = "[" + level + "]"
buf.WriteString(render(termW, levelStyle(ent.Level), level))
buf.WriteString("\t")

if len(ent.LoggerNames) > 0 {
loggerName := "(" + quoteKey(strings.Join(ent.LoggerNames, ".")) + ")"
loggerName = c(w, color.FgMagenta).Sprint(loggerName)
ents += fmt.Sprintf("%v\t", loggerName)
buf.WriteString(render(termW, loggerNameStyle, loggerName))
buf.WriteString("\t")
}

hpath, hfn := humanPathAndFunc(ent.File, ent.Func)
loc := fmt.Sprintf("<%v:%v>\t%v", hpath, ent.Line, hfn)
loc = c(w, color.FgCyan).Sprint(loc)
ents += fmt.Sprintf("%v\t", loc)

var multilineKey string
var multilineVal string
msg := strings.TrimSpace(ent.Message)
if strings.Contains(msg, "\n") {
multilineKey = "msg"
multilineVal = msg
msg = "..."
msg = quote(msg)
buf.WriteString(msg)

}
msg = quote(msg)
ents += msg

if ent.SpanContext != (trace.SpanContext{}) {
ent.Fields = append(slog.M(
Expand Down Expand Up @@ -111,48 +116,56 @@ func Fmt(w io.Writer, ent slog.SinkEntry) string {
multilineVal = s
}

if len(ent.Fields) > 0 {
// No error is guaranteed due to slog.Map handling errors itself.
fields, _ := json.MarshalIndent(ent.Fields, "", "")
fields = bytes.ReplaceAll(fields, []byte(",\n"), []byte(", "))
fields = bytes.ReplaceAll(fields, []byte("\n"), []byte(""))
fields = formatJSON(w, fields)
ents += "\t" + string(fields)
for i, f := range ent.Fields {
if i < len(ent.Fields) {
buf.WriteString("\t")
}
buf.WriteString(render(termW, keyStyle, f.Name+"="))
valueStr := fmt.Sprintf("%+v", f.Value)
buf.WriteString(quote(valueStr))
}

if multilineVal != "" {
if msg != "..." {
ents += " ..."
buf.WriteString(" ...")
}

// Proper indentation.
lines := strings.Split(multilineVal, "\n")
for i, line := range lines[1:] {
if line != "" {
lines[i+1] = c(w, color.Reset).Sprint("") + strings.Repeat(" ", len(multilineKey)+4) + line
lines[i+1] = strings.Repeat(" ", len(multilineKey)+4) + line
}
}
multilineVal = strings.Join(lines, "\n")

multilineKey = c(w, color.FgBlue).Sprintf(`"%v"`, multilineKey)
ents += fmt.Sprintf("\n%v: %v", multilineKey, multilineVal)
multilineKey = render(termW, multiLineKeyStyle, multilineKey)
buf.WriteString("\n")
buf.WriteString(multilineKey)
buf.WriteString("= ")
buf.WriteString(multilineVal)
}

return ents
}

func levelColor(level slog.Level) color.Attribute {
var (
levelDebugStyle = renderer.NewStyle().Foreground(lipgloss.Color("#ffffff"))
levelInfoStyle = renderer.NewStyle().Foreground(lipgloss.Color("#0091FF"))
levelWarnStyle = renderer.NewStyle().Foreground(lipgloss.Color("#FFCF0D"))
levelErrorStyle = renderer.NewStyle().Foreground(lipgloss.Color("#FF5A0D"))
)

func levelStyle(level slog.Level) lipgloss.Style {
switch level {
case slog.LevelDebug:
return color.Reset
return levelDebugStyle
case slog.LevelInfo:
return color.FgBlue
return levelInfoStyle
case slog.LevelWarn:
return color.FgYellow
case slog.LevelError:
return color.FgRed
return levelWarnStyle
case slog.LevelError, slog.LevelFatal, slog.LevelCritical:
return levelErrorStyle
default:
return color.FgHiRed
panic("unknown level")
}
}

Expand Down Expand Up @@ -196,64 +209,3 @@ func quoteKey(key string) string {
// Replace spaces in the map keys with underscores.
return strings.ReplaceAll(key, " ", "_")
}

var mainPackagePath string
var mainModulePath string

func init() {
// Unfortunately does not work for tests yet :(
// See https://github.com/golang/go/issues/33976
bi, ok := debug.ReadBuildInfo()
if !ok {
return
}
mainPackagePath = bi.Path
mainModulePath = bi.Main.Path
}

// humanPathAndFunc takes the absolute path to a file and an absolute module path to a
// function in that file and returns the module path to the file. It also returns
// the path to the function stripped of its module prefix.
//
// If the file is in the main Go module then its path is returned
// relative to the main Go module's root.
//
// fn is from https://pkg.go.dev/runtime#Func.Name
func humanPathAndFunc(filename, fn string) (hpath, hfn string) {
// pkgDir is the dir of the pkg.
// e.g. cdr.dev/slog/internal
// base is the package name and the function name separated by a period.
// e.g. entryhuman.humanPathAndFunc
// There can be multiple periods when methods of types are involved.
pkgDir, base := filepath.Split(fn)
s := strings.Split(base, ".")
pkg := s[0]
hfn = strings.Join(s[1:], ".")

if pkg == "main" {
// This happens with go build main.go
if mainPackagePath == "command-line-arguments" {
// Without a real mainPath, we can't find the path to the file
// relative to the module. So we just return the base.
return filepath.Base(filename), hfn
}
// Go doesn't return the full main path in runtime.Func.Name()
// It just returns the path "main"
// Only runtime.ReadBuildInfo returns it so we have to check and replace.
pkgDir = mainPackagePath
// pkg main isn't reflected on the disk so we should not add it
// into the pkgpath.
pkg = ""
}

hpath = filepath.Join(pkgDir, pkg, filepath.Base(filename))

if mainModulePath != "" {
relhpath, err := filepath.Rel(mainModulePath, hpath)
if err == nil {
hpath = "./" + relhpath
}
}

return hpath, hfn
}
Loading