daemon_client.go 665 Bytes
Newer Older
1 2 3 4
package daemon

import (
	"encoding/json"
5
	"io"
6
	"net"
7
	"os"
8

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10 11
)

Siraj Ravel's avatar
Siraj Ravel committed
12
//SendCommand connects to the address on the network with a timeout and encodes the connection into JSON
Siraj Ravel's avatar
Siraj Ravel committed
13
func SendCommand(command *Command, server string) error {
Siraj Ravel's avatar
Siraj Ravel committed
14

15 16 17 18 19 20 21 22 23
	maddr, err := ma.NewMultiaddr(server)
	if err != nil {
		return err
	}

	network, host, err := maddr.DialArgs()
	if err != nil {
		return err
	}
Siraj Ravel's avatar
Siraj Ravel committed
24

25
	conn, err := net.Dial(network, host)
26
	if err != nil {
27
		return err
28 29
	}

Siraj Ravel's avatar
Siraj Ravel committed
30 31
	enc := json.NewEncoder(conn)
	err = enc.Encode(command)
32
	if err != nil {
33
		return err
34 35
	}

Siraj Ravel's avatar
Siraj Ravel committed
36
	io.Copy(os.Stdout, conn)
37

38
	return nil
39
}