diff --git a/cmd/cmd.go b/cmd/cmd.go index d5bdff25..27240a9b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -7,6 +7,9 @@ import ( "os" "text/template" "time" + "bufio" + "io/ioutil" + "strings" "github.com/aptly-dev/aptly/aptly" "github.com/aptly-dev/aptly/deb" @@ -129,3 +132,35 @@ package environment to new version.`, } return cmd } + +// Reads the content of a file. If the file is "-", reads from stdin. +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 +} diff --git a/cmd/mirror_create.go b/cmd/mirror_create.go index 022a5cf2..fba190be 100644 --- a/cmd/mirror_create.go +++ b/cmd/mirror_create.go @@ -46,12 +46,15 @@ func aptlyMirrorCreate(cmd *commander.Command, args []string) error { return fmt.Errorf("unable to create mirror: %s", err) } - repo.Filter = context.Flags().Lookup("filter").Value.String() + repo.Filter, err = getContent(context.Flags().Lookup("filter").Value.String()) repo.FilterWithDeps = context.Flags().Lookup("filter-with-deps").Value.Get().(bool) repo.SkipComponentCheck = context.Flags().Lookup("force-components").Value.Get().(bool) repo.SkipArchitectureCheck = context.Flags().Lookup("force-architectures").Value.Get().(bool) if repo.Filter != "" { + if err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", context.Flags().Lookup("filter").Value.String(), err) + } _, err = query.Parse(repo.Filter) if err != nil { return fmt.Errorf("unable to create mirror: %s", err) diff --git a/cmd/mirror_edit.go b/cmd/mirror_edit.go index 27c2cfbf..e3e1d5b3 100644 --- a/cmd/mirror_edit.go +++ b/cmd/mirror_edit.go @@ -2,10 +2,6 @@ package cmd import ( "fmt" - "bufio" - "io/ioutil" - "os" - "strings" "github.com/aptly-dev/aptly/pgp" "github.com/aptly-dev/aptly/query" @@ -13,37 +9,6 @@ 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 { @@ -63,13 +28,13 @@ func aptlyMirrorEdit(cmd *commander.Command, args []string) error { } fetchMirror := false - filter := false ignoreSignatures := context.Config().GpgDisableVerify + var f string context.Flags().Visit(func(flag *flag.Flag) { switch flag.Name { case "filter": repo.Filter, err = getContent(flag.Value.String()) - filter = true + f = flag.Value.String() case "filter-with-deps": repo.FilterWithDeps = flag.Value.Get().(bool) case "with-installer": @@ -86,8 +51,8 @@ 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.Filter != "" && err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", f, err) } if repo.IsFlat() && repo.DownloadUdebs { diff --git a/cmd/snapshot_filter.go b/cmd/snapshot_filter.go index c712ae67..095f8661 100644 --- a/cmd/snapshot_filter.go +++ b/cmd/snapshot_filter.go @@ -60,7 +60,12 @@ func aptlySnapshotFilter(cmd *commander.Command, args []string) error { // Initial queries out of arguments queries := make([]deb.PackageQuery, len(args)-2) for i, arg := range args[2:] { - queries[i], err = query.Parse(arg) + var q string + q, err = getContent(arg) + if err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", arg, err) + } + queries[i], err = query.Parse(q) if err != nil { return fmt.Errorf("unable to parse query: %s", err) } diff --git a/cmd/snapshot_pull.go b/cmd/snapshot_pull.go index 27593b77..3e9b99b5 100644 --- a/cmd/snapshot_pull.go +++ b/cmd/snapshot_pull.go @@ -88,7 +88,12 @@ func aptlySnapshotPull(cmd *commander.Command, args []string) error { // Initial queries out of arguments queries := make([]deb.PackageQuery, len(args)-3) for i, arg := range args[3:] { - queries[i], err = query.Parse(arg) + var q string + q, err = getContent(arg) + if err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", arg, err) + } + queries[i], err = query.Parse(q) if err != nil { return fmt.Errorf("unable to parse query: %s", err) }