util.go 3.65 KB
Newer Older
Juan Batiz-Benet's avatar
key  
Juan Batiz-Benet committed
1
package util
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
2 3

import (
Jeromy's avatar
Jeromy committed
4
	"errors"
5
	"fmt"
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
6
	"os"
7
	"os/user"
8
	"path/filepath"
9
	"strings"
10

11
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
12 13
	b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
15 16
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18
// LogFormat is the format used for our logger.
var LogFormat = "%{color}%{time:01-02 15:04:05.9999} %{shortfile} %{level}: %{color:reset}%{message}"
19

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
20
// Debug is a global flag for debugging.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
21 22
var Debug bool

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
23
// ErrNotImplemented signifies a function has not been implemented yet.
Jeromy's avatar
Jeromy committed
24
var ErrNotImplemented = errors.New("Error: not implemented yet.")
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
25

26
// ErrTimeout implies that a timeout has been triggered
Jeromy's avatar
Jeromy committed
27 28 29 30 31
var ErrTimeout = errors.New("Error: Call timed out.")

// ErrSeErrSearchIncomplete implies that a search type operation didnt
// find the expected node, but did find 'a' node.
var ErrSearchIncomplete = errors.New("Error: Search Incomplete.")
32

33
// ErrNotFound is returned when a search fails to find anything
34
var ErrNotFound = ds.ErrNotFound
35

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
36
// Key is a string representation of multihash for use with maps.
Juan Batiz-Benet's avatar
key  
Juan Batiz-Benet committed
37 38
type Key string

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39
// Pretty returns Key in a b58 encoded string
40
func (k Key) Pretty() string {
41
	return b58.Encode([]byte(k))
42 43
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46 47 48 49 50 51 52 53 54 55
// DsKey returns a Datastore key
func (k Key) DsKey() ds.Key {
	return ds.NewKey(k.Pretty())
}

// KeyFromDsKey returns a Datastore key
func KeyFromDsKey(dsk ds.Key) Key {
	l := dsk.List()
	enc := l[len(l)-1]
	return Key(b58.Decode(enc))
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
56
// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58 59 60
func Hash(data []byte) (mh.Multihash, error) {
	return mh.Sum(data, mh.SHA2_256, -1)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
62 63 64 65 66 67 68 69
func IsValidHash(s string) bool {
	out := b58.Decode(s)
	if out == nil || len(out) == 0 {
		return false
	}
	return true
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
70
// TildeExpansion expands a filename, which may begin with a tilde.
71 72 73 74 75 76 77 78 79 80 81 82 83
func TildeExpansion(filename string) (string, error) {
	if strings.HasPrefix(filename, "~/") {
		usr, err := user.Current()
		if err != nil {
			return "", err
		}

		dir := usr.HomeDir + "/"
		filename = strings.Replace(filename, "~/", dir, 1)
	}
	return filename, nil
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
84
// PErr is a shorthand printing function to output to Stderr.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
85
func PErr(format string, a ...interface{}) {
86
	fmt.Fprintf(os.Stderr, format, a...)
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
87 88
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
89
// POut is a shorthand printing function to output to Stdout.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
90
func POut(format string, a ...interface{}) {
91
	fmt.Fprintf(os.Stdout, format, a...)
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
92 93
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
94 95
// DErr is a shorthand debug printing function to output to Stderr.
// Will only print if Debug is true.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
96 97 98 99 100 101
func DErr(format string, a ...interface{}) {
	if Debug {
		PErr(format, a...)
	}
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
102 103
// DOut is a shorthand debug printing function to output to Stdout.
// Will only print if Debug is true.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
104 105 106 107 108
func DOut(format string, a ...interface{}) {
	if Debug {
		POut(format, a...)
	}
}
109

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111 112 113
// SetupLogging will initialize the logger backend and set the flags.
func SetupLogging() {
	backend := logging.NewLogBackend(os.Stderr, "", 0)
	logging.SetBackend(backend)
Jeromy's avatar
Jeromy committed
114 115 116 117 118 119 120 121 122
	/*
		if Debug {
			logging.SetLevel(logging.DEBUG, "")
		} else {
			logging.SetLevel(logging.ERROR, "")
		}
	*/
	logging.SetLevel(logging.ERROR, "merkledag")
	logging.SetLevel(logging.ERROR, "blockservice")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
123 124 125
	logging.SetFormatter(logging.MustStringFormatter(LogFormat))
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126
// ExpandPathnames takes a set of paths and turns them into absolute paths
127 128 129 130 131 132 133 134 135 136 137
func ExpandPathnames(paths []string) ([]string, error) {
	var out []string
	for _, p := range paths {
		abspath, err := filepath.Abs(p)
		if err != nil {
			return nil, err
		}
		out = append(out, abspath)
	}
	return out, nil
}