Skip to content

Commit

Permalink
refactor: use ridiculous getter
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Aug 17, 2023
1 parent 49c2924 commit a2cc131
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
74 changes: 45 additions & 29 deletions cmd/ipfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"runtime/pprof"
"strings"
"sync"
"time"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -323,13 +324,7 @@ func makeExecutor(req *cmds.Request, env interface{}) (cmds.Executor, error) {
Transport: otelhttp.NewTransport(tpt),
}
opts = append(opts, cmdhttp.ClientWithHTTPClient(httpClient))

// Fetch remove version, as some feature compatibility might change depending on it.
remoteVersion, err := getRemoteVersion(tracingWrappedExecutor{cmdhttp.NewClient(host, opts...)})
if err != nil {
return nil, fmt.Errorf("could not determine version of remote API: %w", err)
}
opts = append(opts, cmdhttp.ClientWithRawAbsPath(remoteVersion.LT(ipfs.EncodedAbsolutePathVersion)))
opts = append(opts, cmdhttp.ClientWithRawAbsPath(getRawAbsPath(tracingWrappedExecutor{cmdhttp.NewClient(host, opts...)})))

Check warning on line 327 in cmd/ipfs/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/main.go#L325-L327

Added lines #L325 - L327 were not covered by tests

return tracingWrappedExecutor{cmdhttp.NewClient(host, opts...)}, nil
}
Expand Down Expand Up @@ -438,31 +433,52 @@ func (nw nopWriter) Close() error {
return nil

Check warning on line 433 in cmd/ipfs/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/main.go#L432-L433

Added lines #L432 - L433 were not covered by tests
}

func getRemoteVersion(exe cmds.Executor) (*semver.Version, error) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
defer cancel()
func getRawAbsPath(exe cmds.Executor) func() (bool, error) {
var (
rawAbsPath bool
err error
once sync.Once
)

return func() (bool, error) {
once.Do(func() {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second*30))
defer cancel()

req, e := cmds.NewRequest(ctx, []string{"version"}, nil, nil, nil, Root)
if e != nil {
err = e
return
}

Check warning on line 452 in cmd/ipfs/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/main.go#L436-L452

Added lines #L436 - L452 were not covered by tests

req, err := cmds.NewRequest(ctx, []string{"version"}, nil, nil, nil, Root)
if err != nil {
return nil, err
}
var buf bytes.Buffer
re, e := cmds.NewWriterResponseEmitter(nopWriter{&buf}, req)
if e != nil {
err = e
return
}
err = exe.Execute(req, re, nil)
if e != nil {
err = e
return
}

Check warning on line 464 in cmd/ipfs/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/main.go#L454-L464

Added lines #L454 - L464 were not covered by tests

var buf bytes.Buffer
re, err := cmds.NewWriterResponseEmitter(nopWriter{&buf}, req)
if err != nil {
return nil, err
}
var out ipfs.VersionInfo
dec := json.NewDecoder(&buf)
if e := dec.Decode(&out); e != nil {
err = e
return
}

Check warning on line 471 in cmd/ipfs/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/main.go#L466-L471

Added lines #L466 - L471 were not covered by tests

err = exe.Execute(req, re, nil)
if err != nil {
return nil, err
}
remoteVersion, e := semver.New(out.Version)
if e != nil {
err = e
return
}

Check warning on line 477 in cmd/ipfs/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/main.go#L473-L477

Added lines #L473 - L477 were not covered by tests

var out ipfs.VersionInfo
dec := json.NewDecoder(&buf)
if err := dec.Decode(&out); err != nil {
return nil, err
}
rawAbsPath = remoteVersion.LT(ipfs.EncodedAbsolutePathVersion)

Check warning on line 479 in cmd/ipfs/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/main.go#L479

Added line #L479 was not covered by tests
})

return semver.New(out.Version)
return rawAbsPath, err

Check warning on line 482 in cmd/ipfs/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/main.go#L482

Added line #L482 was not covered by tests
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/ipfs/go-ds-measure v0.2.0
github.com/ipfs/go-fs-lock v0.0.7
github.com/ipfs/go-graphsync v0.14.4
github.com/ipfs/go-ipfs-cmds v0.9.1-0.20230817083650-931096e39dda
github.com/ipfs/go-ipfs-cmds v0.9.1-0.20230817092353-bc5d76dbe1a2
github.com/ipfs/go-ipld-format v0.5.0
github.com/ipfs/go-ipld-git v0.1.1
github.com/ipfs/go-ipld-legacy v0.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ github.com/ipfs/go-ipfs-blockstore v1.3.0 h1:m2EXaWgwTzAfsmt5UdJ7Is6l4gJcaM/A12X
github.com/ipfs/go-ipfs-blockstore v1.3.0/go.mod h1:KgtZyc9fq+P2xJUiCAzbRdhhqJHvsw8u2Dlqy2MyRTE=
github.com/ipfs/go-ipfs-blocksutil v0.0.1 h1:Eh/H4pc1hsvhzsQoMEP3Bke/aW5P5rVM1IWFJMcGIPQ=
github.com/ipfs/go-ipfs-chunker v0.0.5 h1:ojCf7HV/m+uS2vhUGWcogIIxiO5ubl5O57Q7NapWLY8=
github.com/ipfs/go-ipfs-cmds v0.9.1-0.20230817083650-931096e39dda h1:+QVJu/I1vFZUM/Q5gJ3uyB+in2aQmV9SKGKUPRPJVGE=
github.com/ipfs/go-ipfs-cmds v0.9.1-0.20230817083650-931096e39dda/go.mod h1:OMMX/4WtqvDh8VbIxWLVAWH6HkVy2WMjnsEzZZcgsdw=
github.com/ipfs/go-ipfs-cmds v0.9.1-0.20230817092353-bc5d76dbe1a2 h1:qKD3rV/dTXhAQdiG+6uCrdE5kM3MGYr7PBOFsI0w9ls=
github.com/ipfs/go-ipfs-cmds v0.9.1-0.20230817092353-bc5d76dbe1a2/go.mod h1:OMMX/4WtqvDh8VbIxWLVAWH6HkVy2WMjnsEzZZcgsdw=
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
Expand Down

0 comments on commit a2cc131

Please sign in to comment.