Pull request 2274: AGDNS-2374-slog-scripts

Squashed commit of the following:

commit 257bd542f78d6e06a6b4783a050b573240a2b5ca
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Sep 2 17:33:12 2024 +0300

    scripts: slog
This commit is contained in:
Stanislav Chzhen
2024-09-02 18:03:37 +03:00
parent aab6769fa2
commit 76344f9785
3 changed files with 48 additions and 26 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)