Allow replica URLs for generations command

This commit is contained in:
Ben Johnson
2021-01-27 07:48:56 -07:00
parent 39a6fabb9f
commit 7ca2e193b9
2 changed files with 49 additions and 36 deletions

View File

@@ -7,9 +7,10 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"text/tabwriter" "text/tabwriter"
"time" "time"
"github.com/benbjohnson/litestream"
) )
// GenerationsCommand represents a command to list all generations for a database. // GenerationsCommand represents a command to list all generations for a database.
@@ -25,50 +26,60 @@ func (c *GenerationsCommand) Run(ctx context.Context, args []string) (err error)
if err := fs.Parse(args); err != nil { if err := fs.Parse(args); err != nil {
return err return err
} else if fs.NArg() == 0 || fs.Arg(0) == "" { } else if fs.NArg() == 0 || fs.Arg(0) == "" {
return fmt.Errorf("database path required") return fmt.Errorf("database path or replica URL required")
} else if fs.NArg() > 1 { } else if fs.NArg() > 1 {
return fmt.Errorf("too many arguments") return fmt.Errorf("too many arguments")
} }
// Load configuration. var db *litestream.DB
if configPath == "" { var r litestream.Replica
return errors.New("-config required") updatedAt := time.Now()
if isURL(fs.Arg(0)) {
if r, err = NewReplicaFromURL(fs.Arg(0)); err != nil {
return err
} }
} else if configPath != "" {
// Load configuration.
config, err := ReadConfigFile(configPath) config, err := ReadConfigFile(configPath)
if err != nil { if err != nil {
return err return err
} }
// Determine absolute path for database. // Lookup database from configuration file by path.
dbPath, err := filepath.Abs(fs.Arg(0)) if path, err := expand(fs.Arg(0)); err != nil {
if err != nil { return err
} else if dbc := config.DBConfig(path); dbc == nil {
return fmt.Errorf("database not found in config: %s", path)
} else if db, err = newDBFromConfig(&config, dbc); err != nil {
return err return err
} }
// Instantiate DB from from configuration. // Filter by replica, if specified.
dbConfig := config.DBConfig(dbPath) if *replicaName != "" {
if dbConfig == nil { if r = db.Replica(*replicaName); r == nil {
return fmt.Errorf("database not found in config: %s", dbPath) return fmt.Errorf("replica %q not found for database %q", *replicaName, db.Path())
} }
db, err := newDBFromConfig(&config, dbConfig)
if err != nil {
return err
} }
// Determine last time database or WAL was updated. // Determine last time database or WAL was updated.
updatedAt, err := db.UpdatedAt() if updatedAt, err = db.UpdatedAt(); err != nil {
if err != nil {
return err return err
} }
} else {
return errors.New("config path or replica URL required")
}
var replicas []litestream.Replica
if r != nil {
replicas = []litestream.Replica{r}
} else {
replicas = db.Replicas
}
// List each generation. // List each generation.
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "name\tgeneration\tlag\tstart\tend") fmt.Fprintln(w, "name\tgeneration\tlag\tstart\tend")
for _, r := range db.Replicas { for _, r := range replicas {
if *replicaName != "" && r.Name() != *replicaName {
continue
}
generations, err := r.Generations(ctx) generations, err := r.Generations(ctx)
if err != nil { if err != nil {
log.Printf("%s: cannot list generations: %s", r.Name(), err) log.Printf("%s: cannot list generations: %s", r.Name(), err)
@@ -114,7 +125,8 @@ Usage:
Arguments: Arguments:
-config PATH -config PATH
Specifies the configuration file. Defaults to %s Specifies the configuration file.
Defaults to %s
-replica NAME -replica NAME
Optional, filters by replica. Optional, filters by replica.

View File

@@ -156,7 +156,8 @@ Usage:
Arguments: Arguments:
-config PATH -config PATH
Specifies the configuration file. Defaults to %s Specifies the configuration file.
Defaults to %s
-v -v
Enable verbose logging output. Enable verbose logging output.