Skip to content

Commit

Permalink
go-vet friendly codebase
Browse files Browse the repository at this point in the history
- distinguish log.Error and log.Errorf functions
- Initialize structs with field names
- A bit of unreachable code (defers)
  • Loading branch information
jbenet committed Oct 25, 2014
1 parent 210b5df commit 184c254
Show file tree
Hide file tree
Showing 38 changed files with 187 additions and 188 deletions.
3 changes: 2 additions & 1 deletion blocks/set/dbset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type datastoreBlockSet struct {
bset BlockSet
}

// NewDBWrapperSet returns a new blockset wrapping a given datastore
func NewDBWrapperSet(d ds.Datastore, bset BlockSet) BlockSet {
return &datastoreBlockSet{
dstore: d,
Expand All @@ -21,7 +22,7 @@ func NewDBWrapperSet(d ds.Datastore, bset BlockSet) BlockSet {
func (d *datastoreBlockSet) AddBlock(k util.Key) {
err := d.dstore.Put(k.DsKey(), []byte{})
if err != nil {
log.Error("blockset put error: %s", err)
log.Errorf("blockset put error: %s", err)
}

d.bset.AddBlock(k)
Expand Down
8 changes: 4 additions & 4 deletions commands/cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func TestOptionParsing(t *testing.T) {
t.Error("Should have passed")
}
if len(opts) != 4 || opts["beep"] != "" || opts["boop"] != "lol" || opts["c"] != "" || opts["foo"] != "5" {
t.Error("Returned options were defferent than expected: %v", opts)
t.Errorf("Returned options were defferent than expected: %v", opts)
}
if len(input) != 2 || input[0] != "test2" || input[1] != "beep" {
t.Error("Returned input was different than expected: %v", input)
t.Errorf("Returned input was different than expected: %v", input)
}

_, _, err = parseOptions([]string{"-beep=1", "-boop=2", "-beep=3"})
Expand All @@ -39,9 +39,9 @@ func TestOptionParsing(t *testing.T) {

path, args := parsePath([]string{"test", "beep", "boop"}, cmd)
if len(path) != 1 || path[0] != "test" {
t.Error("Returned path was defferent than expected: %v", path)
t.Errorf("Returned path was defferent than expected: %v", path)
}
if len(args) != 2 || args[0] != "beep" || args[1] != "boop" {
t.Error("Returned args were different than expected: %v", args)
t.Errorf("Returned args were different than expected: %v", args)
}
}
4 changes: 2 additions & 2 deletions commands/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func TestOptionValidation(t *testing.T) {
req = NewEmptyRequest()
req.SetOption("b", ":)")
res = cmd.Call(req)
if res.Error == nil {
t.Error(res.Error, "Should have failed (string value not convertible to int)")
if res.Error() == nil {
t.Error(res.Error(), "Should have failed (string value not convertible to int)")
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func printRefs(n *core.IpfsNode, nd *mdag.Node, refSeen map[u.Key]bool, recursiv
if recursive {
nd, err := n.DAG.Get(u.Key(link.Hash))
if err != nil {
log.Error("error: cannot retrieve %s (%s)\n", link.Hash.B58String(), err)
log.Errorf("error: cannot retrieve %s (%s)", link.Hash.B58String(), err)
return
}

Expand Down
10 changes: 5 additions & 5 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"

context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"

bserv "github.com/jbenet/go-ipfs/blockservice"
Expand Down Expand Up @@ -230,25 +230,25 @@ func initIdentity(cfg *config.Config, peers peer.Peerstore, online bool) (peer.P
func initConnections(ctx context.Context, cfg *config.Config, pstore peer.Peerstore, route *dht.IpfsDHT) {
for _, p := range cfg.Bootstrap {
if p.PeerID == "" {
log.Error("error: peer does not include PeerID. %v", p)
log.Errorf("error: peer does not include PeerID. %v", p)
}

maddr, err := ma.NewMultiaddr(p.Address)
if err != nil {
log.Error("%s", err)
log.Error(err)
continue
}

// setup peer
npeer, err := pstore.Get(peer.DecodePrettyID(p.PeerID))
if err != nil {
log.Error("Bootstrapping error: %v", err)
log.Errorf("Bootstrapping error: %v", err)
continue
}
npeer.AddAddress(maddr)

if _, err = route.Connect(ctx, npeer); err != nil {
log.Error("Bootstrapping error: %v", err)
log.Errorf("Bootstrapping error: %v", err)
}
}
}
6 changes: 3 additions & 3 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func NewDaemonListener(ipfsnode *core.IpfsNode, addr ma.Multiaddr, confdir strin

ofi, err := os.Create(confdir + "/rpcaddress")
if err != nil {
log.Warning("Could not create rpcaddress file: %s", err)
log.Warningf("Could not create rpcaddress file: %s", err)
return nil, err
}

_, err = ofi.Write([]byte(addr.String()))
if err != nil {
log.Warning("Could not write to rpcaddress file: %s", err)
log.Warningf("Could not write to rpcaddress file: %s", err)
return nil, err
}
ofi.Close()
Expand Down Expand Up @@ -148,7 +148,7 @@ func (dl *DaemonListener) handleConnection(conn manet.Conn) {
err = fmt.Errorf("Invalid Command: '%s'", command.Command)
}
if err != nil {
log.Error("%s: %s", command.Command, err)
log.Errorf("%s: %s", command.Command, err)
fmt.Fprintln(conn, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func serverIsRunning(confdir string) bool {
var err error
confdir, err = u.TildeExpansion(confdir)
if err != nil {
log.Error("Tilde Expansion Failed: %s", err)
log.Errorf("Tilde Expansion Failed: %s", err)
return false
}
lk, err := daemonLock(confdir)
Expand Down
26 changes: 13 additions & 13 deletions diagnostics/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (d *Diagnostics) GetDiagnostic(timeout time.Duration) ([]*DiagInfo, error)
log.Debug("Begin Diagnostic")

peers := d.getPeers()
log.Debug("Sending diagnostic request to %d peers.", len(peers))
log.Debugf("Sending diagnostic request to %d peers.", len(peers))

var out []*DiagInfo
di := d.getDiagInfo()
Expand All @@ -116,12 +116,12 @@ func (d *Diagnostics) GetDiagnostic(timeout time.Duration) ([]*DiagInfo, error)
respdata := make(chan []byte)
sends := 0
for _, p := range peers {
log.Debug("Sending getDiagnostic to: %s", p)
log.Debugf("Sending getDiagnostic to: %s", p)
sends++
go func(p peer.Peer) {
data, err := d.getDiagnosticFromPeer(ctx, p, pmes)
if err != nil {
log.Error("GetDiagnostic error: %v", err)
log.Errorf("GetDiagnostic error: %v", err)
respdata <- nil
return
}
Expand All @@ -147,7 +147,7 @@ func AppendDiagnostics(data []byte, cur []*DiagInfo) []*DiagInfo {
err := dec.Decode(di)
if err != nil {
if err != io.EOF {
log.Error("error decoding DiagInfo: %v", err)
log.Errorf("error decoding DiagInfo: %v", err)
}
break
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func (d *Diagnostics) sendRequest(ctx context.Context, p peer.Peer, pmes *pb.Mes
}

rtt := time.Since(start)
log.Info("diagnostic request took: %s", rtt.String())
log.Infof("diagnostic request took: %s", rtt.String())

rpmes := new(pb.Message)
if err := proto.Unmarshal(rmes.Data(), rpmes); err != nil {
Expand All @@ -200,7 +200,7 @@ func (d *Diagnostics) sendRequest(ctx context.Context, p peer.Peer, pmes *pb.Mes
}

func (d *Diagnostics) handleDiagnostic(p peer.Peer, pmes *pb.Message) (*pb.Message, error) {
log.Debug("HandleDiagnostic from %s for id = %s", p, pmes.GetDiagID())
log.Debugf("HandleDiagnostic from %s for id = %s", p, pmes.GetDiagID())
resp := newMessage(pmes.GetDiagID())
d.diagLock.Lock()
_, found := d.diagMap[pmes.GetDiagID()]
Expand All @@ -220,12 +220,12 @@ func (d *Diagnostics) handleDiagnostic(p peer.Peer, pmes *pb.Message) (*pb.Messa
respdata := make(chan []byte)
sendcount := 0
for _, p := range d.getPeers() {
log.Debug("Sending diagnostic request to peer: %s", p)
log.Debugf("Sending diagnostic request to peer: %s", p)
sendcount++
go func(p peer.Peer) {
out, err := d.getDiagnosticFromPeer(ctx, p, pmes)
if err != nil {
log.Error("getDiagnostic error: %v", err)
log.Errorf("getDiagnostic error: %v", err)
respdata <- nil
return
}
Expand All @@ -237,7 +237,7 @@ func (d *Diagnostics) handleDiagnostic(p peer.Peer, pmes *pb.Message) (*pb.Messa
out := <-respdata
_, err := buf.Write(out)
if err != nil {
log.Error("getDiagnostic write output error: %v", err)
log.Errorf("getDiagnostic write output error: %v", err)
continue
}
}
Expand All @@ -263,18 +263,18 @@ func (d *Diagnostics) HandleMessage(ctx context.Context, mes msg.NetMessage) msg
pmes := new(pb.Message)
err := proto.Unmarshal(mData, pmes)
if err != nil {
log.Error("Failed to decode protobuf message: %v", err)
log.Errorf("Failed to decode protobuf message: %v", err)
return nil
}

// Print out diagnostic
log.Info("[peer: %s] Got message from [%s]\n",
log.Infof("[peer: %s] Got message from [%s]\n",
d.self.ID().Pretty(), mPeer.ID().Pretty())

// dispatch handler.
rpmes, err := d.handleDiagnostic(mPeer, pmes)
if err != nil {
log.Error("handleDiagnostic error: %s", err)
log.Errorf("handleDiagnostic error: %s", err)
return nil
}

Expand All @@ -286,7 +286,7 @@ func (d *Diagnostics) HandleMessage(ctx context.Context, mes msg.NetMessage) msg
// serialize response msg
rmes, err := msg.FromObject(mPeer, rpmes)
if err != nil {
log.Error("Failed to encode protobuf message: %v", err)
log.Errorf("Failed to encode protobuf message: %v", err)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions exchange/bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error)
log.Debug("bitswap dialing peer: %s", p)
err := bs.sender.DialPeer(p)
if err != nil {
log.Error("Error sender.DialPeer(%s)", p)
log.Errorf("Error sender.DialPeer(%s)", p)
return
}

response, err := bs.sender.SendRequest(ctx, p, message)
if err != nil {
log.Error("Error sender.SendRequest(%s)", p)
log.Errorf("Error sender.SendRequest(%s)", p)
return
}
// FIXME ensure accounting is handled correctly when
Expand Down
8 changes: 4 additions & 4 deletions fuse/ipns/ipns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func TestFastRepublish(t *testing.T) {
hasPublished := func() bool {
res, err := node.Namesys.Resolve(pubkeyHash)
if err != nil {
t.Fatal("resolve err: %v", err)
t.Fatalf("resolve err: %v", err)
}
return res != resolvedHash
}
Expand All @@ -264,7 +264,7 @@ func TestFastRepublish(t *testing.T) {
// at this point, should not have written dataA and not have written dataB
rbuf, err := ioutil.ReadFile(fname)
if err != nil || !bytes.Equal(rbuf, dataA) {
t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
t.Fatalf("Data inconsistent! %v %v", err, string(rbuf))
}

if hasPublished() {
Expand All @@ -276,7 +276,7 @@ func TestFastRepublish(t *testing.T) {
// at this point, should have written written dataB, but not published it
rbuf, err = ioutil.ReadFile(fname)
if err != nil || !bytes.Equal(rbuf, dataB) {
t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
t.Fatalf("Data inconsistent! %v %v", err, string(rbuf))
}

if hasPublished() {
Expand All @@ -288,7 +288,7 @@ func TestFastRepublish(t *testing.T) {
// at this point, should have written written dataB, and published it
rbuf, err = ioutil.ReadFile(fname)
if err != nil || !bytes.Equal(rbuf, dataB) {
t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
t.Fatalf("Data inconsistent! %v %v", err, string(rbuf))
}

if !hasPublished() {
Expand Down
Loading

0 comments on commit 184c254

Please sign in to comment.