yay/vote.go
Jo 8916cd174b
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
2023-08-06 21:39:41 +02:00

61 lines
1.2 KiB
Go

package main
import (
"context"
"errors"
"github.com/Jguer/aur"
"github.com/Jguer/votar/pkg/vote"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v12/pkg/text"
)
type ErrAURVote struct {
inner error
pkgName string
}
func (e *ErrAURVote) Error() string {
return gotext.Get("Unable to handle package vote for: %s. err: %s", e.pkgName, e.inner.Error())
}
func handlePackageVote(ctx context.Context,
targets []string, aurClient aur.QueryClient, logger *text.Logger,
voteClient *vote.Client, upvote bool,
) error {
infos, err := aurClient.Get(ctx, &aur.Query{
Needles: targets,
By: aur.Name,
})
if err != nil {
return err
}
if len(infos) == 0 {
logger.Println(gotext.Get(" there is nothing to do"))
return nil
}
for i := range infos {
var err error
if upvote {
err = voteClient.Vote(ctx, infos[i].PackageBase)
} else {
err = voteClient.Unvote(ctx, infos[i].PackageBase)
}
if err != nil {
if errors.Is(err, vote.ErrNoCredentials) {
return errors.New(
gotext.Get("%s: please set AUR_USERNAME and AUR_PASSWORD environment variables for voting",
err.Error()))
}
return &ErrAURVote{inner: err, pkgName: infos[i].Name}
}
}
return nil
}