Allow reading package query for -filter option from a file.

This commit is contained in:
Christof Warlich
2024-08-21 14:03:22 +02:00
committed by Gordian Schoenherr
parent 93650efddb
commit a5d322252a

View File

@@ -2,6 +2,10 @@ package cmd
import (
"fmt"
"bufio"
"io/ioutil"
"os"
"strings"
"github.com/aptly-dev/aptly/pgp"
"github.com/aptly-dev/aptly/query"
@@ -9,6 +13,37 @@ import (
"github.com/smira/flag"
)
func getContent(filterarg string) (string, error) {
var err error
// Check if filterarg starts with '@'
if strings.HasPrefix(filterarg, "@") {
// Remove the '@' character from filterarg
filterarg = strings.TrimPrefix(filterarg, "@")
if filterarg == "-" {
// If filterarg is "-", read from stdin
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
var content strings.Builder
for scanner.Scan() {
content.WriteString(scanner.Text() + "\n")
}
err = scanner.Err()
if err == nil {
filterarg = content.String()
}
} else {
// Read the file content into a byte slice
var data []byte
data, err = ioutil.ReadFile(filterarg)
if err == nil {
filterarg = string(data)
}
}
}
return filterarg, err
}
func aptlyMirrorEdit(cmd *commander.Command, args []string) error {
var err error
if len(args) != 1 {
@@ -28,11 +63,13 @@ func aptlyMirrorEdit(cmd *commander.Command, args []string) error {
}
fetchMirror := false
filter := false
ignoreSignatures := context.Config().GpgDisableVerify
context.Flags().Visit(func(flag *flag.Flag) {
switch flag.Name {
case "filter":
repo.Filter = flag.Value.String()
repo.Filter, err = getContent(flag.Value.String())
filter = true
case "filter-with-deps":
repo.FilterWithDeps = flag.Value.Get().(bool)
case "with-installer":
@@ -49,6 +86,10 @@ func aptlyMirrorEdit(cmd *commander.Command, args []string) error {
}
})
if filter && err != nil {
return fmt.Errorf("unable to read package query from file %s: %w", repo.Filter, err)
}
if repo.IsFlat() && repo.DownloadUdebs {
return fmt.Errorf("unable to edit: flat mirrors don't support udebs")
}