15960d4269
* WIP creating admin node service - WIP changing the process pkg to accept multiple services - WIP looping over services passed to process - add netstate/service.go file and abstract it for service processing * implement goroutine to launch each process * goroutines working with multiple services * code review fixes * more code updates for review * Add pkg lock and mod files back in * code review updates * update process.Main with better concurrent error handling * Update error handling and pass ctx to StartService * Update error handling with channel implementation * Merge in upstream changes - Simplify error handling channels * updates * Updates per reviewable * fix test * Setup test exec * Scaffold test setup * process main test working * update admin process test * Test multiple processes done * Add error classes for testing, test main logger error * Updates to tests * Update how process.Main() handles configs * Complete merge * Update Gopkg and add Copyright * Fix cyclical import issue - Added .coverprofile to gitignore - Update admin main.go function call * remove unnecessary line * Updates * DRY up cmd/netstate package * update service function calls * updates * Trying no-ops in examples * rename netstate to pointerdb * trying to fix merge * dep ensure and run tests * remove flag.Parse * Update deps * Skip offending test in pkg/process, to be fixed later
148 lines
3.1 KiB
Go
148 lines
3.1 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"sort"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/urfave/cli"
|
|
"github.com/zeebo/errs"
|
|
|
|
"storj.io/storj/pkg/piecestore"
|
|
"storj.io/storj/pkg/process"
|
|
)
|
|
|
|
var argError = errs.Class("argError")
|
|
|
|
func run(ctx context.Context, _ *cobra.Command, _ []string) error {
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "Piece Store CLI"
|
|
app.Usage = "Store data in hash folder structure"
|
|
app.Version = "1.0.0"
|
|
|
|
app.Flags = []cli.Flag{}
|
|
|
|
app.Commands = []cli.Command{
|
|
{
|
|
Name: "store",
|
|
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")
|
|
}
|
|
|
|
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)
|
|
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Close the file when we are done
|
|
defer file.Close()
|
|
|
|
fileInfo, err := os.Stat(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if fileInfo.IsDir() {
|
|
return argError.New(fmt.Sprintf("Path (%s) is a directory, not a file", path))
|
|
}
|
|
|
|
dataFileChunk, err := pstore.StoreWriter(id, outputDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Close when finished
|
|
defer dataFileChunk.Close()
|
|
|
|
_, err = io.Copy(dataFileChunk, file)
|
|
|
|
return err
|
|
},
|
|
},
|
|
{
|
|
Name: "retrieve",
|
|
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))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if fileInfo.IsDir() != true {
|
|
return argError.New(fmt.Sprintf("Path (%s) is a file, not a directory", c.Args().Get(1)))
|
|
}
|
|
|
|
dataFileChunk, err := pstore.RetrieveReader(context.Background(),
|
|
c.Args().Get(0), 0, -1, c.Args().Get(1))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Close when finished
|
|
defer dataFileChunk.Close()
|
|
|
|
_, err = io.Copy(os.Stdout, dataFileChunk)
|
|
return err
|
|
},
|
|
},
|
|
{
|
|
Name: "delete",
|
|
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")
|
|
}
|
|
err := pstore.Delete(c.Args().Get(0), c.Args().Get(1))
|
|
|
|
return err
|
|
},
|
|
},
|
|
}
|
|
|
|
sort.Sort(cli.FlagsByName(app.Flags))
|
|
sort.Sort(cli.CommandsByName(app.Commands))
|
|
|
|
return app.Run(append([]string{os.Args[0]}, flag.Args()...))
|
|
}
|
|
|
|
func main() { process.Must(process.Main(func() error { return nil }, process.ServiceFunc(run))) }
|