util.go 3 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
	"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
)

Jeromy's avatar
Jeromy committed
17
var format = "%{color}%{time:01-02 15:04:05.9999} %{shortfile} %{level}: %{color:reset}%{message}"
18 19 20 21 22 23 24

func init() {
	backend := logging.NewLogBackend(os.Stderr, "", 0)
	logging.SetBackend(backend)
	logging.SetFormatter(logging.MustStringFormatter(format))
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
25
// Debug is a global flag for debugging.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
26 27
var Debug bool

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

31
// ErrTimeout implies that a timeout has been triggered
Jeromy's avatar
Jeromy committed
32 33 34 35 36
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.")
37

38
// ErrNotFound is returned when a search fails to find anything
39
var ErrNotFound = ds.ErrNotFound
40

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
// Pretty returns Key in a b58 encoded string
45
func (k Key) Pretty() string {
46
	return b58.Encode([]byte(k))
47 48
}

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

54 55 56 57 58 59 60 61
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
62
// TildeExpansion expands a filename, which may begin with a tilde.
63 64 65 66 67 68 69 70 71 72 73 74 75
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
76
// PErr is a shorthand printing function to output to Stderr.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
77
func PErr(format string, a ...interface{}) {
78
	fmt.Fprintf(os.Stderr, format, a...)
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
79 80
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
81
// POut is a shorthand printing function to output to Stdout.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
82
func POut(format string, a ...interface{}) {
83
	fmt.Fprintf(os.Stdout, format, a...)
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
84 85
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
86 87
// 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
88 89 90 91 92 93
func DErr(format string, a ...interface{}) {
	if Debug {
		PErr(format, a...)
	}
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
94 95
// 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
96 97 98 99 100
func DOut(format string, a ...interface{}) {
	if Debug {
		POut(format, a...)
	}
}
101

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102
// ExpandPathnames takes a set of paths and turns them into absolute paths
103 104 105 106 107 108 109 110 111 112 113
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
}