Make the next shirt, better (#362)

This commit is contained in:
Matt Robinson 2018-09-18 11:26:59 -04:00 committed by JT Olio
parent ebb839da7d
commit e1f44ec782
3 changed files with 116 additions and 9 deletions

46
cmd/uplink/cmd/cat.go Normal file
View File

@ -0,0 +1,46 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/utils"
)
func init() {
addCmd(&cobra.Command{
Use: "cat",
Short: "Copies a Storj object to standard out",
RunE: catMain,
})
}
// catMain is the function executed when catCmd is called
func catMain(cmd *cobra.Command, args []string) (err error) {
if len(args) == 0 {
return fmt.Errorf("No object specified for copy")
}
ctx := process.Ctx(cmd)
u0, err := utils.ParseURL(args[0])
if err != nil {
return err
}
bs, err := cfg.BucketStore(ctx)
if err != nil {
return err
}
if u0.Host == "" {
return fmt.Errorf("No bucket specified. Please use format sj://bucket/")
}
return download(ctx, bs, u0, "-")
}

View File

@ -42,6 +42,7 @@ func cleanAbsPath(p string) string {
// upload uploads args[0] from local machine to s3 compatible object args[1]
func upload(ctx context.Context, bs buckets.Store, srcFile string, destObj *url.URL) error {
var err error
if destObj.Scheme == "" {
return fmt.Errorf("Invalid destination")
}
@ -52,11 +53,16 @@ func upload(ctx context.Context, bs buckets.Store, srcFile string, destObj *url.
destObj.Path = path.Join(destObj.Path, path.Base(srcFile))
}
f, err := os.Open(srcFile)
if err != nil {
return err
var f *os.File
if srcFile == "-" {
f = os.Stdin
} else {
f, err = os.Open(srcFile)
if err != nil {
return err
}
defer utils.LogClose(f)
}
defer utils.LogClose(f)
o, err := bs.GetObjectStore(ctx, destObj.Host)
if err != nil {
@ -78,6 +84,7 @@ func upload(ctx context.Context, bs buckets.Store, srcFile string, destObj *url.
// download downloads s3 compatible object args[0] to args[1] on local machine
func download(ctx context.Context, bs buckets.Store, srcObj *url.URL, destFile string) error {
var err error
if srcObj.Scheme == "" {
return fmt.Errorf("Invalid source")
}
@ -91,11 +98,16 @@ func download(ctx context.Context, bs buckets.Store, srcObj *url.URL, destFile s
destFile = filepath.Join(destFile, filepath.Base(srcObj.Path))
}
f, err := os.Create(destFile)
if err != nil {
return err
var f *os.File
if destFile == "-" {
f = os.Stdout
} else {
f, err = os.Open(destFile)
if err != nil {
return err
}
defer utils.LogClose(f)
}
defer utils.LogClose(f)
rr, _, err := o.Get(ctx, paths.New(srcObj.Path))
if err != nil {
@ -113,7 +125,9 @@ func download(ctx context.Context, bs buckets.Store, srcObj *url.URL, destFile s
return err
}
fmt.Printf("Downloaded %s to %s\n", srcObj, destFile)
if destFile != "-" {
fmt.Printf("Downloaded %s to %s\n", srcObj, destFile)
}
return nil
}

47
cmd/uplink/cmd/put.go Normal file
View File

@ -0,0 +1,47 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/utils"
)
func init() {
addCmd(&cobra.Command{
Use: "put",
Short: "Copies data from standard in to a Storj object",
RunE: putMain,
})
}
// putMain is the function executed when putCmd is called
func putMain(cmd *cobra.Command, args []string) (err error) {
if len(args) == 0 {
return fmt.Errorf("No object specified for copy")
}
ctx := process.Ctx(cmd)
u0, err := utils.ParseURL(args[0])
if err != nil {
return err
}
bs, err := cfg.BucketStore(ctx)
if err != nil {
return err
}
// if uploading
if u0.Host == "" {
return fmt.Errorf("No bucket specified. Please use format sj://bucket/")
}
return upload(ctx, bs, "-", u0)
}