Skip to content

Commit 3a84352

Browse files
authored
feat(cli): add daemon option --agent-version-suffix (#8419)
* feat(cli): add daemon option --agent-version-suffix * fix sharness test when commit is empty (release)
1 parent c891109 commit 3a84352

File tree

7 files changed

+48
-8
lines changed

7 files changed

+48
-8
lines changed

cmd/ipfs/daemon.go

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const (
6767
enablePubSubKwd = "enable-pubsub-experiment"
6868
enableIPNSPubSubKwd = "enable-namesys-pubsub"
6969
enableMultiplexKwd = "enable-mplex-experiment"
70+
agentVersionSuffix = "agent-version-suffix"
7071
// apiAddrKwd = "address-api"
7172
// swarmAddrKwd = "address-swarm"
7273
)
@@ -180,6 +181,7 @@ Headers.
180181
cmds.BoolOption(enablePubSubKwd, "Instantiate the ipfs daemon with the experimental pubsub feature enabled."),
181182
cmds.BoolOption(enableIPNSPubSubKwd, "Enable IPNS record distribution through pubsub; enables pubsub."),
182183
cmds.BoolOption(enableMultiplexKwd, "DEPRECATED"),
184+
cmds.StringOption(agentVersionSuffix, "Optional suffix to the AgentVersion presented by `ipfs id` and also advertised through BitSwap."),
183185

184186
// TODO: add way to override addresses. tricky part: updating the config if also --init.
185187
// cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"),
@@ -410,6 +412,11 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
410412
return fmt.Errorf("unrecognized routing option: %s", routingOption)
411413
}
412414

415+
agentVersionSuffixString, _ := req.Options[agentVersionSuffix].(string)
416+
if agentVersionSuffixString != "" {
417+
version.SetUserAgentSuffix(agentVersionSuffixString)
418+
}
419+
413420
node, err := core.NewNode(req.Context, ncfg)
414421
if err != nil {
415422
log.Error("error from node construction: ", err)

core/commands/id.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,6 @@ func printSelf(keyEnc ke.KeyEncoder, node *core.IpfsNode) (interface{}, error) {
223223
sort.Strings(info.Protocols)
224224
}
225225
info.ProtocolVersion = identify.LibP2PVersion
226-
info.AgentVersion = version.UserAgent
226+
info.AgentVersion = version.GetUserAgentVersion()
227227
return info, nil
228228
}

core/corehttp/gateway.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func VersionOption() ServeOption {
104104
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
105105
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
106106
fmt.Fprintf(w, "Commit: %s\n", version.CurrentCommit)
107-
fmt.Fprintf(w, "Client Version: %s\n", version.UserAgent)
107+
fmt.Fprintf(w, "Client Version: %s\n", version.GetUserAgentVersion())
108108
fmt.Fprintf(w, "Protocol Version: %s\n", id.LibP2PVersion)
109109
})
110110
return mux, nil

core/corehttp/gateway_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ func TestVersion(t *testing.T) {
732732
t.Fatalf("response doesn't contain commit:\n%s", s)
733733
}
734734

735-
if !strings.Contains(s, "Client Version: "+version.UserAgent) {
735+
if !strings.Contains(s, "Client Version: "+version.GetUserAgentVersion()) {
736736
t.Fatalf("response doesn't contain client version:\n%s", s)
737737
}
738738

core/node/libp2p/libp2p.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ type Libp2pOpts struct {
2525
}
2626

2727
// Misc options
28-
29-
var UserAgent = simpleOpt(libp2p.UserAgent(version.UserAgent))
28+
var UserAgent = simpleOpt(libp2p.UserAgent(version.GetUserAgentVersion()))
3029

3130
func ConnectionManager(low, high int, grace time.Duration) func() (opts Libp2pOpts, err error) {
3231
return func() (opts Libp2pOpts, err error) {

test/sharness/t0026-id.sh

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@ test_description="Test to make sure our identity information looks sane"
77
test_init_ipfs
88

99
test_id_compute_agent() {
10+
local AGENT_SUFFIX
11+
AGENT_SUFFIX=$1
1012
AGENT_VERSION="$(ipfs version --number)" || return 1
1113
AGENT_COMMIT="$(ipfs version --number --commit)" || return 1
1214
if test "$AGENT_COMMIT" = "$AGENT_VERSION"; then
1315
AGENT_COMMIT=""
1416
else
1517
AGENT_COMMIT="${AGENT_COMMIT##$AGENT_VERSION-}"
1618
fi
17-
echo "go-ipfs/$AGENT_VERSION/$AGENT_COMMIT"
19+
AGENT_VERSION="go-ipfs/$AGENT_VERSION/$AGENT_COMMIT"
20+
if test -n "$AGENT_SUFFIX"; then
21+
if test -n "$AGENT_COMMIT"; then
22+
AGENT_VERSION="$AGENT_VERSION/"
23+
fi
24+
AGENT_VERSION="$AGENT_VERSION$AGENT_SUFFIX"
25+
fi
26+
echo "$AGENT_VERSION"
1827
}
1928

2029
test_expect_success "checking AgentVersion" '
@@ -23,6 +32,16 @@ test_expect_success "checking AgentVersion" '
2332
test_cmp expected-agent-version actual-agent-version
2433
'
2534

35+
test_launch_ipfs_daemon_without_network --agent-version-suffix=test-suffix
36+
37+
test_expect_success "checking AgentVersion with suffix (daemon running)" '
38+
test_id_compute_agent test-suffix > expected-agent-version &&
39+
ipfs id -f "<aver>\n" > actual-agent-version &&
40+
test_cmp expected-agent-version actual-agent-version
41+
'
42+
43+
test_kill_ipfs_daemon
44+
2645
test_expect_success "checking ProtocolVersion" '
2746
echo "ipfs/0.1.0" > expected-protocol-version &&
2847
ipfs id -f "<pver>\n" > actual-protocol-version &&

version.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@ const CurrentVersionNumber = "0.11.0-dev"
88

99
const ApiVersion = "/go-ipfs/" + CurrentVersionNumber + "/"
1010

11-
// UserAgent is the libp2p user agent used by go-ipfs.
11+
// GetUserAgentVersion is the libp2p user agent used by go-ipfs.
1212
//
1313
// Note: This will end in `/` when no commit is available. This is expected.
14-
var UserAgent = "go-ipfs/" + CurrentVersionNumber + "/" + CurrentCommit
14+
func GetUserAgentVersion() string {
15+
userAgent := "go-ipfs/" + CurrentVersionNumber + "/" + CurrentCommit
16+
if userAgentSuffix != "" {
17+
if CurrentCommit != "" {
18+
userAgent += "/"
19+
}
20+
userAgent += userAgentSuffix
21+
}
22+
return userAgent
23+
}
24+
25+
var userAgentSuffix string
26+
27+
func SetUserAgentSuffix(suffix string) {
28+
userAgentSuffix = suffix
29+
}

0 commit comments

Comments
 (0)