Pull request 1897: nextapi-write-conf

Squashed commit of the following:

commit 72f25ffe73d6b8216b01e590fba66fb5f6944113
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Jun 28 21:29:04 2023 +0300

    next: add conf writing, validation
This commit is contained in:
Ainar Garipov
2023-06-29 14:34:06 +03:00
parent 2069eddf98
commit d4a4bda645
10 changed files with 232 additions and 37 deletions

View File

@@ -19,8 +19,6 @@ import (
func Main(frontend fs.FS) {
start := time.Now()
// Initial Configuration
cmdName := os.Args[0]
opts, err := parseOptions(cmdName, os.Args[1:])
exitCode, needExit := processOptions(opts, cmdName, err)
@@ -39,9 +37,7 @@ func Main(frontend fs.FS) {
check(err)
}
// Web Service
confMgr, err := configmgr.New(opts.confFile, frontend, start)
confMgr, err := newConfigMgr(opts.confFile, frontend, start)
check(err)
web := confMgr.Web()
@@ -73,6 +69,15 @@ func ctxWithDefaultTimeout() (ctx context.Context, cancel context.CancelFunc) {
return context.WithTimeout(context.Background(), defaultTimeout)
}
// newConfigMgr returns a new configuration manager using defaultTimeout as the
// context timeout.
func newConfigMgr(confFile string, frontend fs.FS, start time.Time) (m *configmgr.Manager, err error) {
ctx, cancel := ctxWithDefaultTimeout()
defer cancel()
return configmgr.New(ctx, confFile, frontend, start)
}
// check is a simple error-checking helper. It must only be used within Main.
func check(err error) {
if err != nil {

View File

@@ -8,6 +8,7 @@ import (
"os"
"strings"
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
"github.com/AdguardTeam/AdGuardHome/internal/version"
"golang.org/x/exp/slices"
)
@@ -55,9 +56,8 @@ type options struct {
webAddrs []netip.AddrPort
// checkConfig, if true, instructs AdGuard Home to check the configuration
// file and exit with a corresponding exit code.
//
// TODO(a.garipov): Use.
// file, optionally print an error message to stdout, and exit with a
// corresponding exit code.
checkConfig bool
// disableUpdate, if true, prevents AdGuard Home from automatically checking
@@ -183,7 +183,7 @@ var commandLineOptions = []*commandLineOption{
checkConfigIdx: {
defaultValue: false,
description: "Check configuration and quit.",
description: "Check configuration, print errors to stdout, and quit.",
long: "check-config",
short: "",
valueType: "",
@@ -399,5 +399,16 @@ func processOptions(
return 0, true
}
if opts.checkConfig {
err := configmgr.Validate(opts.confFile)
if err != nil {
_, _ = io.WriteString(os.Stdout, err.Error()+"\n")
return 1, true
}
return 0, true
}
return 0, false
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
"github.com/AdguardTeam/golibs/log"
)
@@ -63,7 +62,7 @@ func (h *signalHandler) reconfigure() {
// reconfigured without the full shutdown, and the error handling is
// currently not the best.
confMgr, err := configmgr.New(h.confFile, h.frontend, h.start)
confMgr, err := newConfigMgr(h.confFile, h.frontend, h.start)
check(err)
web := confMgr.Web()