This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform_inrelease_file.go
78 lines (68 loc) · 2.37 KB
/
transform_inrelease_file.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
package utils
import (
"github.com/be-ys-cloud/zest/internal/utils/configuration"
"github.com/be-ys-cloud/zest/internal/utils/gpg"
cp "github.com/otiai10/copy"
"github.com/sirupsen/logrus"
"io"
"os"
"os/exec"
)
func TransformInReleaseFile(folderName string, fileFullPath string, body []byte) (bodyReturn []byte, err error) {
//Copy file
releaseFile := configuration.DataStorage + folderName + "/Release"
err = os.MkdirAll(configuration.DataStorage+folderName, 0777)
if err != nil {
logrus.Warnln("Could not create directory " + configuration.DataStorage + folderName)
return
}
err = os.WriteFile(releaseFile, body, 0777)
if err != nil {
logrus.Warnln("Could not create file " + releaseFile)
return
}
//Remove armor around file content
err = exec.Command("sed", "-i", "-e", "1,3d", releaseFile).Run()
if err != nil {
logrus.Warnln("Could not create sed in file " + releaseFile)
return
}
err = exec.Command("bash", "-c", "truncate -s $(grep -b '^-----BEGIN PGP SIGNATURE-----' "+releaseFile+" | grep -o -E '^[0-9]*') "+releaseFile).Run()
if err != nil {
logrus.Warnln("Could not grep in file " + releaseFile)
return
}
//Change Signed-By field
err = ReplaceValidUntilAndSignedBy(releaseFile)
if err != nil {
logrus.Warnln("Could not change Signed-By field. Program will continue, but some clients may reject your file.")
}
//Re-create Release.gpg and InRelease file
err = gpg.DetachedSign(configuration.KeyFile, configuration.KeyPass, releaseFile, releaseFile+".gpg")
if err != nil {
logrus.Warnln("Could not create detached sign for file " + releaseFile)
return
}
err = cp.Copy(releaseFile, configuration.DataStorage+fileFullPath)
if err != nil {
logrus.Warnln("Could not copy file " + releaseFile + " to " + configuration.DataStorage + fileFullPath)
return
}
err = gpg.InlineSign(configuration.KeyFile, configuration.KeyPass, configuration.DataStorage+fileFullPath)
if err != nil {
logrus.Warnln("Could not inline sign " + configuration.DataStorage + fileFullPath)
return
}
//Return InRelease content to body variable
f, err := os.Open(configuration.DataStorage + fileFullPath)
if err != nil {
logrus.Warnln("Could not open file " + configuration.DataStorage + fileFullPath)
return
}
bodyReturn, err = io.ReadAll(f)
if err != nil {
logrus.Warnln("Could not read file content" + configuration.DataStorage + fileFullPath)
return
}
return
}