2022-06-17 18:55:22 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2023-05-24 10:22:18 +02:00
|
|
|
"github.com/Jguer/aur"
|
2022-06-17 18:55:22 +02:00
|
|
|
"github.com/Jguer/votar/pkg/vote"
|
|
|
|
"github.com/leonelquinteros/gotext"
|
2023-08-06 21:39:41 +02:00
|
|
|
|
|
|
|
"github.com/Jguer/yay/v12/pkg/text"
|
2022-06-17 18:55:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2023-08-06 21:39:41 +02:00
|
|
|
targets []string, aurClient aur.QueryClient, logger *text.Logger,
|
2023-05-24 10:22:18 +02:00
|
|
|
voteClient *vote.Client, upvote bool,
|
2022-06-17 18:55:22 +02:00
|
|
|
) error {
|
2023-05-24 10:22:18 +02:00
|
|
|
infos, err := aurClient.Get(ctx, &aur.Query{
|
|
|
|
Needles: targets,
|
|
|
|
By: aur.Name,
|
|
|
|
})
|
2022-06-17 18:55:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(infos) == 0 {
|
2023-08-06 21:39:41 +02:00
|
|
|
logger.Println(gotext.Get(" there is nothing to do"))
|
2022-06-17 18:55:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:43 +01:00
|
|
|
for i := range infos {
|
2022-06-17 18:55:22 +02:00
|
|
|
var err error
|
|
|
|
if upvote {
|
2022-12-29 19:42:43 +01:00
|
|
|
err = voteClient.Vote(ctx, infos[i].PackageBase)
|
2022-06-17 18:55:22 +02:00
|
|
|
} else {
|
2022-12-29 19:42:43 +01:00
|
|
|
err = voteClient.Unvote(ctx, infos[i].PackageBase)
|
2022-06-17 18:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()))
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:42:43 +01:00
|
|
|
return &ErrAURVote{inner: err, pkgName: infos[i].Name}
|
2022-06-17 18:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|