cmd/swarm: Make encrypted upload a flag instead of a command

This commit is contained in:
Balint Gabor 2018-04-12 14:44:15 +02:00
parent f0dc791adf
commit a0ae2a37dd
3 changed files with 20 additions and 24 deletions

View file

@ -141,6 +141,10 @@ var (
Name: "mime", Name: "mime",
Usage: "force mime type", Usage: "force mime type",
} }
SwarmEncryptedFlag = cli.BoolFlag{
Name: "encrypted",
Usage: "use encrypted upload",
}
SwarmPssEnabledFlag = cli.BoolFlag{ SwarmPssEnabledFlag = cli.BoolFlag{
Name: "pss", Name: "pss",
Usage: "Enable pss (message passing over swarm)", Usage: "Enable pss (message passing over swarm)",
@ -218,19 +222,11 @@ The output of this command is supposed to be machine-readable.
`, `,
}, },
{ {
Action: nonEncryptedUpload, Action: upload,
Name: "up", Name: "up",
Usage: "upload a file or directory to swarm using the HTTP API", Usage: "upload a file or directory to swarm using the HTTP API",
ArgsUsage: " <file>", ArgsUsage: " <file>",
Description: ` Flags: []cli.Flag{SwarmEncryptedFlag},
"upload a file or directory to swarm using the HTTP API and prints the root hash",
`,
},
{
Action: encryptedUpload,
Name: "encrypted-up",
Usage: "Upload a file or directory with encryption to swarm using the HTTP API. NOTE: Currently the reference for the uploaded content is non-deterministic, so you will receive different references if you upload it twice.",
ArgsUsage: " <file>",
Description: ` Description: `
"upload a file or directory to swarm using the HTTP API and prints the root hash", "upload a file or directory to swarm using the HTTP API and prints the root hash",
`, `,

View file

@ -35,15 +35,7 @@ import (
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
func encryptedUpload(ctx *cli.Context) { func upload(ctx *cli.Context) {
upload(ctx, true)
}
func nonEncryptedUpload(ctx *cli.Context) {
upload(ctx, false)
}
func upload(ctx *cli.Context, toEncrypt bool) {
args := ctx.Args() args := ctx.Args()
var ( var (
@ -54,6 +46,7 @@ func upload(ctx *cli.Context, toEncrypt bool) {
fromStdin = ctx.GlobalBool(SwarmUpFromStdinFlag.Name) fromStdin = ctx.GlobalBool(SwarmUpFromStdinFlag.Name)
mimeType = ctx.GlobalString(SwarmUploadMimeType.Name) mimeType = ctx.GlobalString(SwarmUploadMimeType.Name)
client = swarm.NewClient(bzzapi) client = swarm.NewClient(bzzapi)
toEncrypt = ctx.Bool(SwarmEncryptedFlag.Name)
file string file string
) )

View file

@ -59,15 +59,22 @@ func testCLISwarmUp(toEncrypt bool, t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
cmd := "up"
hashRegexp := `[a-f\d]{64}` hashRegexp := `[a-f\d]{64}`
flags := []string{
"--bzzapi", cluster.Nodes[0].URL,
"up",
tmp.Name()}
if toEncrypt { if toEncrypt {
cmd = "encrypted-up"
hashRegexp = `[a-f\d]{128}` hashRegexp = `[a-f\d]{128}`
flags = []string{
"--bzzapi", cluster.Nodes[0].URL,
"up",
"--encrypted",
tmp.Name()}
} }
// upload the file with 'swarm up' or 'swarm encrypted-up' and expect a hash // upload the file with 'swarm up' and expect a hash
log.Info(fmt.Sprintf("uploading file with '%s'", cmd)) log.Info(fmt.Sprintf("uploading file with 'swarm up'"))
up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, cmd, tmp.Name()) up := runSwarm(t, flags...)
_, matches := up.ExpectRegexp(hashRegexp) _, matches := up.ExpectRegexp(hashRegexp)
up.ExpectExit() up.ExpectExit()
hash := matches[0] hash := matches[0]