mirror of
https://github.com/offen/docker-volume-backup.git
synced 2026-04-21 08:02:41 +02:00
Add "print-config" subcommand (#698)
* Add "show-config" subcommand * Add integration tests * Handle wrong additional args * Support "BackupFilenameExpand" option * Rename to print-config * Remove config nil check as it will never be nil * Rework print config to work with config.resolve() * Use defer unset() * Add warning about feature not being stable yet
This commit is contained in:
@@ -11,8 +11,18 @@ func main() {
|
||||
foreground := flag.Bool("foreground", false, "run the tool in the foreground")
|
||||
profile := flag.String("profile", "", "collect runtime metrics and log them periodically on the given cron expression")
|
||||
flag.Parse()
|
||||
|
||||
additionalArgs := flag.Args()
|
||||
c := newCommand()
|
||||
|
||||
if len(additionalArgs) > 0 {
|
||||
switch additionalArgs[0] {
|
||||
case "print-config":
|
||||
c.must(runPrintConfig())
|
||||
return
|
||||
default:
|
||||
panic("unknown command: " + additionalArgs[0])
|
||||
}
|
||||
}
|
||||
if *foreground {
|
||||
opts := foregroundOpts{
|
||||
profileCronExpression: *profile,
|
||||
|
||||
47
cmd/backup/print_config.go
Normal file
47
cmd/backup/print_config.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2025 - offen.software <hioffen@posteo.de>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/offen/docker-volume-backup/internal/errwrap"
|
||||
)
|
||||
|
||||
func runPrintConfig() error {
|
||||
configurations, err := sourceConfiguration(configStrategyConfd)
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, "error sourcing configuration")
|
||||
}
|
||||
|
||||
formatter := regexp.MustCompile(`\s([A-Z])`)
|
||||
for _, config := range configurations {
|
||||
if err := func() (err error) {
|
||||
unset, warnings, err := config.resolve()
|
||||
if err != nil {
|
||||
return errwrap.Wrap(err, "error resolving configuration")
|
||||
}
|
||||
defer func() {
|
||||
if derr := unset(); derr != nil {
|
||||
err = errors.Join(err, errwrap.Wrap(derr, "error unsetting environment variables"))
|
||||
}
|
||||
}()
|
||||
|
||||
fmt.Printf("source=%s\n", config.source)
|
||||
for _, warning := range warnings {
|
||||
fmt.Printf("warning:%s\n", warning)
|
||||
}
|
||||
// insert line breaks before each field name, assuming field names start with uppercase letters
|
||||
formatted := formatter.ReplaceAllString(fmt.Sprintf("%+v", *config), "\n$1")
|
||||
fmt.Printf("%s\n", formatted)
|
||||
return nil
|
||||
}(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user