examples: piecestore-cli use cobra directly (#409)
This commit is contained in:
parent
dac03acbc4
commit
ec0850fa9f
@ -8,51 +8,40 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/urfave/cli"
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
pstore "storj.io/storj/pkg/piecestore"
|
||||
"storj.io/storj/pkg/process"
|
||||
)
|
||||
|
||||
var argError = errs.Class("argError")
|
||||
func main() {
|
||||
cobra.EnableCommandSorting = false
|
||||
root := &cobra.Command{
|
||||
Use: "piecestore-cli",
|
||||
Short: "piecestore example cli",
|
||||
}
|
||||
|
||||
func run(_ *cobra.Command, args []string) error {
|
||||
app := cli.NewApp()
|
||||
root.AddCommand(
|
||||
storeMain,
|
||||
retrieveMain,
|
||||
deleteMain,
|
||||
)
|
||||
|
||||
app.Name = "Piece Store CLI"
|
||||
app.Usage = "Store data in hash folder structure"
|
||||
app.Version = "1.0.0"
|
||||
process.Exec(root)
|
||||
}
|
||||
|
||||
app.Flags = []cli.Flag{}
|
||||
|
||||
app.Commands = []cli.Command{
|
||||
{
|
||||
Name: "store",
|
||||
var storeMain = &cobra.Command{
|
||||
Use: "store [id] [dataPath] [storeDir]",
|
||||
Aliases: []string{"s"},
|
||||
Usage: "Store data by id",
|
||||
ArgsUsage: "[id] [dataPath] [storeDir]",
|
||||
Action: func(c *cli.Context) error {
|
||||
if c.Args().Get(0) == "" {
|
||||
return argError.New("No id specified")
|
||||
}
|
||||
Short: "Store data by id",
|
||||
Args: cobra.ExactArgs(3),
|
||||
ValidArgs: []string{"id", "datapath", "storedir"},
|
||||
|
||||
id := c.Args().Get(0)
|
||||
|
||||
if c.Args().Get(1) == "" {
|
||||
return argError.New("No input file specified")
|
||||
}
|
||||
|
||||
path := c.Args().Get(1)
|
||||
|
||||
if c.Args().Get(2) == "" {
|
||||
return argError.New("No output directory specified")
|
||||
}
|
||||
|
||||
outputDir := c.Args().Get(2)
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
id := args[0]
|
||||
path := args[1]
|
||||
outputDir := args[2]
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
@ -68,7 +57,7 @@ func run(_ *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if fileInfo.IsDir() {
|
||||
return argError.New(fmt.Sprintf("Path (%s) is a directory, not a file", path))
|
||||
return fmt.Errorf("Path (%s) is a directory, not a file", path)
|
||||
}
|
||||
|
||||
dataFileChunk, err := pstore.StoreWriter(id, outputDir)
|
||||
@ -83,30 +72,28 @@ func run(_ *cobra.Command, args []string) error {
|
||||
|
||||
return err
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "retrieve",
|
||||
}
|
||||
|
||||
var retrieveMain = &cobra.Command{
|
||||
Use: "retrieve [id] [storeDir]",
|
||||
Aliases: []string{"r"},
|
||||
Usage: "Retrieve data by id and print to Stdout",
|
||||
ArgsUsage: "[id] [storeDir]",
|
||||
Action: func(c *cli.Context) error {
|
||||
if c.Args().Get(0) == "" {
|
||||
return argError.New("Missing data id")
|
||||
}
|
||||
if c.Args().Get(1) == "" {
|
||||
return argError.New("Missing file path")
|
||||
}
|
||||
fileInfo, err := os.Stat(c.Args().Get(1))
|
||||
Args: cobra.ExactArgs(2),
|
||||
Short: "Retrieve data by id and print to Stdout",
|
||||
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
id := args[0]
|
||||
path := args[1]
|
||||
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !fileInfo.IsDir() {
|
||||
return argError.New(fmt.Sprintf("Path (%s) is a file, not a directory", c.Args().Get(1)))
|
||||
return fmt.Errorf("Path (%s) is a file, not a directory", path)
|
||||
}
|
||||
|
||||
dataFileChunk, err := pstore.RetrieveReader(context.Background(),
|
||||
c.Args().Get(0), 0, -1, c.Args().Get(1))
|
||||
dataFileChunk, err := pstore.RetrieveReader(context.Background(), id, 0, -1, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -117,36 +104,19 @@ func run(_ *cobra.Command, args []string) error {
|
||||
_, err = io.Copy(os.Stdout, dataFileChunk)
|
||||
return err
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "delete",
|
||||
}
|
||||
|
||||
var deleteMain = &cobra.Command{
|
||||
Use: "delete [id] [storeDir]",
|
||||
Aliases: []string{"d"},
|
||||
Usage: "Delete data by id",
|
||||
ArgsUsage: "[id] [storeDir]",
|
||||
Action: func(c *cli.Context) error {
|
||||
if c.Args().Get(0) == "" {
|
||||
return argError.New("Missing data id")
|
||||
}
|
||||
if c.Args().Get(1) == "" {
|
||||
return argError.New("No directory specified")
|
||||
}
|
||||
return pstore.Delete(c.Args().Get(0), c.Args().Get(1))
|
||||
Args: cobra.ExactArgs(2),
|
||||
Short: "Delete data by id",
|
||||
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
id := args[0]
|
||||
directory := args[1]
|
||||
return pstore.Delete(id, directory)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
sort.Sort(cli.FlagsByName(app.Flags))
|
||||
sort.Sort(cli.CommandsByName(app.Commands))
|
||||
|
||||
return app.Run(append([]string{os.Args[0]}, args...))
|
||||
}
|
||||
|
||||
func main() {
|
||||
process.Exec(&cobra.Command{
|
||||
Use: "piecestore-cli",
|
||||
Short: "piecestore example cli",
|
||||
RunE: run,
|
||||
})
|
||||
}
|
||||
|
||||
func printError(fn func() error) {
|
||||
|
Loading…
Reference in New Issue
Block a user