Add 'databases' command.

This commit is contained in:
Ben Johnson
2020-12-30 16:10:48 -07:00
parent 11d7d22383
commit cff778464e
7 changed files with 78 additions and 20 deletions

View File

@@ -0,0 +1,76 @@
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"strings"
"text/tabwriter"
)
type DatabasesCommand struct{}
func (c *DatabasesCommand) Run(ctx context.Context, args []string) (err error) {
var configPath string
fs := flag.NewFlagSet("litestream-databases", flag.ContinueOnError)
registerConfigFlag(fs, &configPath)
fs.Usage = c.Usage
if err := fs.Parse(args); err != nil {
return err
} else if fs.NArg() != 0 {
return fmt.Errorf("too many argument")
}
// Load configuration.
if configPath == "" {
return errors.New("-config required")
}
config, err := ReadConfigFile(configPath)
if err != nil {
return err
}
// List all databases.
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "path\treplicas")
for _, dbConfig := range config.DBs {
db, err := newDBFromConfig(dbConfig)
if err != nil {
return err
}
var replicaNames []string
for _, r := range db.Replicas {
replicaNames = append(replicaNames, r.Name())
}
fmt.Fprintf(w, "%s\t%s\n",
db.Path(),
strings.Join(replicaNames, ","),
)
}
w.Flush()
return nil
}
func (c *DatabasesCommand) Usage() {
fmt.Printf(`
The databases command lists all databases in the configuration file.
Usage:
litestream databases [arguments]
Arguments:
-config PATH
Specifies the configuration file.
Defaults to %s
`[1:],
DefaultConfigPath,
)
}

View File

@@ -14,10 +14,6 @@ import (
type GenerationsCommand struct{}
func NewGenerationsCommand() *GenerationsCommand {
return &GenerationsCommand{}
}
func (c *GenerationsCommand) Run(ctx context.Context, args []string) (err error) {
var configPath string
fs := flag.NewFlagSet("litestream-generations", flag.ContinueOnError)

View File

@@ -48,6 +48,8 @@ func (m *Main) Run(ctx context.Context, args []string) (err error) {
}
switch cmd {
case "databases":
return (&DatabasesCommand{}).Run(ctx, args)
case "generations":
return (&GenerationsCommand{}).Run(ctx, args)
case "replicate":

View File

@@ -19,10 +19,6 @@ type ReplicateCommand struct {
DBs []*litestream.DB
}
func NewReplicateCommand() *ReplicateCommand {
return &ReplicateCommand{}
}
// Run loads all databases specified in the configuration.
func (c *ReplicateCommand) Run(ctx context.Context, args []string) (err error) {
fs := flag.NewFlagSet("litestream-replicate", flag.ContinueOnError)

View File

@@ -17,10 +17,6 @@ type RestoreCommand struct {
DBPath string
}
func NewRestoreCommand() *RestoreCommand {
return &RestoreCommand{}
}
func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
var configPath string
var opt litestream.RestoreOptions

View File

@@ -15,10 +15,6 @@ import (
type SnapshotsCommand struct{}
func NewSnapshotsCommand() *SnapshotsCommand {
return &SnapshotsCommand{}
}
func (c *SnapshotsCommand) Run(ctx context.Context, args []string) (err error) {
var configPath string
fs := flag.NewFlagSet("litestream-snapshots", flag.ContinueOnError)

View File

@@ -15,10 +15,6 @@ import (
type WALCommand struct{}
func NewWALCommand() *WALCommand {
return &WALCommand{}
}
func (c *WALCommand) Run(ctx context.Context, args []string) (err error) {
var configPath string
fs := flag.NewFlagSet("litestream-wal", flag.ContinueOnError)