Pull request 2111: 6545-schema-version

Updates #6545.

Squashed commit of the following:

commit b1969128a99ff21c97feb4e7805b4b8133d7122f
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Dec 15 20:04:37 2023 +0300

    home: fix import

commit 872ccea1491a8da76cc24db79247438d0ce4d256
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Dec 15 20:01:15 2023 +0300

    all: output schema version
This commit is contained in:
Ainar Garipov 2023-12-15 20:27:47 +03:00
parent 9241393ed2
commit d32832735c
92 changed files with 86 additions and 80 deletions

View File

@ -25,6 +25,9 @@ NOTE: Add new changes BELOW THIS COMMENT.
### Added ### Added
- The schema version of the configuration file to the output of running
`AdGuardHome` (or `AdGuardHome.exe) with `-v --version` command-line options
([#6545]).
- Ability to disable plain-DNS serving via UI if an encrypted protocol is - Ability to disable plain-DNS serving via UI if an encrypted protocol is
already used ([#1660]). already used ([#1660]).
@ -34,6 +37,7 @@ NOTE: Add new changes BELOW THIS COMMENT.
work on iOS ([#6352]). work on iOS ([#6352]).
[#6352]: https://github.com/AdguardTeam/AdGuardHome/issues/6352 [#6352]: https://github.com/AdguardTeam/AdGuardHome/issues/6352
[#6545]: https://github.com/AdguardTeam/AdGuardHome/issues/6545
<!-- <!--
NOTE: Add new changes ABOVE THIS COMMENT. NOTE: Add new changes ABOVE THIS COMMENT.

View File

@ -0,0 +1,5 @@
// Package configmigrate provides a way to upgrade the YAML configuration file.
package configmigrate
// LastSchemaVersion is the most recent schema version.
const LastSchemaVersion uint = 27

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"testing" "testing"

View File

@ -1,5 +1,4 @@
// Package confmigrate provides a way to upgrade the YAML configuration file. package configmigrate
package confmigrate
import ( import (
"bytes" "bytes"
@ -9,9 +8,6 @@ import (
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
// LastSchemaVersion is the most recent schema version.
const LastSchemaVersion uint = 27
// Config is a the configuration for initializing a [Migrator]. // Config is a the configuration for initializing a [Migrator].
type Config struct { type Config struct {
// WorkingDir is an absolute path to the working directory of AdGuardHome. // WorkingDir is an absolute path to the working directory of AdGuardHome.

View File

@ -1,4 +1,4 @@
package confmigrate_test package configmigrate_test
import ( import (
"io/fs" "io/fs"
@ -6,7 +6,7 @@ import (
"path" "path"
"testing" "testing"
"github.com/AdguardTeam/AdGuardHome/internal/confmigrate" "github.com/AdguardTeam/AdGuardHome/internal/configmigrate"
"github.com/AdguardTeam/golibs/testutil" "github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -200,7 +200,7 @@ func TestMigrateConfig_Migrate(t *testing.T) {
wantBody, err := fs.ReadFile(testdata, path.Join(t.Name(), outputFileName)) wantBody, err := fs.ReadFile(testdata, path.Join(t.Name(), outputFileName))
require.NoError(t, err) require.NoError(t, err)
migrator := confmigrate.New(&confmigrate.Config{ migrator := configmigrate.New(&configmigrate.Config{
WorkingDir: t.Name(), WorkingDir: t.Name(),
}) })
newBody, upgraded, err := migrator.Migrate(body, tc.targetVersion) newBody, upgraded, err := migrator.Migrate(body, tc.targetVersion)

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"os" "os"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo11 performs the following changes: // migrateTo11 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"time" "time"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo13 performs the following changes: // migrateTo13 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo14 performs the following changes: // migrateTo14 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo15 performs the following changes: // migrateTo15 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo16 performs the following changes: // migrateTo16 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo17 performs the following changes: // migrateTo17 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo18 performs the following changes: // migrateTo18 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import "github.com/AdguardTeam/golibs/log" import "github.com/AdguardTeam/golibs/log"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"os" "os"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"time" "time"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo21 performs the following changes: // migrateTo21 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo24 performs the following changes: // migrateTo24 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo25 performs the following changes: // migrateTo25 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo26 performs the following changes: // migrateTo26 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo27 performs the following changes: // migrateTo27 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo3 performs the following changes: // migrateTo3 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo4 performs the following changes: // migrateTo4 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import "fmt" import "fmt"

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo7 performs the following changes: // migrateTo7 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo8 performs the following changes: // migrateTo8 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
// migrateTo9 performs the following changes: // migrateTo9 performs the following changes:
// //

View File

@ -1,4 +1,4 @@
package confmigrate package configmigrate
import ( import (
"fmt" "fmt"

View File

@ -10,7 +10,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghalg" "github.com/AdguardTeam/AdGuardHome/internal/aghalg"
"github.com/AdguardTeam/AdGuardHome/internal/aghtls" "github.com/AdguardTeam/AdGuardHome/internal/aghtls"
"github.com/AdguardTeam/AdGuardHome/internal/confmigrate" "github.com/AdguardTeam/AdGuardHome/internal/configmigrate"
"github.com/AdguardTeam/AdGuardHome/internal/dhcpd" "github.com/AdguardTeam/AdGuardHome/internal/dhcpd"
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward" "github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/AdGuardHome/internal/filtering" "github.com/AdguardTeam/AdGuardHome/internal/filtering"
@ -149,7 +149,7 @@ type configuration struct {
sync.RWMutex `yaml:"-"` sync.RWMutex `yaml:"-"`
// SchemaVersion is the version of the configuration schema. See // SchemaVersion is the version of the configuration schema. See
// [confmigrate.LastSchemaVersion]. // [configmigrate.LastSchemaVersion].
SchemaVersion uint `yaml:"schema_version"` SchemaVersion uint `yaml:"schema_version"`
} }
@ -434,7 +434,7 @@ var config = &configuration{
MaxAge: 3, MaxAge: 3,
}, },
OSConfig: &osConfig{}, OSConfig: &osConfig{},
SchemaVersion: confmigrate.LastSchemaVersion, SchemaVersion: configmigrate.LastSchemaVersion,
Theme: ThemeAuto, Theme: ThemeAuto,
} }
@ -479,14 +479,14 @@ func parseConfig() (err error) {
return err return err
} }
migrator := confmigrate.New(&confmigrate.Config{ migrator := configmigrate.New(&configmigrate.Config{
WorkingDir: Context.workDir, WorkingDir: Context.workDir,
}) })
var upgraded bool var upgraded bool
config.fileData, upgraded, err = migrator.Migrate( config.fileData, upgraded, err = migrator.Migrate(
config.fileData, config.fileData,
confmigrate.LastSchemaVersion, configmigrate.LastSchemaVersion,
) )
if err != nil { if err != nil {
// Don't wrap the error, because it's informative enough as is. // Don't wrap the error, because it's informative enough as is.

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/AdguardTeam/AdGuardHome/internal/configmigrate"
"github.com/AdguardTeam/AdGuardHome/internal/version" "github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/stringutil" "github.com/AdguardTeam/golibs/stringutil"
@ -308,7 +309,7 @@ var cmdLineOpts = []cmdLineOpt{{
effect: func(o options, exec string) (effect, error) { effect: func(o options, exec string) (effect, error) {
return func() error { return func() error {
if o.verbose { if o.verbose {
fmt.Println(version.Verbose()) fmt.Print(version.Verbose(configmigrate.LastSchemaVersion))
} else { } else {
fmt.Println(version.Full()) fmt.Println(version.Full())
} }

View File

@ -10,6 +10,7 @@ import (
"os" "os"
"strings" "strings"
"github.com/AdguardTeam/AdGuardHome/internal/configmigrate"
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr" "github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
"github.com/AdguardTeam/AdGuardHome/internal/version" "github.com/AdguardTeam/AdGuardHome/internal/version"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
@ -382,7 +383,7 @@ func processOptions(
if opts.version { if opts.version {
if opts.verbose { if opts.verbose {
fmt.Println(version.Verbose()) fmt.Print(version.Verbose(configmigrate.LastSchemaVersion))
} else { } else {
fmt.Printf("AdGuard Home %s\n", version.Version()) fmt.Printf("AdGuard Home %s\n", version.Version())
} }

View File

@ -98,6 +98,7 @@ func fmtModule(m *debug.Module) (formatted string) {
const ( const (
vFmtAGHHdr = "AdGuard Home" vFmtAGHHdr = "AdGuard Home"
vFmtVerHdr = "Version: " vFmtVerHdr = "Version: "
vFmtSchemaVerHdr = "Schema version: "
vFmtChanHdr = "Channel: " vFmtChanHdr = "Channel: "
vFmtGoHdr = "Go version: " vFmtGoHdr = "Go version: "
vFmtTimeHdr = "Commit time: " vFmtTimeHdr = "Commit time: "
@ -113,6 +114,7 @@ const (
// //
// AdGuard Home // AdGuard Home
// Version: v0.105.3 // Version: v0.105.3
// Schema version: 27
// Channel: development // Channel: development
// Go version: go1.15.3 // Go version: go1.15.3
// Build time: 2021-03-30T16:26:08Z+0300 // Build time: 2021-03-30T16:26:08Z+0300
@ -125,34 +127,31 @@ const (
// ... // ...
// //
// TODO(e.burkov): Make it write into passed io.Writer. // TODO(e.burkov): Make it write into passed io.Writer.
func Verbose() (v string) { func Verbose(schemaVersion uint) (v string) {
b := &strings.Builder{} b := &strings.Builder{}
const nl = "\n" const nl = "\n"
stringutil.WriteToBuilder( stringutil.WriteToBuilder(b, vFmtAGHHdr, nl)
b, stringutil.WriteToBuilder(b, vFmtVerHdr, version, nl)
vFmtAGHHdr,
nl, schemaVerStr := strconv.FormatUint(uint64(schemaVersion), 10)
vFmtVerHdr, stringutil.WriteToBuilder(b, vFmtSchemaVerHdr, schemaVerStr, nl)
version,
nl, stringutil.WriteToBuilder(b, vFmtChanHdr, channel, nl)
vFmtChanHdr, stringutil.WriteToBuilder(b, vFmtGoHdr, runtime.Version(), nl)
channel,
nl,
vFmtGoHdr,
runtime.Version(),
)
writeCommitTime(b) writeCommitTime(b)
stringutil.WriteToBuilder(b, nl, vFmtGOOSHdr, nl, vFmtGOARCHHdr) stringutil.WriteToBuilder(b, vFmtGOOSHdr, nl)
stringutil.WriteToBuilder(b, vFmtGOARCHHdr, nl)
if goarm != "" { if goarm != "" {
stringutil.WriteToBuilder(b, nl, vFmtGOARMHdr, "v", goarm) stringutil.WriteToBuilder(b, vFmtGOARMHdr, "v", goarm, nl)
} else if gomips != "" { } else if gomips != "" {
stringutil.WriteToBuilder(b, nl, vFmtGOMIPSHdr, gomips) stringutil.WriteToBuilder(b, vFmtGOMIPSHdr, gomips, nl)
} }
stringutil.WriteToBuilder(b, nl, vFmtRaceHdr, strconv.FormatBool(isRace)) stringutil.WriteToBuilder(b, vFmtRaceHdr, strconv.FormatBool(isRace), nl)
info, ok := debug.ReadBuildInfo() info, ok := debug.ReadBuildInfo()
if !ok { if !ok {
@ -163,10 +162,10 @@ func Verbose() (v string) {
return b.String() return b.String()
} }
stringutil.WriteToBuilder(b, nl, vFmtDepsHdr) stringutil.WriteToBuilder(b, vFmtDepsHdr, nl)
for _, dep := range info.Deps { for _, dep := range info.Deps {
if depStr := fmtModule(dep); depStr != "" { if depStr := fmtModule(dep); depStr != "" {
stringutil.WriteToBuilder(b, "\n\t", depStr) stringutil.WriteToBuilder(b, "\t", depStr, nl)
} }
} }
@ -180,8 +179,8 @@ func writeCommitTime(b *strings.Builder) {
commitTimeUnix, err := strconv.ParseInt(committime, 10, 64) commitTimeUnix, err := strconv.ParseInt(committime, 10, 64)
if err != nil { if err != nil {
stringutil.WriteToBuilder(b, "\n", vFmtTimeHdr, fmt.Sprintf("parse error: %s", err)) stringutil.WriteToBuilder(b, vFmtTimeHdr, fmt.Sprintf("parse error: %s", err), "\n")
} else { } else {
stringutil.WriteToBuilder(b, "\n", vFmtTimeHdr, time.Unix(commitTimeUnix, 0).String()) stringutil.WriteToBuilder(b, vFmtTimeHdr, time.Unix(commitTimeUnix, 0).String(), "\n")
} }
} }

View File

@ -211,7 +211,7 @@ run_linter gocognit --over='10'\
./internal/aghtest/\ ./internal/aghtest/\
./internal/arpdb/\ ./internal/arpdb/\
./internal/client/\ ./internal/client/\
./internal/confmigrate/\ ./internal/configmigrate/\
./internal/dhcpsvc\ ./internal/dhcpsvc\
./internal/dnsforward/\ ./internal/dnsforward/\
./internal/filtering/hashprefix/\ ./internal/filtering/hashprefix/\
@ -250,7 +250,7 @@ run_linter fieldalignment \
./internal/aghtls/\ ./internal/aghtls/\
./internal/arpdb/\ ./internal/arpdb/\
./internal/client/\ ./internal/client/\
./internal/confmigrate/\ ./internal/configmigrate/\
./internal/dhcpsvc/\ ./internal/dhcpsvc/\
./internal/filtering/hashprefix/\ ./internal/filtering/hashprefix/\
./internal/filtering/rewrite/\ ./internal/filtering/rewrite/\
@ -280,7 +280,7 @@ run_linter gosec --quiet\
./internal/aghtest/\ ./internal/aghtest/\
./internal/arpdb/\ ./internal/arpdb/\
./internal/client/\ ./internal/client/\
./internal/confmigrate/\ ./internal/configmigrate/\
./internal/dhcpd/\ ./internal/dhcpd/\
./internal/dhcpsvc/\ ./internal/dhcpsvc/\
./internal/dnsforward/\ ./internal/dnsforward/\