Skip to content

Commit

Permalink
Merge pull request #1381 from libp2p/use-build-info
Browse files Browse the repository at this point in the history
use the vcs information from ReadBuildInfo in Go 1.18
  • Loading branch information
marten-seemann committed Apr 10, 2022
2 parents d347c86 + 8260908 commit 2ad1d3d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
14 changes: 0 additions & 14 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"runtime/debug"
"sync"
"time"

Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions p2p/protocol/identify/id_go117.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
46 changes: 46 additions & 0 deletions p2p/protocol/identify/id_go118.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//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
if len(revision) > 9 {
revision = revision[:9]
}
case "vcs.modified":
if bs.Value == "true" {
dirty = true
}
}
}
defaultUserAgent = fmt.Sprintf("%s@%s", bi.Main.Path, revision)
if dirty {
defaultUserAgent += "-dirty"
}
}

0 comments on commit 2ad1d3d

Please sign in to comment.