Debora download command

This commit is contained in:
Jae Kwon
2015-04-20 14:47:59 -07:00
parent f7639bf105
commit 4948fe7725
3 changed files with 62 additions and 1 deletions

View File

@ -2,11 +2,16 @@ package main
import (
"fmt"
"io"
"net/url"
"os"
acm "github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
btypes "github.com/tendermint/tendermint/cmd/barak/types"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/rpc"
"net/http"
)
// These are convenience functions for a single developer.
@ -44,6 +49,36 @@ func ListProcesses(privKey acm.PrivKey, remote string, command btypes.CommandLis
return response, err
}
func DownloadFile(privKey acm.PrivKey, remote string, command btypes.CommandServeFile, outPath string) (n int64, err error) {
// Create authCommandJSONBytes
nonce, err := GetNonce(remote)
if err != nil {
return 0, err
}
commandBytes, signature := SignCommand(privKey, nonce+1, command)
authCommand := btypes.AuthCommand{
CommandJSONStr: string(commandBytes),
Signatures: []acm.Signature{signature},
}
authCommandJSONBytes := binary.JSONBytes(authCommand)
// Make request and write to outPath.
httpResponse, err := http.PostForm(remote+"/download", url.Values{"auth_command": {string(authCommandJSONBytes)}})
if err != nil {
return 0, err
}
defer httpResponse.Body.Close()
outFile, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return 0, err
}
defer outFile.Close()
n, err = io.Copy(outFile, httpResponse.Body)
if err != nil {
return 0, err
}
return n, nil
}
//-----------------------------------------------------------------------------
// Utility method to get nonce from the remote.