chore: replace old logger with new

This commit is contained in:
2025-01-22 16:37:16 +03:00
parent ac35b4d71d
commit a6076b1253
22 changed files with 707 additions and 221 deletions

View File

@ -21,16 +21,17 @@ package cliutils
import (
"context"
"log/slog"
"os"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/leonelquinteros/gotext"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/db"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/pager"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/translations"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/loggerctx"
)
// YesNoPrompt asks the user a yes or no question, using def as the default answer
@ -54,8 +55,6 @@ func YesNoPrompt(ctx context.Context, msg string, interactive, def bool) (bool,
// shows it if they answer yes, then asks if they'd still like to
// continue, and exits if they answer no.
func PromptViewScript(ctx context.Context, script, name, style string, interactive bool) error {
log := loggerctx.From(ctx)
if !interactive {
return nil
}
@ -78,7 +77,8 @@ func PromptViewScript(ctx context.Context, script, name, style string, interacti
}
if !cont {
log.Fatal(translations.Translator(ctx).TranslateTo("User chose not to continue after reading script", config.Language(ctx))).Send()
slog.Error(gotext.Get("User chose not to continue after reading script"))
os.Exit(1)
}
}
@ -106,13 +106,13 @@ func ShowScript(path, name, style string) error {
// FlattenPkgs attempts to flatten the a map of slices of packages into a single slice
// of packages by prompting the user if multiple packages match.
func FlattenPkgs(ctx context.Context, found map[string][]db.Package, verb string, interactive bool) []db.Package {
log := loggerctx.From(ctx)
var outPkgs []db.Package
for _, pkgs := range found {
if len(pkgs) > 1 && interactive {
choice, err := PkgPrompt(ctx, pkgs, verb, interactive)
if err != nil {
log.Fatal("Error prompting for choice of package").Send()
slog.Error(gotext.Get("Error prompting for choice of package"))
os.Exit(1)
}
outPkgs = append(outPkgs, choice)
} else if len(pkgs) == 1 || !interactive {

View File

@ -21,13 +21,13 @@ package config
import (
"context"
"log/slog"
"os"
"strings"
"sync"
"github.com/leonelquinteros/gotext"
"golang.org/x/text/language"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/loggerctx"
)
var (
@ -43,12 +43,12 @@ var (
func Language(ctx context.Context) language.Tag {
langMtx.Lock()
defer langMtx.Unlock()
log := loggerctx.From(ctx)
if !langSet {
syslang := SystemLang()
tag, err := language.Parse(syslang)
if err != nil {
log.Fatal("Error parsing system language").Err(err).Send()
slog.Error(gotext.Get("Error parsing system language"), "err", err)
os.Exit(1)
}
base, _ := tag.Base()
lang = language.Make(base.String())

View File

@ -21,11 +21,12 @@ package db
import (
"context"
"log/slog"
"github.com/jmoiron/sqlx"
"github.com/leonelquinteros/gotext"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/loggerctx"
)
// CurrentVersion is the current version of the database.
@ -94,7 +95,6 @@ func (d *Database) GetConn() *sqlx.DB {
}
func (d *Database) initDB(ctx context.Context) error {
log := loggerctx.From(ctx)
d.conn = d.conn.Unsafe()
conn := d.conn
_, err := conn.ExecContext(ctx, `
@ -128,11 +128,11 @@ func (d *Database) initDB(ctx context.Context) error {
ver, ok := d.GetVersion(ctx)
if ok && ver != CurrentVersion {
log.Warn("Database version mismatch; resetting").Int("version", ver).Int("expected", CurrentVersion).Send()
slog.Warn(gotext.Get("Database version mismatch; resetting"), "version", ver, "expected", CurrentVersion)
d.reset(ctx)
return d.initDB(ctx)
} else if !ok {
log.Warn("Database version does not exist. Run alr fix if something isn't working.").Send()
slog.Warn(gotext.Get("Database version does not exist. Run alr fix if something isn't working."), "version", ver, "expected", CurrentVersion)
return d.addVersion(ctx, CurrentVersion)
}

View File

@ -18,12 +18,14 @@ package db
import (
"context"
"log/slog"
"os"
"sync"
"github.com/jmoiron/sqlx"
"github.com/leonelquinteros/gotext"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/loggerctx"
)
// DB returns the ALR database.
@ -92,12 +94,12 @@ var (
// Deprecated: For legacy only
func GetInstance(ctx context.Context) *Database {
dbOnce.Do(func() {
log := loggerctx.From(ctx)
cfg := config.GetInstance(ctx)
database = New(cfg)
err := database.Init(ctx)
if err != nil {
log.Fatal("Error opening database").Err(err).Send()
slog.Error(gotext.Get("Error opening database"), "err", err)
os.Exit(1)
}
})
return database

View File

@ -31,11 +31,13 @@ import (
"fmt"
"hash"
"io"
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/PuerkitoBio/purell"
"github.com/leonelquinteros/gotext"
"github.com/vmihailenco/msgpack/v5"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/blake2s"
@ -43,7 +45,6 @@ import (
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/dlcache"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/loggerctx"
)
// Константа для имени файла манифеста кэша
@ -144,7 +145,6 @@ type UpdatingDownloader interface {
// Функция Download загружает файл или каталог с использованием указанных параметров
func Download(ctx context.Context, opts Options) (err error) {
log := loggerctx.From(ctx)
cfg := config.GetInstance(ctx)
dc := dlcache.New(cfg)
@ -166,7 +166,11 @@ func Download(ctx context.Context, opts Options) (err error) {
if ok {
var updated bool
if d, ok := d.(UpdatingDownloader); ok {
log.Info("Source can be updated, updating if required").Str("source", opts.Name).Str("downloader", d.Name()).Send()
slog.Info(
gotext.Get("Source can be updated, updating if required"),
"source", opts.Name,
"downloader", d.Name(),
)
updated, err = d.Update(Options{
Hash: opts.Hash,
@ -193,10 +197,18 @@ func Download(ctx context.Context, opts Options) (err error) {
}
if ok && !updated {
log.Info("Source found in cache and linked to destination").Str("source", opts.Name).Stringer("type", t).Send()
slog.Info(
gotext.Get("Source found in cache and linked to destination"),
"source", opts.Name,
"type", t,
)
return nil
} else if ok {
log.Info("Source updated and linked to destination").Str("source", opts.Name).Stringer("type", t).Send()
slog.Info(
gotext.Get("Source updated and linked to destination"),
"source", opts.Name,
"type", t,
)
return nil
}
} else {
@ -207,7 +219,7 @@ func Download(ctx context.Context, opts Options) (err error) {
}
}
log.Info("Downloading source").Str("source", opts.Name).Str("downloader", d.Name()).Send()
slog.Info("Downloading source", "source", opts.Name, "downloader", d.Name())
cacheDir, err = dc.New(ctx, opts.URL)
if err != nil {

View File

@ -9,6 +9,54 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: build.go:69
msgid "Error pulling repositories"
msgstr ""
#: build.go:75
msgid "Unable to detect a supported package manager on the system"
msgstr ""
#: build.go:86
msgid "Error building package"
msgstr ""
#: build.go:92
msgid "Error getting working directory"
msgstr ""
#: build.go:100
msgid "Error moving the package"
msgstr ""
#: fix.go:43
msgid "Removing cache directory"
msgstr ""
#: fix.go:47
msgid "Unable to remove cache directory"
msgstr ""
#: fix.go:51
msgid "Rebuilding cache"
msgstr ""
#: fix.go:55
msgid "Unable to create new cache directory"
msgstr ""
#: fix.go:61
msgid "Error pulling repos"
msgstr ""
#: fix.go:65
msgid "Done"
msgstr ""
#: helper.go:60
msgid "No such helper command"
msgstr ""
#: info.go:42
msgid "Print information about a package"
msgstr ""
@ -21,10 +69,6 @@ msgstr ""
msgid "Command info expected at least 1 argument, got %d"
msgstr ""
#: info.go:65
msgid "Error pulling repositories"
msgstr ""
#: info.go:71
msgid "Error finding packages"
msgstr ""
@ -37,11 +81,38 @@ msgstr ""
msgid "Error resolving overrides"
msgstr ""
#: info.go:105
#: info.go:111
#: info.go:105 info.go:111
msgid "Error encoding script variables"
msgstr ""
#: install.go:55
msgid "Command install expected at least 1 argument, got %d"
msgstr ""
#: install.go:88
msgid "Error getting packages"
msgstr ""
#: install.go:97
msgid "Error iterating over packages"
msgstr ""
#: install.go:113
msgid "Command remove expected at least 1 argument, got %d"
msgstr ""
#: install.go:125
msgid "Error removing packages"
msgstr ""
#: internal/cliutils/prompt.go:80
msgid "User chose not to continue after reading script"
msgstr ""
#: internal/cliutils/prompt.go:114
msgid "Error prompting for choice of package"
msgstr ""
#: internal/config/config.go:63
msgid "Error opening config file, using defaults"
msgstr ""
@ -78,7 +149,179 @@ msgstr ""
msgid "Unable to create package cache directory"
msgstr ""
#: internal/config/lang.go:50
msgid "Error parsing system language"
msgstr ""
#: internal/db/db.go:131
msgid "Database version mismatch; resetting"
msgstr ""
#: internal/db/db.go:135
msgid ""
"Database version does not exist. Run alr fix if something isn't working."
msgstr ""
#: internal/db/db_legacy.go:101
msgid "Error opening database"
msgstr ""
#: internal/dl/dl.go:170
msgid "Source can be updated, updating if required"
msgstr ""
#: internal/dl/dl.go:201
msgid "Source found in cache and linked to destination"
msgstr ""
#: internal/dl/dl.go:208
msgid "Source updated and linked to destination"
msgstr ""
#: internal/logger/log.go:44
msgid "ERROR"
msgstr ""
#: internal/translations/translations.go:52
msgid "Error creating new translator"
msgstr ""
#: list.go:53
msgid "Error initialization database"
msgstr ""
#: list.go:87
msgid "Error listing installed packages"
msgstr ""
#: main.go:88
msgid ""
"Running ALR as root is forbidden as it may cause catastrophic damage to your "
"system"
msgstr ""
#: main.go:122
msgid "Error while running app"
msgstr ""
#: pkg/build/build.go:104
msgid "Failed to prompt user to view build script"
msgstr ""
#: pkg/build/build.go:108
msgid "Building package"
msgstr ""
#: pkg/build/build.go:152
msgid "Downloading sources"
msgstr ""
#: pkg/build/build.go:164
msgid "Building package metadata"
msgstr ""
#: pkg/build/build.go:186
msgid "Compressing package"
msgstr ""
#: pkg/build/build.go:323
msgid "This package is already installed"
msgstr ""
#: pkg/build/build.go:351
msgid "Installing build dependencies"
msgstr ""
#: pkg/build/build.go:393
msgid "Installing dependencies"
msgstr ""
#: pkg/build/build.go:435
msgid "Executing version()"
msgstr ""
#: pkg/build/build.go:460
msgid "Executing prepare()"
msgstr ""
#: pkg/build/build.go:470
msgid "Executing build()"
msgstr ""
#: pkg/build/build.go:482
msgid "Executing package()"
msgstr ""
#: pkg/build/build.go:557
msgid "AutoProv is not implemented for this package format, so it's skiped"
msgstr ""
#: pkg/build/build.go:568
msgid "AutoReq is not implemented for this package format, so it's skiped"
msgstr ""
#: pkg/build/build.go:759
msgid "The checksums array must be the same length as sources"
msgstr ""
#: pkg/build/findDeps.go:35
msgid "Command not found on the system"
msgstr ""
#: pkg/build/findDeps.go:82
msgid "Provided dependency found"
msgstr ""
#: pkg/build/findDeps.go:89
msgid "Required dependency found"
msgstr ""
#: pkg/build/install.go:42
msgid "Error installing native packages"
msgstr ""
#: pkg/build/install.go:79
msgid "Error installing package"
msgstr ""
#: pkg/repos/pull.go:75
msgid "Pulling repository"
msgstr ""
#: pkg/repos/pull.go:99
msgid "Repository up to date"
msgstr ""
#: pkg/repos/pull.go:156
msgid "Git repository does not appear to be a valid ALR repo"
msgstr ""
#: pkg/repos/pull.go:172
msgid ""
"ALR repo's minumum ALR version is greater than the current version. Try "
"updating ALR if something doesn't work."
msgstr ""
#: repo.go:78 repo.go:133
msgid "Error opening config file"
msgstr ""
#: repo.go:84 repo.go:139
msgid "Error encoding config"
msgstr ""
#: repo.go:125
msgid "Repo does not exist"
msgstr ""
#: repo.go:145
msgid "Error removing repo directory"
msgstr ""
#: repo.go:151
msgid "Error removing packages from database"
msgstr ""
#: upgrade.go:78
msgid "Error checking for updates"
msgstr ""

View File

@ -3,17 +3,65 @@
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"PO-Revision-Date: 2025-01-22 14:23+0300\n"
"Last-Translator: Maxim Slipenko <maks1ms@alt-gnome.ru>\n"
"Language-Team: Russian\n"
"Language: ru\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && "
"n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"X-Generator: Gtranslator 47.1\n"
"Project-Id-Version: \n"
"Language-Team: Russian\n"
#: build.go:69
msgid "Error pulling repositories"
msgstr ""
#: build.go:75
msgid "Unable to detect a supported package manager on the system"
msgstr ""
#: build.go:86
msgid "Error building package"
msgstr ""
#: build.go:92
msgid "Error getting working directory"
msgstr ""
#: build.go:100
msgid "Error moving the package"
msgstr ""
#: fix.go:43
msgid "Removing cache directory"
msgstr ""
#: fix.go:47
msgid "Unable to remove cache directory"
msgstr ""
#: fix.go:51
msgid "Rebuilding cache"
msgstr ""
#: fix.go:55
msgid "Unable to create new cache directory"
msgstr ""
#: fix.go:61
msgid "Error pulling repos"
msgstr ""
#: fix.go:65
msgid "Done"
msgstr ""
#: helper.go:60
msgid "No such helper command"
msgstr ""
#: info.go:42
msgid "Print information about a package"
@ -27,10 +75,6 @@ msgstr "Показывать всю информацию, а не только
msgid "Command info expected at least 1 argument, got %d"
msgstr ""
#: info.go:65
msgid "Error pulling repositories"
msgstr ""
#: info.go:71
msgid "Error finding packages"
msgstr ""
@ -47,6 +91,34 @@ msgstr ""
msgid "Error encoding script variables"
msgstr ""
#: install.go:55
msgid "Command install expected at least 1 argument, got %d"
msgstr ""
#: install.go:88
msgid "Error getting packages"
msgstr ""
#: install.go:97
msgid "Error iterating over packages"
msgstr ""
#: install.go:113
msgid "Command remove expected at least 1 argument, got %d"
msgstr ""
#: install.go:125
msgid "Error removing packages"
msgstr ""
#: internal/cliutils/prompt.go:80
msgid "User chose not to continue after reading script"
msgstr ""
#: internal/cliutils/prompt.go:114
msgid "Error prompting for choice of package"
msgstr ""
#: internal/config/config.go:63
msgid "Error opening config file, using defaults"
msgstr ""
@ -83,6 +155,179 @@ msgstr ""
msgid "Unable to create package cache directory"
msgstr ""
#: internal/config/lang.go:50
msgid "Error parsing system language"
msgstr ""
#: internal/db/db.go:131
msgid "Database version mismatch; resetting"
msgstr ""
#: internal/db/db.go:135
msgid ""
"Database version does not exist. Run alr fix if something isn't working."
msgstr ""
#: internal/db/db_legacy.go:101
msgid "Error opening database"
msgstr ""
#: internal/dl/dl.go:170
msgid "Source can be updated, updating if required"
msgstr ""
#: internal/dl/dl.go:201
msgid "Source found in cache and linked to destination"
msgstr ""
#: internal/dl/dl.go:208
msgid "Source updated and linked to destination"
msgstr ""
#: internal/logger/log.go:44
msgid "ERROR"
msgstr "ОШИБКА"
#: internal/translations/translations.go:52
msgid "Error creating new translator"
msgstr ""
#: list.go:53
msgid "Error initialization database"
msgstr ""
#: list.go:87
msgid "Error listing installed packages"
msgstr ""
#: main.go:88
msgid ""
"Running ALR as root is forbidden as it may cause catastrophic damage to your "
"system"
msgstr ""
#: main.go:122
msgid "Error while running app"
msgstr ""
#: pkg/build/build.go:104
msgid "Failed to prompt user to view build script"
msgstr ""
#: pkg/build/build.go:108
msgid "Building package"
msgstr ""
#: pkg/build/build.go:152
msgid "Downloading sources"
msgstr ""
#: pkg/build/build.go:164
msgid "Building package metadata"
msgstr ""
#: pkg/build/build.go:186
msgid "Compressing package"
msgstr ""
#: pkg/build/build.go:323
msgid "This package is already installed"
msgstr ""
#: pkg/build/build.go:351
msgid "Installing build dependencies"
msgstr ""
#: pkg/build/build.go:393
msgid "Installing dependencies"
msgstr ""
#: pkg/build/build.go:435
msgid "Executing version()"
msgstr ""
#: pkg/build/build.go:460
msgid "Executing prepare()"
msgstr ""
#: pkg/build/build.go:470
msgid "Executing build()"
msgstr ""
#: pkg/build/build.go:482
msgid "Executing package()"
msgstr ""
#: pkg/build/build.go:557
msgid "AutoProv is not implemented for this package format, so it's skiped"
msgstr ""
#: pkg/build/build.go:568
msgid "AutoReq is not implemented for this package format, so it's skiped"
msgstr ""
#: pkg/build/build.go:759
msgid "The checksums array must be the same length as sources"
msgstr ""
#: pkg/build/findDeps.go:35
msgid "Command not found on the system"
msgstr ""
#: pkg/build/findDeps.go:82
msgid "Provided dependency found"
msgstr ""
#: pkg/build/findDeps.go:89
msgid "Required dependency found"
msgstr ""
#: pkg/build/install.go:42
msgid "Error installing native packages"
msgstr ""
#: pkg/build/install.go:79
msgid "Error installing package"
msgstr ""
#: pkg/repos/pull.go:75
msgid "Pulling repository"
msgstr ""
#: pkg/repos/pull.go:99
msgid "Repository up to date"
msgstr ""
#: pkg/repos/pull.go:156
msgid "Git repository does not appear to be a valid ALR repo"
msgstr ""
#: pkg/repos/pull.go:172
msgid ""
"ALR repo's minumum ALR version is greater than the current version. Try "
"updating ALR if something doesn't work."
msgstr ""
#: repo.go:78 repo.go:133
msgid "Error opening config file"
msgstr ""
#: repo.go:84 repo.go:139
msgid "Error encoding config"
msgstr ""
#: repo.go:125
msgid "Repo does not exist"
msgstr ""
#: repo.go:145
msgid "Error removing repo directory"
msgstr ""
#: repo.go:151
msgid "Error removing packages from database"
msgstr ""
#: upgrade.go:78
msgid "Error checking for updates"
msgstr ""

View File

@ -23,6 +23,7 @@ import (
"context"
"embed"
"io/fs"
"log/slog"
"os"
"path"
"sync"
@ -32,8 +33,6 @@ import (
"go.elara.ws/logger"
"go.elara.ws/translate"
"golang.org/x/text/language"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/loggerctx"
)
//go:embed files
@ -47,11 +46,11 @@ var (
func Translator(ctx context.Context) *translate.Translator {
mu.Lock()
defer mu.Unlock()
log := loggerctx.From(ctx)
if translator == nil {
t, err := translate.NewFromFS(translationFS)
if err != nil {
log.Fatal("Error creating new translator").Err(err).Send()
slog.Error(gotext.Get("Error creating new translator"), "err", err)
os.Exit(1)
}
translator = &t
}