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.

View File

@ -79,6 +79,11 @@ func main() {
Usage: "list processes",
Action: cliListProcesses,
},
cli.Command{
Name: "download",
Usage: "download file <remote-path> <local-path-prefix>",
Action: cliDownloadFile,
},
}
app.Run(os.Args)
}
@ -189,3 +194,24 @@ func cliListProcesses(c *cli.Context) {
}
}
}
func cliDownloadFile(c *cli.Context) {
args := c.Args()
if len(args) != 2 {
Exit("Must specify <remote-path> <local-path-prefix>")
}
remotePath := args[0]
localPathPrefix := args[1]
command := btypes.CommandServeFile{
Path: remotePath,
}
for i, remote := range Config.Remotes {
localPath := Fmt("%v_%v", localPathPrefix, i)
n, err := DownloadFile(Config.PrivKey, remote, command, localPath)
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v success. Wrote %v bytes to %v\n", remote, n, localPath)
}
}
}

View File

@ -31,7 +31,7 @@ const (
// execPath: command name
// args: args to command. (should not include name)
func Create(mode int, label string, execPath string, args []string, input string, outPath string) (*Process, error) {
outFile, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
outFile, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return nil, err
}