From a5d322252aab0a657fa93b1871343c3924dec737 Mon Sep 17 00:00:00 2001 From: Christof Warlich Date: Wed, 21 Aug 2024 14:03:22 +0200 Subject: [PATCH] Allow reading package query for -filter option from a file. --- cmd/mirror_edit.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/cmd/mirror_edit.go b/cmd/mirror_edit.go index 5adf504b..27c2cfbf 100644 --- a/cmd/mirror_edit.go +++ b/cmd/mirror_edit.go @@ -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") }