all: resync with master

This commit is contained in:
Ainar Garipov
2024-09-30 20:17:20 +03:00
parent c7d8b9ede1
commit 8cb5781770
153 changed files with 28633 additions and 27594 deletions

View File

@@ -4,8 +4,10 @@
package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
@@ -14,10 +16,13 @@ import (
"text/template"
"time"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
func main() {
ctx := context.Background()
l := slogutil.New(nil)
urlStr := "https://adguardteam.github.io/HostlistsRegistry/assets/services.json"
if v, ok := os.LookupEnv("URL"); ok {
urlStr = v
@@ -33,7 +38,7 @@ func main() {
resp, err := c.Get(urlStr)
check(err)
defer log.OnCloserError(resp.Body, log.ERROR)
defer slogutil.CloseAndLog(ctx, l, resp.Body, slog.LevelError)
if resp.StatusCode != http.StatusOK {
panic(fmt.Errorf("expected code %d, got %d", http.StatusOK, resp.StatusCode))
@@ -64,7 +69,7 @@ func main() {
0o644,
)
check(err)
defer log.OnCloserError(f, log.ERROR)
defer slogutil.CloseAndLog(ctx, l, f, slog.LevelError)
err = tmpl.Execute(f, hlSvcs)
check(err)

View File

@@ -6,7 +6,7 @@ set -e -f -u
# Bump this number every time a significant change is made to this
# script.
#
# AdGuard-Project-Version: 2
# AdGuard-Project-Version: 3
# TODO(a.garipov): Add pre-merge-commit.
@@ -56,13 +56,15 @@ prompt() {
# Warn the programmer about unstaged changes and untracked files, but do
# not fail the commit, because those changes might be temporary or for
# a different branch.
#
# shellcheck disable=SC2016
awk_prog='substr($2, 2, 1) != "." { print $9; } $1 == "?" { print $2; }'
readonly awk_prog
unstaged="$( git status --porcelain=2 | awk "$awk_prog" )"
readonly unstaged
if [ "$unstaged" != "" ]
if [ "$unstaged" != '' ]
then
printf 'WARNING: you have unstaged changes:\n\n%s\n\n' "$unstaged"
prompt
@@ -73,7 +75,7 @@ fi
temp_todos="$( git grep -e 'TODO.*!!' -- ':!scripts/hooks/pre-commit' || : )"
readonly temp_todos
if [ "$temp_todos" != "" ]
if [ "$temp_todos" != '' ]
then
printf 'WARNING: you have temporary todos:\n\n%s\n\n' "$temp_todos"
prompt
@@ -82,22 +84,32 @@ fi
verbose="${VERBOSE:-0}"
readonly verbose
if [ "$( git diff --cached --name-only -- 'client/*.js' )" ]
if [ "$( git diff --cached --name-only -- 'client/*.js' || true )" != '' ]
then
make VERBOSE="$verbose" js-lint js-test
fi
if [ "$( git diff --cached --name-only -- '*.go' '*.mod' '*.sh' 'Makefile' )" ]
if [ "$( git diff --cached --name-only -- '*.go' '*.mod' 'Makefile' || true )" != '' ]
then
make VERBOSE="$verbose" go-os-check go-lint go-test
fi
if [ "$( git diff --cached --name-only -- '*.md' '*.txt' '*.yaml' '*.yml' )" ]
if [ "$( git diff --cached --name-only -- '*.md' || true )" != '' ]
then
make VERBOSE="$verbose" md-lint
fi
if [ "$( git diff --cached --name-only -- '*.sh' || true )" != '' ]
then
make VERBOSE="$verbose" sh-lint
fi
if [ "$( git diff --cached --name-only -- '*.md' '*.txt' '*.yaml' '*.yml' || true )" != '' ]
then
make VERBOSE="$verbose" txt-lint
fi
if [ "$( git diff --cached --name-only -- './openapi/openapi.yaml' )" ]
if [ "$( git diff --cached --name-only -- './openapi/openapi.yaml' || true )" != '' ]
then
make VERBOSE="$verbose" openapi-lint
fi

View File

@@ -83,11 +83,15 @@ if [ "$sign" -eq '1' ]
then
gpg_key_passphrase="${GPG_KEY_PASSPHRASE:?please set GPG_KEY_PASSPHRASE or unset SIGN}"
gpg_key="${GPG_KEY:?please set GPG_KEY or unset SIGN}"
signer_api_key="${SIGNER_API_KEY:?please set SIGNER_API_KEY or unset SIGN}"
deploy_script_path="${DEPLOY_SCRIPT_PATH:?please set DEPLOY_SCRIPT_PATH or unset SIGN}"
else
gpg_key_passphrase=''
gpg_key=''
signer_api_key=''
deploy_script_path=''
fi
readonly gpg_key_passphrase gpg_key
readonly gpg_key_passphrase gpg_key signer_api_key deploy_script_path
# The default distribution files directory is dist.
dist="${DIST_DIR:-dist}"
@@ -141,6 +145,7 @@ linux mips64 - softfloat
linux mips64le - softfloat
linux mipsle - softfloat
linux ppc64le - -
linux riscv64 - -
openbsd amd64 - -
openbsd arm64 - -
windows 386 - -
@@ -148,6 +153,50 @@ windows amd64 - -
windows arm64 - -"
readonly platforms
# Function sign signs the specified build as intended by the target operating
# system.
sign() {
# Only sign if needed.
if [ "$sign" -ne '1' ]
then
return
fi
# Get the arguments. Here and below, use the "sign_" prefix for all
# variables local to function sign.
sign_os="$1"
sign_bin_path="$2"
if [ "$sign_os" != 'windows' ]
then
gpg\
--default-key "$gpg_key"\
--detach-sig\
--passphrase "$gpg_key_passphrase"\
--pinentry-mode loopback\
-q\
"$sign_bin_path"\
;
return
# TODO(e.burkov): Enable for all releases.
elif [ "$channel" != 'beta' ]
then
return
fi
signed_bin_path="${sign_bin_path}.signed"
env\
INPUT_FILE="$sign_bin_path"\
OUTPUT_FILE="$signed_bin_path"\
SIGNER_API_KEY="$signer_api_key"\
"$deploy_script_path" sign-executable\
;
mv "$signed_bin_path" "$sign_bin_path"
}
# Function build builds the release for one platform. It builds a binary and an
# archive.
build() {
@@ -188,17 +237,7 @@ build() {
log "$build_output"
if [ "$sign" -eq '1' ]
then
gpg\
--default-key "$gpg_key"\
--detach-sig\
--passphrase "$gpg_key_passphrase"\
--pinentry-mode loopback\
-q\
"$build_output"\
;
fi
sign "$os" "$build_output"
# Prepare the build directory for archiving.
cp ./CHANGELOG.md ./LICENSE.txt ./README.md "$build_dir"

View File

@@ -91,6 +91,7 @@ elif [ "${GOMIPS:-}" != '' ]
then
ldflags="${ldflags} -X ${version_pkg}.gomips=${GOMIPS}"
fi
readonly ldflags
# Allow users to limit the build's parallelism.
parallelism="${PARALLELISM:-}"
@@ -143,7 +144,7 @@ then
fi
"$go" build\
--ldflags "$ldflags"\
--ldflags="$ldflags"\
"$race_flags"\
"$tags_flags"\
--trimpath\

View File

@@ -3,7 +3,7 @@
# This comment is used to simplify checking local copies of the script. Bump
# this number every time a significant change is made to this script.
#
# AdGuard-Project-Version: 5
# AdGuard-Project-Version: 8
verbose="${VERBOSE:-0}"
readonly verbose
@@ -35,9 +35,12 @@ set -f -u
# blocklist_imports is a simple check against unwanted packages. The following
# packages are banned:
#
# * Packages errors and log are replaced by our own packages in the
# * Package errors is replaced by our own package in the
# github.com/AdguardTeam/golibs module.
#
# * Packages golang.org/x/exp/slices and golang.org/x/net/context have been
# moved into stdlib.
#
# * Package io/ioutil is soft-deprecated.
#
# * Package reflect is often an overkill, and for deep comparisons there are
@@ -51,10 +54,6 @@ set -f -u
#
# * Package unsafe is… unsafe.
#
# * Package golang.org/x/exp/slices has been moved into stdlib.
#
# * Package golang.org/x/net/context has been moved into stdlib.
#
# Currently, the only standard exception are files generated from protobuf
# schemas, which use package reflect. If your project needs more exceptions,
# add and document them.
@@ -62,17 +61,17 @@ set -f -u
# TODO(a.garipov): Add golibs/log.
#
# TODO(a.garipov): Add deprecated package golang.org/x/exp/maps once all
# projects switch to Go 1.22.
# projects switch to Go 1.23.
blocklist_imports() {
git grep\
-e '[[:space:]]"errors"$'\
-e '[[:space:]]"golang.org/x/exp/slices"$'\
-e '[[:space:]]"golang.org/x/net/context"$'\
-e '[[:space:]]"io/ioutil"$'\
-e '[[:space:]]"log"$'\
-e '[[:space:]]"reflect"$'\
-e '[[:space:]]"sort"$'\
-e '[[:space:]]"unsafe"$'\
-e '[[:space:]]"golang.org/x/exp/slices"$'\
-e '[[:space:]]"golang.org/x/net/context"$'\
-n\
-- '*.go'\
':!*.pb.go'\
@@ -105,6 +104,7 @@ underscores() {
-e '_bsd.go'\
-e '_darwin.go'\
-e '_freebsd.go'\
-e '_generate.go'\
-e '_linux.go'\
-e '_next.go'\
-e '_openbsd.go'\
@@ -140,7 +140,7 @@ run_linter -e gofumpt --extra -e -l .
# TODO(a.garipov): golint is deprecated, find a suitable replacement.
run_linter "$GO" vet ./...
run_linter "${GO:-go}" vet ./...
run_linter govulncheck ./...
@@ -212,8 +212,6 @@ git ls-files -- 'Makefile' '*.conf' '*.go' '*.mod' '*.sh' '*.yaml' '*.yml'\
| xargs misspell --error\
| sed -e 's/^/misspell: /'
run_linter looppointer ./...
run_linter nilness ./...
# TODO(a.garipov): Enable for all.
@@ -246,9 +244,9 @@ run_linter fieldalignment \
run_linter -e shadow --strict ./...
# TODO(a.garipov): Enable for all.
run_linter gosec --quiet\
# TODO(e.burkov): Re-enable G115.
run_linter gosec --exclude G115 --quiet\
./internal/aghalg/\
./internal/aghchan/\
./internal/aghhttp/\
./internal/aghnet/\
./internal/aghos/\

View File

@@ -3,7 +3,7 @@
# This comment is used to simplify checking local copies of the script. Bump
# this number every time a significant change is made to this script.
#
# AdGuard-Project-Version: 3
# AdGuard-Project-Version: 4
verbose="${VERBOSE:-0}"
readonly verbose
@@ -42,7 +42,6 @@ rm -f\
bin/gosec\
bin/govulncheck\
bin/ineffassign\
bin/looppointer\
bin/misspell\
bin/nilness\
bin/shadow\
@@ -66,7 +65,6 @@ env\
github.com/golangci/misspell/cmd/misspell\
github.com/gordonklaus/ineffassign\
github.com/kisielk/errcheck\
github.com/kyoh86/looppointer/cmd/looppointer\
github.com/securego/gosec/v2/cmd/gosec\
github.com/uudashr/gocognit/cmd/gocognit\
golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment\

View File

@@ -3,7 +3,7 @@
# This comment is used to simplify checking local copies of the script. Bump
# this number every time a significant change is made to this script.
#
# AdGuard-Project-Version: 1
# AdGuard-Project-Version: 2
verbose="${VERBOSE:-0}"
readonly verbose
@@ -29,5 +29,6 @@ go="${GO:-go}"
readonly go
cd ./internal/tools/
"$go" get -u
"$go" get -u "$x_flags"
"$go" mod tidy

23
scripts/make/md-lint.sh Normal file
View File

@@ -0,0 +1,23 @@
#!/bin/sh
# This comment is used to simplify checking local copies of the script. Bump
# this number every time a remarkable change is made to this script.
#
# AdGuard-Project-Version: 2
verbose="${VERBOSE:-0}"
readonly verbose
set -e -f -u
if [ "$verbose" -gt '0' ]
then
set -x
fi
# NOTE: Adjust for your project.
# markdownlint\
# ./README.md\
# ;
# TODO(e.burkov): Lint markdown documents within this project.

39
scripts/make/sh-lint.sh Normal file
View File

@@ -0,0 +1,39 @@
#!/bin/sh
# This comment is used to simplify checking local copies of the script. Bump
# this number every time a remarkable change is made to this script.
#
# AdGuard-Project-Version: 2
verbose="${VERBOSE:-0}"
readonly verbose
# Don't use -f, because we use globs in this script.
set -e -u
if [ "$verbose" -gt '0' ]
then
set -x
fi
# NOTE: Adjust for your project.
#
# TODO(e.burkov): Add build-docker.sh, build-release.sh and install.sh.
shellcheck -e 'SC2250' -f 'gcc' -o 'all' -x --\
./scripts/hooks/*\
./scripts/snap/*\
./scripts/make/clean.sh\
./scripts/make/go-bench.sh\
./scripts/make/go-build.sh\
./scripts/make/go-deps.sh\
./scripts/make/go-fuzz.sh\
./scripts/make/go-lint.sh\
./scripts/make/go-test.sh\
./scripts/make/go-tools.sh\
./scripts/make/go-upd-tools.sh\
./scripts/make/helper.sh\
./scripts/make/md-lint.sh\
./scripts/make/sh-lint.sh\
./scripts/make/txt-lint.sh\
./scripts/make/version.sh\
;

View File

@@ -1,25 +1,26 @@
package main
import (
"context"
"flag"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/ioutil"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
// download and save all translations.
func (c *twoskyClient) download() (err error) {
func (c *twoskyClient) download(ctx context.Context, l *slog.Logger) (err error) {
var numWorker int
flagSet := flag.NewFlagSet("download", flag.ExitOnError)
@@ -50,7 +51,7 @@ func (c *twoskyClient) download() (err error) {
for range numWorker {
wg.Add(1)
go downloadWorker(wg, failed, client, uriCh)
go downloadWorker(ctx, l, wg, failed, client, uriCh)
}
for _, lang := range c.langs {
@@ -62,13 +63,13 @@ func (c *twoskyClient) download() (err error) {
close(uriCh)
wg.Wait()
printFailedLocales(failed)
printFailedLocales(ctx, l, failed)
return nil
}
// printFailedLocales prints sorted list of failed downloads, if any.
func printFailedLocales(failed *sync.Map) {
func printFailedLocales(ctx context.Context, l *slog.Logger, failed *sync.Map) {
keys := []string{}
failed.Range(func(k, _ any) bool {
s, ok := k.(string)
@@ -86,12 +87,14 @@ func printFailedLocales(failed *sync.Map) {
}
slices.Sort(keys)
log.Info("failed locales: %s", strings.Join(keys, " "))
l.InfoContext(ctx, "failed", "locales", keys)
}
// downloadWorker downloads translations by received urls and saves them.
// Where failed is a map for storing failed downloads.
func downloadWorker(
ctx context.Context,
l *slog.Logger,
wg *sync.WaitGroup,
failed *sync.Map,
client *http.Client,
@@ -103,9 +106,9 @@ func downloadWorker(
q := uri.Query()
code := q.Get("language")
err := saveToFile(client, uri, code)
err := saveToFile(ctx, l, client, uri, code)
if err != nil {
log.Error("download: worker: %s", err)
l.ErrorContext(ctx, "download worker", slogutil.KeyError, err)
failed.Store(code, struct{}{})
}
}
@@ -113,12 +116,16 @@ func downloadWorker(
// saveToFile downloads translation by url and saves it to a file, or returns
// error.
func saveToFile(client *http.Client, uri *url.URL, code string) (err error) {
data, err := getTranslation(client, uri.String())
func saveToFile(
ctx context.Context,
l *slog.Logger,
client *http.Client,
uri *url.URL,
code string,
) (err error) {
data, err := getTranslation(ctx, l, client, uri.String())
if err != nil {
log.Info("%s", data)
return fmt.Errorf("getting translation: %s", err)
return fmt.Errorf("getting translation %q: %s", code, err)
}
name := filepath.Join(localesDir, code+".json")
@@ -134,13 +141,18 @@ func saveToFile(client *http.Client, uri *url.URL, code string) (err error) {
// getTranslation returns received translation data and error. If err is not
// nil, data may contain a response from server for inspection.
func getTranslation(client *http.Client, url string) (data []byte, err error) {
func getTranslation(
ctx context.Context,
l *slog.Logger,
client *http.Client,
url string,
) (data []byte, err error) {
resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("requesting: %w", err)
}
defer log.OnCloserError(resp.Body, log.ERROR)
defer slogutil.CloseAndLog(ctx, l, resp.Body, slog.LevelError)
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("url: %q; status code: %s", url, http.StatusText(resp.StatusCode))

View File

@@ -6,8 +6,10 @@ import (
"bufio"
"bytes"
"cmp"
"context"
"encoding/json"
"fmt"
"log/slog"
"net/url"
"os"
"os/exec"
@@ -18,7 +20,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"golang.org/x/exp/maps"
)
@@ -63,6 +65,9 @@ type textLabel string
type locales map[textLabel]string
func main() {
ctx := context.Background()
l := slogutil.New(nil)
if len(os.Args) == 1 {
usage("need a command")
}
@@ -83,9 +88,9 @@ func main() {
cli, err = conf.toClient()
check(err)
err = cli.download()
err = cli.download(ctx, l)
case "unused":
err = unused(conf.LocalizableFiles[0])
err = unused(ctx, l, conf.LocalizableFiles[0])
case "upload":
cli, err = conf.toClient()
check(err)
@@ -322,7 +327,7 @@ func summary(langs languages) (err error) {
}
// unused prints unused text labels.
func unused(basePath string) (err error) {
func unused(ctx context.Context, l *slog.Logger, basePath string) (err error) {
defer func() { err = errors.Annotate(err, "unused: %w") }()
baseLoc, err := readLocales(basePath)
@@ -331,7 +336,7 @@ func unused(basePath string) (err error) {
}
locDir := filepath.Clean(localesDir)
js, err := findJS(locDir)
js, err := findJS(ctx, l, locDir)
if err != nil {
return err
}
@@ -340,10 +345,10 @@ func unused(basePath string) (err error) {
}
// findJS returns list of JavaScript and JSON files or error.
func findJS(locDir string) (fileNames []string, err error) {
func findJS(ctx context.Context, l *slog.Logger, locDir string) (fileNames []string, err error) {
walkFn := func(name string, _ os.FileInfo, err error) error {
if err != nil {
log.Info("warning: accessing a path %q: %s", name, err)
l.WarnContext(ctx, "accessing a path", slogutil.KeyError, err)
return nil
}