github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands/diag.go (about)

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  	"time"
     9  
    10  	"github.com/jbenet/go-ipfs/core"
    11  	diagn "github.com/jbenet/go-ipfs/diagnostics"
    12  )
    13  
    14  func PrintDiagnostics(info []*diagn.DiagInfo, out io.Writer) {
    15  	for _, i := range info {
    16  		fmt.Fprintf(out, "Peer: %s\n", i.ID)
    17  		fmt.Fprintf(out, "\tUp for: %s\n", i.LifeSpan.String())
    18  		fmt.Fprintf(out, "\tConnected To:\n")
    19  		for _, c := range i.Connections {
    20  			fmt.Fprintf(out, "\t%s\n\t\tLatency = %s\n", c.ID, c.Latency.String())
    21  		}
    22  		fmt.Fprintln(out)
    23  	}
    24  
    25  }
    26  
    27  func Diag(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
    28  	if n.Diagnostics == nil {
    29  		return errors.New("Cannot run diagnostic in offline mode!")
    30  	}
    31  	info, err := n.Diagnostics.GetDiagnostic(time.Second * 20)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	raw, ok := opts["raw"].(bool)
    36  	if !ok {
    37  		return errors.New("incorrect value to parameter 'raw'")
    38  	}
    39  	if raw {
    40  		enc := json.NewEncoder(out)
    41  		err = enc.Encode(info)
    42  		if err != nil {
    43  			return err
    44  		}
    45  	} else {
    46  		PrintDiagnostics(info, out)
    47  	}
    48  	return nil
    49  }