Add 'databases' command.
This commit is contained in:
76
cmd/litestream/databases.go
Normal file
76
cmd/litestream/databases.go
Normal 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,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -14,10 +14,6 @@ import (
|
|||||||
|
|
||||||
type GenerationsCommand struct{}
|
type GenerationsCommand struct{}
|
||||||
|
|
||||||
func NewGenerationsCommand() *GenerationsCommand {
|
|
||||||
return &GenerationsCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GenerationsCommand) Run(ctx context.Context, args []string) (err error) {
|
func (c *GenerationsCommand) Run(ctx context.Context, args []string) (err error) {
|
||||||
var configPath string
|
var configPath string
|
||||||
fs := flag.NewFlagSet("litestream-generations", flag.ContinueOnError)
|
fs := flag.NewFlagSet("litestream-generations", flag.ContinueOnError)
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ func (m *Main) Run(ctx context.Context, args []string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch cmd {
|
switch cmd {
|
||||||
|
case "databases":
|
||||||
|
return (&DatabasesCommand{}).Run(ctx, args)
|
||||||
case "generations":
|
case "generations":
|
||||||
return (&GenerationsCommand{}).Run(ctx, args)
|
return (&GenerationsCommand{}).Run(ctx, args)
|
||||||
case "replicate":
|
case "replicate":
|
||||||
|
|||||||
@@ -19,10 +19,6 @@ type ReplicateCommand struct {
|
|||||||
DBs []*litestream.DB
|
DBs []*litestream.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReplicateCommand() *ReplicateCommand {
|
|
||||||
return &ReplicateCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run loads all databases specified in the configuration.
|
// Run loads all databases specified in the configuration.
|
||||||
func (c *ReplicateCommand) Run(ctx context.Context, args []string) (err error) {
|
func (c *ReplicateCommand) Run(ctx context.Context, args []string) (err error) {
|
||||||
fs := flag.NewFlagSet("litestream-replicate", flag.ContinueOnError)
|
fs := flag.NewFlagSet("litestream-replicate", flag.ContinueOnError)
|
||||||
|
|||||||
@@ -17,10 +17,6 @@ type RestoreCommand struct {
|
|||||||
DBPath string
|
DBPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRestoreCommand() *RestoreCommand {
|
|
||||||
return &RestoreCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
|
func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
|
||||||
var configPath string
|
var configPath string
|
||||||
var opt litestream.RestoreOptions
|
var opt litestream.RestoreOptions
|
||||||
|
|||||||
@@ -15,10 +15,6 @@ import (
|
|||||||
|
|
||||||
type SnapshotsCommand struct{}
|
type SnapshotsCommand struct{}
|
||||||
|
|
||||||
func NewSnapshotsCommand() *SnapshotsCommand {
|
|
||||||
return &SnapshotsCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *SnapshotsCommand) Run(ctx context.Context, args []string) (err error) {
|
func (c *SnapshotsCommand) Run(ctx context.Context, args []string) (err error) {
|
||||||
var configPath string
|
var configPath string
|
||||||
fs := flag.NewFlagSet("litestream-snapshots", flag.ContinueOnError)
|
fs := flag.NewFlagSet("litestream-snapshots", flag.ContinueOnError)
|
||||||
|
|||||||
@@ -15,10 +15,6 @@ import (
|
|||||||
|
|
||||||
type WALCommand struct{}
|
type WALCommand struct{}
|
||||||
|
|
||||||
func NewWALCommand() *WALCommand {
|
|
||||||
return &WALCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *WALCommand) Run(ctx context.Context, args []string) (err error) {
|
func (c *WALCommand) Run(ctx context.Context, args []string) (err error) {
|
||||||
var configPath string
|
var configPath string
|
||||||
fs := flag.NewFlagSet("litestream-wal", flag.ContinueOnError)
|
fs := flag.NewFlagSet("litestream-wal", flag.ContinueOnError)
|
||||||
|
|||||||
Reference in New Issue
Block a user