deb-simple/apt.go

125 lines
2.3 KiB
Go

package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/spf13/afero"
"golang.org/x/crypto/openpgp"
"io"
"meow.tf/deb-simple/deb/release"
"path"
"strings"
"time"
)
func createRelease(config Conf, distro string) error {
outfile, err := fs.Create(path.Join(config.DistPath(distro), "Release"))
if err != nil {
return fmt.Errorf("failed to create Release: %s", err)
}
defer outfile.Close()
r := &release.Release{
Suite: distro,
Architectures: config.Repo.ArchitectureNames(),
Components: []string{"main"},
Date: time.Now(),
}
for _, arch := range config.Repo.ArchitectureNames() {
absolutePath := path.Join(config.DistPath(distro), "main", "binary-" + arch)
list, err := afero.ReadDir(fs, absolutePath)
if err != nil {
continue
}
basePath := path.Join("main", "binary-" + arch)
for _, file := range list {
filePath := path.Join(absolutePath, file.Name())
fileLocalPath := path.Join(basePath, file.Name())
fileLocalPath = strings.Replace(fileLocalPath, "\\", "/", -1)
f, err := fs.Open(filePath)
if err != nil {
return err
}
var size = file.Size()
if size == 0 {
if stat, err := fs.Stat(filePath); err == nil {
size = stat.Size()
}
}
var (
md5hash = md5.New()
sha1hash = sha1.New()
sha256hash = sha256.New()
)
w := io.MultiWriter(md5hash, sha1hash, sha256hash)
io.Copy(w, f)
repoFile := &release.File{
Name: fileLocalPath,
Size: size,
MD5Sum: hex.EncodeToString(md5hash.Sum(nil)),
SHA1: hex.EncodeToString(sha1hash.Sum(nil)),
SHA256: hex.EncodeToString(sha256hash.Sum(nil)),
}
r.Append(repoFile)
f.Close()
f = nil
}
}
r.Write(outfile)
if pgpEntity != nil {
return signRelease(config, distro)
}
return nil
}
func signRelease(config Conf, distro string) error {
distPath := config.DistPath(distro)
f, err := fs.Open(path.Join(distPath, "Release"))
if err != nil {
return fmt.Errorf("failed to read Release: %s", err)
}
defer f.Close()
gpgfile, err := fs.Create(path.Join(distPath, "Release.gpg"))
if err != nil {
return fmt.Errorf("failed to create Release.gpg: %s", err)
}
defer gpgfile.Close()
if err := openpgp.ArmoredDetachSignText(gpgfile, pgpEntity, f, nil); err != nil {
return fmt.Errorf("failed to sign Release: %s", err)
}
return nil
}