cleanup arg processing

This commit is contained in:
Todd Gruben 2021-05-24 08:48:32 -05:00
parent b5613a6c33
commit 69fda13ff4

View file

@ -29,20 +29,20 @@ import (
"github.com/pilosa/pilosa/v2"
"github.com/pilosa/pilosa/v2/server"
"github.com/pilosa/pilosa/v2/topology"
"github.com/pilosa/pilosa/v2/vprint"
)
// RestoreCommand represents a command for restoring a backup to
type RestoreCommand struct {
TLS server.TLSConfig
Host string
// Filepath to the backup file.
Path string
Host string
// Reusable client.
client pilosa.InternalClient
// Standard input/output
*pilosa.CmdIO
TLS server.TLSConfig
}
// NewRestoreCommand returns a new instance of RestoreCommand.
@ -54,23 +54,31 @@ func NewRestoreCommand(stdin io.Reader, stdout, stderr io.Writer) *RestoreComman
// Run executes the restore.
func (cmd *RestoreCommand) Run(ctx context.Context) error {
logger := cmd.Logger()
// Validate arguments.
if cmd.Path == "" {
return fmt.Errorf("-s flag required")
}
useStdin := cmd.Path == "-"
var f *os.File
// read from Stdin if path specified as -
if useStdin {
f = os.Stdin
} else {
f, err := os.Open(cmd.Path)
if err != nil {
return (err)
}
defer f.Close()
}
// Create a client to the server.
client, err := commandClient(cmd)
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
cmd.client = client
var f *os.File
// read from Stdin if path specified as -
if cmd.Path == "-" {
f = os.Stdin
} else {
f, err = os.Open(cmd.Path)
if err != nil {
return (err)
}
}
defer f.Close()
var tarReader *tar.Reader
if strings.HasSuffix(cmd.Path, "gz") {
gzf, err := gzip.NewReader(f)
@ -106,14 +114,14 @@ func (cmd *RestoreCommand) Run(ctx context.Context) error {
if len(record) == 1 {
switch record[0] {
case "schema":
vprint.VV("Load Schema")
logger.Printf("Load Schema")
url := primary.URI.Path("/schema")
_, err = c.Post(url, "application/json", tarReader)
if err != nil {
return err
}
case "idalloc":
vprint.VV("Load ids")
logger.Printf("Load ids")
url := primary.URI.Path("/internal/idalloc/restore")
_, err = c.Post(url, "application/octet-stream", tarReader)
if err != nil {
@ -132,7 +140,7 @@ func (cmd *RestoreCommand) Run(ctx context.Context) error {
if err != nil {
return err
}
vprint.VV("shard %v %v", shard, indexName)
logger.Printf("shard %v %v", shard, indexName)
url := primary.URI.Path(fmt.Sprintf("/internal/restore/%v/%v", indexName, shard))
//TODO (twg) cluster aware client
_, err = c.Post(url, "application/octet-stream", tarReader)
@ -141,7 +149,7 @@ func (cmd *RestoreCommand) Run(ctx context.Context) error {
}
case "translate":
partitionID, err := strconv.Atoi(record[3])
vprint.VV("column keys %v (%v)", indexName, partitionID)
logger.Printf("column keys %v (%v)", indexName, partitionID)
if err != nil {
return err
}
@ -152,20 +160,19 @@ func (cmd *RestoreCommand) Run(ctx context.Context) error {
}
case "attributes":
//skip
//vprint.VV("column attributes %v", indexName)
case "fields":
fieldName := record[3]
switch action := record[4]; action {
case "translate":
vprint.VV("field keys %v %v", indexName, fieldName)
logger.Printf("field keys %v %v", indexName, fieldName)
err := cmd.client.ImportFieldKeys(ctx, &primary.URI, indexName, fieldName, false, tarReader)
if err != nil {
return err
}
case "attributes":
// vprint.VV("field attributes %v %v", indexName, fieldName)
//skip
default:
panic("unknown:" + action)
return fmt.Errorf("unknown restore action: %v", action)
}
}