-
Notifications
You must be signed in to change notification settings - Fork 3
/
package.go
94 lines (82 loc) · 2.91 KB
/
package.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"context"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqljson"
"fmt"
log "github.com/sirupsen/logrus"
"os/exec"
"path/filepath"
"somegit.dev/ALHP/ALHP.GO/ent"
"somegit.dev/ALHP/ALHP.GO/ent/dbpackage"
"strings"
)
type Package string
// Name returns package's name
func (pkg Package) Name() string {
fNameSplit := strings.Split(filepath.Base(string(pkg)), "-")
return strings.Join(fNameSplit[:len(fNameSplit)-3], "-")
}
// MArch returns package's march
func (pkg Package) MArch() *string {
splitPath := strings.Split(string(pkg), string(filepath.Separator))
res := strings.Join(strings.Split(splitPath[len(splitPath)-4], "-")[1:], "-")
return &res
}
// Repo returns package's dbpackage.Repository
func (pkg Package) Repo() dbpackage.Repository {
splitPath := strings.Split(string(pkg), string(filepath.Separator))
return dbpackage.Repository(strings.Split(splitPath[len(splitPath)-4], "-")[0])
}
// FullRepo returns package's dbpackage.Repository-march
func (pkg Package) FullRepo() *string {
splitPath := strings.Split(string(pkg), string(filepath.Separator))
return &splitPath[len(splitPath)-4]
}
// Version returns version extracted from package
func (pkg Package) Version() string {
fNameSplit := strings.Split(filepath.Base(string(pkg)), "-")
return strings.Join(fNameSplit[len(fNameSplit)-3:len(fNameSplit)-1], "-")
}
// Arch returns package's Architecture
func (pkg Package) Arch() *string {
fNameSplit := strings.Split(filepath.Base(string(pkg)), "-")
fNameSplit = strings.Split(fNameSplit[len(fNameSplit)-1], ".")
return &fNameSplit[0]
}
// HasValidSignature returns if package has valid detached signature file
func (pkg Package) HasValidSignature() (bool, error) {
cmd := exec.Command("gpg", "--verify", string(pkg)+".sig") //nolint:gosec
res, err := cmd.CombinedOutput()
switch {
case cmd.ProcessState.ExitCode() == 2 || cmd.ProcessState.ExitCode() == 1:
return false, nil
case cmd.ProcessState.ExitCode() == 0:
return true, nil
case err != nil:
return false, fmt.Errorf("error checking signature: %w (%s)", err, res)
}
return false, nil
}
// DBPackage returns ent.DBPackage for package
func (pkg Package) DBPackage(db *ent.Client) (*ent.DBPackage, error) {
return pkg.DBPackageIsolated(*pkg.MArch(), pkg.Repo(), db)
}
// DBPackageIsolated returns ent.DBPackage like DBPackage, but not relying on the path for march and repo
func (pkg Package) DBPackageIsolated(march string, repo dbpackage.Repository, db *ent.Client) (*ent.DBPackage, error) {
dbPkg, err := db.DBPackage.Query().Where(func(s *sql.Selector) {
s.Where(
sql.And(
sqljson.ValueContains(dbpackage.FieldPackages, pkg.Name()),
sql.EQ(dbpackage.FieldMarch, march),
sql.EQ(dbpackage.FieldRepository, repo)),
)
}).Only(context.Background())
if ent.IsNotFound(err) {
log.Debugf("not found in database: %s", pkg.Name())
return nil, err
} else if err != nil {
return nil, err
}
return dbPkg, nil
}