Files
litestream/cmd/litestream/databases.go
Ben Johnson 325482a97c Prevent user from specifying replica URL & config flag
Previously, if a replica URL was specified then the `-config` flag
was silently ignored. This commit changes this behavior so that
specifying both the URL & config flag will now return an error.
2021-02-28 08:09:24 -07:00

79 lines
1.5 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"text/tabwriter"
)
// DatabasesCommand is a command for listing managed databases.
type DatabasesCommand struct{}
// Run executes the command.
func (c *DatabasesCommand) Run(ctx context.Context, args []string) (err error) {
fs := flag.NewFlagSet("litestream-databases", flag.ContinueOnError)
configPath := registerConfigFlag(fs)
fs.Usage = c.Usage
if err := fs.Parse(args); err != nil {
return err
} else if fs.NArg() != 0 {
return fmt.Errorf("too many arguments")
}
// Load configuration.
if *configPath == "" {
*configPath = DefaultConfigPath()
}
config, err := ReadConfigFile(*configPath)
if err != nil {
return err
}
// List all databases.
w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
defer w.Flush()
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, ","),
)
}
return nil
}
// Usage prints the help screen to STDOUT.
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(),
)
}