refactor(yay): move cfg inside of runtime (#2259)

* rework relationship between runtime and cfg

* separate runtime from cfg

* simplify instantiation logic

* move installer to appropriate package

* move operator to sync package

* add tests for srcinfo service

* consolidate srcinfo service in sync

* add logger to srcinfo

* add logger to preparer

* remove unused text functions

* remove remaining text.* from srcinfo

* remove global logger parts

* remove global org method exports

* remove global logger

* move text->input

* add rule to prevent fmt.Print

* update golangci go version

* remove outdated FAQs

* remove outdated FAQs
This commit is contained in:
Jo
2023-08-06 21:39:41 +02:00
committed by GitHub
parent 7483393377
commit 8916cd174b
74 changed files with 1475 additions and 1367 deletions

184
print.go
View File

@@ -6,14 +6,19 @@ import (
"io"
"os"
"strconv"
"strings"
"syscall"
"unicode"
aur "github.com/Jguer/aur"
mapset "github.com/deckarep/golang-set/v2"
"github.com/leonelquinteros/gotext"
"golang.org/x/sys/unix"
"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/dep"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/runtime"
"github.com/Jguer/yay/v12/pkg/settings"
"github.com/Jguer/yay/v12/pkg/settings/parser"
"github.com/Jguer/yay/v12/pkg/text"
@@ -21,47 +26,47 @@ import (
)
// printInfo prints package info like pacman -Si.
func printInfo(config *settings.Configuration, a *aur.Pkg, extendedInfo bool) {
text.PrintInfoValue(gotext.Get("Repository"), "aur")
text.PrintInfoValue(gotext.Get("Name"), a.Name)
text.PrintInfoValue(gotext.Get("Version"), a.Version)
text.PrintInfoValue(gotext.Get("Description"), a.Description)
text.PrintInfoValue(gotext.Get("URL"), a.URL)
text.PrintInfoValue(gotext.Get("Licenses"), a.License...)
text.PrintInfoValue(gotext.Get("Groups"), a.Groups...)
text.PrintInfoValue(gotext.Get("Provides"), a.Provides...)
text.PrintInfoValue(gotext.Get("Depends On"), a.Depends...)
text.PrintInfoValue(gotext.Get("Optional Deps"), a.OptDepends...)
text.PrintInfoValue(gotext.Get("Make Deps"), a.MakeDepends...)
text.PrintInfoValue(gotext.Get("Check Deps"), a.CheckDepends...)
text.PrintInfoValue(gotext.Get("Conflicts With"), a.Conflicts...)
text.PrintInfoValue(gotext.Get("Replaces"), a.Replaces...)
text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
text.PrintInfoValue(gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
text.PrintInfoValue(gotext.Get("Keywords"), a.Keywords...)
text.PrintInfoValue(gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
func printInfo(logger *text.Logger, config *settings.Configuration, a *aur.Pkg, extendedInfo bool) {
printInfoValue(logger, gotext.Get("Repository"), "aur")
printInfoValue(logger, gotext.Get("Name"), a.Name)
printInfoValue(logger, gotext.Get("Version"), a.Version)
printInfoValue(logger, gotext.Get("Description"), a.Description)
printInfoValue(logger, gotext.Get("URL"), a.URL)
printInfoValue(logger, gotext.Get("Licenses"), a.License...)
printInfoValue(logger, gotext.Get("Groups"), a.Groups...)
printInfoValue(logger, gotext.Get("Provides"), a.Provides...)
printInfoValue(logger, gotext.Get("Depends On"), a.Depends...)
printInfoValue(logger, gotext.Get("Optional Deps"), a.OptDepends...)
printInfoValue(logger, gotext.Get("Make Deps"), a.MakeDepends...)
printInfoValue(logger, gotext.Get("Check Deps"), a.CheckDepends...)
printInfoValue(logger, gotext.Get("Conflicts With"), a.Conflicts...)
printInfoValue(logger, gotext.Get("Replaces"), a.Replaces...)
printInfoValue(logger, gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
printInfoValue(logger, gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
printInfoValue(logger, gotext.Get("Keywords"), a.Keywords...)
printInfoValue(logger, gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
printInfoValue(logger, gotext.Get("Maintainer"), a.Maintainer)
printInfoValue(logger, gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
printInfoValue(logger, gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
if a.OutOfDate != 0 {
text.PrintInfoValue(gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
printInfoValue(logger, gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
} else {
text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
printInfoValue(logger, gotext.Get("Out-of-date"), "No")
}
if extendedInfo {
text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
printInfoValue(logger, "ID", fmt.Sprintf("%d", a.ID))
printInfoValue(logger, gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
printInfoValue(logger, gotext.Get("Package Base"), a.PackageBase)
printInfoValue(logger, gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
}
fmt.Println()
logger.Println()
}
// BiggestPackages prints the name of the ten biggest packages in the system.
func biggestPackages(dbExecutor db.Executor) {
func biggestPackages(logger *text.Logger, dbExecutor db.Executor) {
pkgS := dbExecutor.BiggestPackages()
if len(pkgS) < 10 {
@@ -69,34 +74,34 @@ func biggestPackages(dbExecutor db.Executor) {
}
for i := 0; i < 10; i++ {
fmt.Printf("%s: %s\n", text.Bold(pkgS[i].Name()), text.Cyan(text.Human(pkgS[i].ISize())))
logger.Printf("%s: %s\n", text.Bold(pkgS[i].Name()), text.Cyan(text.Human(pkgS[i].ISize())))
}
}
// localStatistics prints installed packages statistics.
func localStatistics(ctx context.Context, cfg *settings.Configuration, dbExecutor db.Executor) error {
info := statistics(cfg, dbExecutor)
func localStatistics(ctx context.Context, run *runtime.Runtime, dbExecutor db.Executor) error {
info := statistics(run, dbExecutor)
remoteNames := dbExecutor.InstalledRemotePackageNames()
remote := dbExecutor.InstalledRemotePackages()
text.Infoln(gotext.Get("Yay version v%s", yayVersion))
fmt.Println(text.Bold(text.Cyan("===========================================")))
text.Infoln(gotext.Get("Total installed packages: %s", text.Cyan(strconv.Itoa(info.Totaln))))
text.Infoln(gotext.Get("Foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames)))))
text.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln))))
text.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize))))
run.Logger.Infoln(gotext.Get("Yay version v%s", yayVersion))
run.Logger.Println(text.Bold(text.Cyan("===========================================")))
run.Logger.Infoln(gotext.Get("Total installed packages: %s", text.Cyan(strconv.Itoa(info.Totaln))))
run.Logger.Infoln(gotext.Get("Foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames)))))
run.Logger.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln))))
run.Logger.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize))))
for path, size := range info.pacmanCaches {
text.Infoln(gotext.Get("Size of pacman cache %s: %s", path, text.Cyan(text.Human(size))))
run.Logger.Infoln(gotext.Get("Size of pacman cache %s: %s", path, text.Cyan(text.Human(size))))
}
text.Infoln(gotext.Get("Size of yay cache %s: %s", cfg.BuildDir, text.Cyan(text.Human(info.yayCache))))
fmt.Println(text.Bold(text.Cyan("===========================================")))
text.Infoln(gotext.Get("Ten biggest packages:"))
biggestPackages(dbExecutor)
fmt.Println(text.Bold(text.Cyan("===========================================")))
run.Logger.Infoln(gotext.Get("Size of yay cache %s: %s", run.Cfg.BuildDir, text.Cyan(text.Human(info.yayCache))))
run.Logger.Println(text.Bold(text.Cyan("===========================================")))
run.Logger.Infoln(gotext.Get("Ten biggest packages:"))
biggestPackages(run.Logger, dbExecutor)
run.Logger.Println(text.Bold(text.Cyan("===========================================")))
aurData, err := cfg.Runtime.AURClient.Get(ctx, &aur.Query{
aurData, err := run.AURClient.Get(ctx, &aur.Query{
Needles: remoteNames,
By: aur.Name,
})
@@ -104,7 +109,7 @@ func localStatistics(ctx context.Context, cfg *settings.Configuration, dbExecuto
return err
}
warnings := query.NewWarnings(cfg.Runtime.Logger.Child("print"))
warnings := query.NewWarnings(run.Logger.Child("warnings"))
for i := range aurData {
warnings.AddToWarnings(remote, &aurData[i])
}
@@ -114,13 +119,13 @@ func localStatistics(ctx context.Context, cfg *settings.Configuration, dbExecuto
return nil
}
func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments,
func printUpdateList(ctx context.Context, run *runtime.Runtime, cmdArgs *parser.Arguments,
dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter,
) error {
quietMode := cmdArgs.ExistsArg("q", "quiet")
// TODO: handle quiet mode in a better way
logger := text.NewLogger(io.Discard, os.Stderr, os.Stdin, cfg.Debug, "update-list")
logger := text.NewLogger(io.Discard, os.Stderr, os.Stdin, run.Cfg.Debug, "update-list")
dbExecutor.SetLogger(logger.Child("db"))
oldNoConfirm := settings.NoConfirm
settings.NoConfirm = true
@@ -128,12 +133,12 @@ func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *
defer func() { settings.NoConfirm = oldNoConfirm }()
targets := mapset.NewThreadUnsafeSet(cmdArgs.Targets...)
grapher := dep.NewGrapher(dbExecutor, cfg.Runtime.AURClient, false, true,
grapher := dep.NewGrapher(dbExecutor, run.AURClient, false, true,
false, false, cmdArgs.ExistsArg("needed"), logger.Child("grapher"))
upService := upgrade.NewUpgradeService(
grapher, cfg.Runtime.AURClient, dbExecutor, cfg.Runtime.VCSStore,
cfg, true, logger.Child("upgrade"))
grapher, run.AURClient, dbExecutor, run.VCSStore,
run.Cfg, true, logger.Child("upgrade"))
graph, errSysUp := upService.GraphUpgrades(ctx, nil,
enableDowngrade, filter)
@@ -163,9 +168,9 @@ func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *
}
if quietMode {
fmt.Printf("%s\n", pkgName)
run.Logger.Printf("%s\n", pkgName)
} else {
fmt.Printf("%s %s -> %s\n", text.Bold(pkgName), text.Bold(text.Green(ii.LocalVersion)),
run.Logger.Printf("%s %s -> %s\n", text.Bold(pkgName), text.Bold(text.Green(ii.LocalVersion)),
text.Bold(text.Green(ii.Version)))
}
@@ -179,7 +184,7 @@ func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *
missing := false
targets.Each(func(pkgName string) bool {
if dbExecutor.LocalPackage(pkgName) == nil {
cfg.Runtime.Logger.Errorln(gotext.Get("package '%s' was not found", pkgName))
run.Logger.Errorln(gotext.Get("package '%s' was not found", pkgName))
missing = true
}
return false
@@ -191,3 +196,72 @@ func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *
return nil
}
func printInfoValue(logger *text.Logger, key string, values ...string) {
const (
keyLength = 32
delimCount = 2
)
specialWordsCount := 0
for _, runeValue := range key {
// CJK handling: the character 'ー' is Katakana
// but if use unicode.Katakana, it will return false
if unicode.IsOneOf([]*unicode.RangeTable{
unicode.Han,
unicode.Hiragana,
unicode.Katakana,
unicode.Hangul,
}, runeValue) || runeValue == 'ー' {
specialWordsCount++
}
}
keyTextCount := specialWordsCount - keyLength + delimCount
str := fmt.Sprintf(text.Bold("%-*s: "), keyTextCount, key)
if len(values) == 0 || (len(values) == 1 && values[0] == "") {
logger.Printf("%s%s\n", str, gotext.Get("None"))
return
}
maxCols := getColumnCount()
cols := keyLength + len(values[0])
str += values[0]
for _, value := range values[1:] {
if maxCols > keyLength && cols+len(value)+delimCount >= maxCols {
cols = keyLength
str += "\n" + strings.Repeat(" ", keyLength)
} else if cols != keyLength {
str += strings.Repeat(" ", delimCount)
cols += delimCount
}
str += value
cols += len(value)
}
logger.Println(str)
}
var cachedColumnCount = -1
func getColumnCount() int {
if cachedColumnCount > 0 {
return cachedColumnCount
}
if count, err := strconv.Atoi(os.Getenv("COLUMNS")); err == nil {
cachedColumnCount = count
return cachedColumnCount
}
if ws, err := unix.IoctlGetWinsize(syscall.Stdout, unix.TIOCGWINSZ); err == nil {
cachedColumnCount = int(ws.Col)
return cachedColumnCount
}
return 80
}