util.go 1.64 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 4

import (
	"fmt"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	mh "github.com/jbenet/go-multihash"
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
6
	"os"
7 8
	"os/user"
	"strings"
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
9 10
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
11
// Debug is a global flag for debugging.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
12 13
var Debug bool

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
14 15 16
// ErrNotImplemented signifies a function has not been implemented yet.
var ErrNotImplemented = fmt.Errorf("Error: not implemented yet.")

17 18 19
// ErrTimeout implies that a timeout has been triggered
var ErrTimeout = fmt.Errorf("Error: Call timed out.")

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

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
28
// TildeExpansion expands a filename, which may begin with a tilde.
29 30 31 32 33 34 35 36 37 38 39 40 41
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
42
// PErr is a shorthand printing function to output to Stderr.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
43
func PErr(format string, a ...interface{}) {
44
	fmt.Fprintf(os.Stderr, format + "\n", a...)
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
45 46
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
47
// POut is a shorthand printing function to output to Stdout.
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
48
func POut(format string, a ...interface{}) {
49
	fmt.Fprintf(os.Stdout, format + "\n", a...)
Juan Batiz-Benet's avatar
util  
Juan Batiz-Benet committed
50 51
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
52 53
// 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
54 55 56 57 58 59
func DErr(format string, a ...interface{}) {
	if Debug {
		PErr(format, a...)
	}
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
60 61
// 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
62 63 64 65 66
func DOut(format string, a ...interface{}) {
	if Debug {
		POut(format, a...)
	}
}