Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package dpkg
|
|
|
|
import (
|
|
"encoding"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Maintainer struct {
|
|
Name string `json:"name,omitempty"`
|
|
Contact string `json:"contact,omitempty"`
|
|
}
|
|
|
|
func (main Maintainer) String() string {
|
|
if main.Contact == "" {
|
|
return main.Name
|
|
}
|
|
return fmt.Sprintf("%s <%s>", main.Name, main.Contact)
|
|
}
|
|
|
|
type Package struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
Architecture Architecture `json:"arch"`
|
|
Maintainer Maintainer `json:"maintainer"`
|
|
Description [2]string `json:"description"`
|
|
OriginalMaintainer *Maintainer `json:"original_maintainer,omitempty"`
|
|
Extra map[string]any `json:"extra"`
|
|
}
|
|
|
|
func (pkg Package) MarshalBinary() ([]byte, error) {
|
|
lines := []string{
|
|
fmt.Sprintf("Package: %s", pkg.Name),
|
|
fmt.Sprintf("Version: %s", pkg.Version),
|
|
fmt.Sprintf("Architecture: %s", pkg.Architecture.String()),
|
|
fmt.Sprintf("Maintainer: %s", pkg.Maintainer.String()),
|
|
}
|
|
if pkg.OriginalMaintainer != nil {
|
|
lines = append(lines, fmt.Sprintf("Original-Maintainer: %s", pkg.OriginalMaintainer.String()))
|
|
}
|
|
|
|
for key, value := range pkg.Extra {
|
|
switch v := value.(type) {
|
|
case fmt.Stringer:
|
|
lines = append(lines, fmt.Sprintf("%s: %s", key, v.String()))
|
|
case encoding.TextMarshaler:
|
|
data, err := v.MarshalText()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
lines = append(lines, fmt.Sprintf("%s: %s", key, string(data)))
|
|
case string:
|
|
lines = append(lines, fmt.Sprintf("%s: %s", key, v))
|
|
case []string:
|
|
lines = append(lines, fmt.Sprintf("%s: %s", key, strings.Join(v, ", ")))
|
|
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
|
lines = append(lines, fmt.Sprintf("%s: %d", key, v))
|
|
case bool:
|
|
str := "no"
|
|
if v {
|
|
str = "yes"
|
|
}
|
|
lines = append(lines, fmt.Sprintf("%s: %s", key, str))
|
|
}
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("Description: %s", pkg.Description[0]))
|
|
if pkg.Description[1] != "" {
|
|
descLines := strings.Split(strings.TrimSpace(pkg.Description[1]), "\n")
|
|
for index := range descLines {
|
|
if strings.TrimSpace(descLines[index]) == "" {
|
|
descLines[index] = "."
|
|
continue
|
|
}
|
|
}
|
|
lines = append(lines, fmt.Sprintf(" %s", strings.Join(descLines, "\n ")))
|
|
}
|
|
|
|
return []byte(strings.Join(append(lines, ""), "\n")), nil
|
|
}
|