Allow replica URLs for generations command
This commit is contained in:
@@ -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)) {
|
||||||
config, err := ReadConfigFile(configPath)
|
if r, err = NewReplicaFromURL(fs.Arg(0)); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
|
} else if configPath != "" {
|
||||||
|
// Load configuration.
|
||||||
|
config, err := ReadConfigFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup database from configuration file by path.
|
||||||
|
if path, err := expand(fs.Arg(0)); 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter by replica, if specified.
|
||||||
|
if *replicaName != "" {
|
||||||
|
if r = db.Replica(*replicaName); r == nil {
|
||||||
|
return fmt.Errorf("replica %q not found for database %q", *replicaName, db.Path())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine last time database or WAL was updated.
|
||||||
|
if updatedAt, err = db.UpdatedAt(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.New("config path or replica URL required")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine absolute path for database.
|
var replicas []litestream.Replica
|
||||||
dbPath, err := filepath.Abs(fs.Arg(0))
|
if r != nil {
|
||||||
if err != nil {
|
replicas = []litestream.Replica{r}
|
||||||
return err
|
} else {
|
||||||
}
|
replicas = db.Replicas
|
||||||
|
|
||||||
// Instantiate DB from from configuration.
|
|
||||||
dbConfig := config.DBConfig(dbPath)
|
|
||||||
if dbConfig == nil {
|
|
||||||
return fmt.Errorf("database not found in config: %s", dbPath)
|
|
||||||
}
|
|
||||||
db, err := newDBFromConfig(&config, dbConfig)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine last time database or WAL was updated.
|
|
||||||
updatedAt, err := db.UpdatedAt()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
Reference in New Issue
Block a user