-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
vote.go
60 lines (49 loc) · 1.2 KB
/
vote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
}