From 1e87471c10b1c4e3eb2a94995d53740518a1b409 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 10 Apr 2022 16:29:04 +0100 Subject: [PATCH] use the vcs information from ReadBuildInfo in Go 1.18 --- p2p/protocol/identify/id.go | 14 ---------- p2p/protocol/identify/id_go117.go | 23 +++++++++++++++++ p2p/protocol/identify/id_go118.go | 43 +++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 p2p/protocol/identify/id_go117.go create mode 100644 p2p/protocol/identify/id_go118.go diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 70ff28b63..15e778e4a 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "runtime/debug" "sync" "time" @@ -55,19 +54,6 @@ var ( defaultUserAgent = "github.com/libp2p/go-libp2p" ) -func init() { - bi, ok := debug.ReadBuildInfo() - if !ok { - return - } - version := bi.Main.Version - if version == "(devel)" { - defaultUserAgent = bi.Main.Path - } else { - defaultUserAgent = fmt.Sprintf("%s@%s", bi.Main.Path, bi.Main.Version) - } -} - type addPeerHandlerReq struct { rp peer.ID resp chan *peerHandler diff --git a/p2p/protocol/identify/id_go117.go b/p2p/protocol/identify/id_go117.go new file mode 100644 index 000000000..42d6ff5b6 --- /dev/null +++ b/p2p/protocol/identify/id_go117.go @@ -0,0 +1,23 @@ +//go:build !go1.18 +// +build !go1.18 + +package identify + +import ( + "fmt" + "runtime/debug" +) + +func init() { + bi, ok := debug.ReadBuildInfo() + // ok will only be true if this is built as a dependency of another module + if !ok { + return + } + version := bi.Main.Version + if version == "(devel)" { + defaultUserAgent = bi.Main.Path + } else { + defaultUserAgent = fmt.Sprintf("%s@%s", bi.Main.Path, bi.Main.Version) + } +} diff --git a/p2p/protocol/identify/id_go118.go b/p2p/protocol/identify/id_go118.go new file mode 100644 index 000000000..439adf96f --- /dev/null +++ b/p2p/protocol/identify/id_go118.go @@ -0,0 +1,43 @@ +//go:build go1.18 +// +build go1.18 + +package identify + +import ( + "fmt" + "runtime/debug" +) + +func init() { + bi, ok := debug.ReadBuildInfo() + if !ok { + return + } + version := bi.Main.Version + // version will only be non-empty if built as a dependency of another module + if version == "" { + return + } + + if version != "(devel)" { + defaultUserAgent = fmt.Sprintf("%s@%s", bi.Main.Path, bi.Main.Version) + return + } + + var revision string + var dirty bool + for _, bs := range bi.Settings { + switch bs.Key { + case "vcs.revision": + revision = bs.Value + case "vcs.modified": + if bs.Value == "true" { + dirty = true + } + } + } + defaultUserAgent = fmt.Sprintf("%s@%s", bi.Main.Path, revision) + if dirty { + defaultUserAgent += " (dirty)" + } +}