From a5d322252aab0a657fa93b1871343c3924dec737 Mon Sep 17 00:00:00 2001 From: Christof Warlich Date: Wed, 21 Aug 2024 14:03:22 +0200 Subject: [PATCH 1/7] 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") } From 005114839ae0f9bedddd394f3d8246feea26e79c Mon Sep 17 00:00:00 2001 From: Christof Warlich Date: Wed, 25 Sep 2024 08:42:02 +0000 Subject: [PATCH 2/7] Generalize to read filter from file or stdin. --- cmd/cmd.go | 35 ++++++++++++++++++++++++++++++++++ cmd/mirror_create.go | 5 ++++- cmd/mirror_edit.go | 43 ++++-------------------------------------- cmd/snapshot_filter.go | 7 ++++++- cmd/snapshot_pull.go | 7 ++++++- 5 files changed, 55 insertions(+), 42 deletions(-) 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) } From 9691b0f518e2fb136a8324c7f6052432fb0d52ad Mon Sep 17 00:00:00 2001 From: Gordian Schoenherr Date: Fri, 13 Dec 2024 12:48:38 +0900 Subject: [PATCH 3/7] Refactor query reading from file, update docs Add support for @file syntax in more places. --- cmd/cmd.go | 35 ------------------------ cmd/mirror_create.go | 4 +-- cmd/mirror_edit.go | 10 ++----- cmd/package_search.go | 7 ++++- cmd/package_show.go | 8 +++++- cmd/repo_move.go | 8 +++++- cmd/repo_remove.go | 8 +++++- cmd/snapshot_filter.go | 7 ++--- cmd/snapshot_pull.go | 7 ++--- cmd/snapshot_search.go | 8 +++++- cmd/string_or_file_flag.go | 55 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 101 insertions(+), 56 deletions(-) create mode 100644 cmd/string_or_file_flag.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 27240a9b..d5bdff25 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -7,9 +7,6 @@ import ( "os" "text/template" "time" - "bufio" - "io/ioutil" - "strings" "github.com/aptly-dev/aptly/aptly" "github.com/aptly-dev/aptly/deb" @@ -132,35 +129,3 @@ 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 fba190be..9c4dbeee 100644 --- a/cmd/mirror_create.go +++ b/cmd/mirror_create.go @@ -46,7 +46,7 @@ func aptlyMirrorCreate(cmd *commander.Command, args []string) error { return fmt.Errorf("unable to create mirror: %s", err) } - repo.Filter, err = getContent(context.Flags().Lookup("filter").Value.String()) + repo.Filter = 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) @@ -106,7 +106,7 @@ Example: cmd.Flag.Bool("with-installer", false, "download additional not packaged installer files") cmd.Flag.Bool("with-sources", false, "download source packages in addition to binary packages") cmd.Flag.Bool("with-udebs", false, "download .udeb packages (Debian installer support)") - cmd.Flag.String("filter", "", "filter packages in mirror") + AddStringOrFileFlag(&cmd.Flag, "filter", "", "filter packages in mirror, use '@file' to read filter from file or '@-' for stdin") cmd.Flag.Bool("filter-with-deps", false, "when filtering, include dependencies of matching packages as well") cmd.Flag.Bool("force-components", false, "(only with component list) skip check that requested components are listed in Release file") cmd.Flag.Bool("force-architectures", false, "(only with architecture list) skip check that requested architectures are listed in Release file") diff --git a/cmd/mirror_edit.go b/cmd/mirror_edit.go index e3e1d5b3..84c4f0b5 100644 --- a/cmd/mirror_edit.go +++ b/cmd/mirror_edit.go @@ -29,12 +29,10 @@ func aptlyMirrorEdit(cmd *commander.Command, args []string) error { fetchMirror := 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()) - f = flag.Value.String() + repo.Filter = flag.Value.String() case "filter-with-deps": repo.FilterWithDeps = flag.Value.Get().(bool) case "with-installer": @@ -51,10 +49,6 @@ func aptlyMirrorEdit(cmd *commander.Command, args []string) error { } }) - if repo.Filter != "" && err != nil { - return fmt.Errorf("unable to read package query from file %s: %w", f, err) - } - if repo.IsFlat() && repo.DownloadUdebs { return fmt.Errorf("unable to edit: flat mirrors don't support udebs") } @@ -110,7 +104,7 @@ Example: } cmd.Flag.String("archive-url", "", "archive url is the root of archive") - cmd.Flag.String("filter", "", "filter packages in mirror") + AddStringOrFileFlag(&cmd.Flag, "filter", "", "filter packages in mirror, use '@file' to read filter from file or '@-' for stdin") cmd.Flag.Bool("filter-with-deps", false, "when filtering, include dependencies of matching packages as well") cmd.Flag.Bool("ignore-signatures", false, "disable verification of Release file signatures") cmd.Flag.Bool("with-installer", false, "download additional not packaged installer files") diff --git a/cmd/package_search.go b/cmd/package_search.go index 2105a1f3..54290c21 100644 --- a/cmd/package_search.go +++ b/cmd/package_search.go @@ -21,7 +21,11 @@ func aptlyPackageSearch(cmd *commander.Command, args []string) error { } if len(args) == 1 { - q, err = query.Parse(args[0]) + value, err := GetStringOrFileContent(args[0]) + if err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", args[0], err) + } + q, err = query.Parse(value) if err != nil { return fmt.Errorf("unable to search: %s", err) } @@ -49,6 +53,7 @@ func makeCmdPackageSearch() *commander.Command { Long: ` Command search displays list of packages in whole DB that match package query. +Use '@file' to read query from file or '@-' for stdin. If query is not specified, all the packages are displayed. Example: diff --git a/cmd/package_show.go b/cmd/package_show.go index 37f07e9b..1715b52a 100644 --- a/cmd/package_show.go +++ b/cmd/package_show.go @@ -66,7 +66,11 @@ func aptlyPackageShow(cmd *commander.Command, args []string) error { return commander.ErrCommandError } - q, err := query.Parse(args[0]) + value, err := GetStringOrFileContent(args[0]) + if err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", args[0], err) + } + q, err := query.Parse(value) if err != nil { return fmt.Errorf("unable to show: %s", err) } @@ -130,6 +134,8 @@ matching query. Information from Debian control file is displayed. Optionally information about package files and inclusion into mirrors/snapshots/local repos is shown. +Use '@file' to read query from file or '@-' for stdin. + Example: $ aptly package show 'nginx-light_1.2.1-2.2+wheezy2_i386' diff --git a/cmd/repo_move.go b/cmd/repo_move.go index e53c3a7c..bd1447ce 100644 --- a/cmd/repo_move.go +++ b/cmd/repo_move.go @@ -110,7 +110,11 @@ func aptlyRepoMoveCopyImport(cmd *commander.Command, args []string) error { queries := make([]deb.PackageQuery, len(args)-2) for i := 0; i < len(args)-2; i++ { - queries[i], err = query.Parse(args[i+2]) + value, err := GetStringOrFileContent(args[i+2]) + if err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", args[i+2], err) + } + queries[i], err = query.Parse(value) if err != nil { return fmt.Errorf("unable to %s: %s", command, err) } @@ -186,6 +190,8 @@ func makeCmdRepoMove() *commander.Command { Command move moves packages matching from local repo to local repo . +Use '@file' to read package queries from file or '@-' for stdin. + Example: $ aptly repo move testing stable 'myapp (=0.1.12)' diff --git a/cmd/repo_remove.go b/cmd/repo_remove.go index 287a42d5..5341a4c3 100644 --- a/cmd/repo_remove.go +++ b/cmd/repo_remove.go @@ -38,7 +38,11 @@ func aptlyRepoRemove(cmd *commander.Command, args []string) error { queries := make([]deb.PackageQuery, len(args)-1) for i := 0; i < len(args)-1; i++ { - queries[i], err = query.Parse(args[i+1]) + value, err := GetStringOrFileContent(args[i+1]) + if err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", args[i+1], err) + } + queries[i], err = query.Parse(value) if err != nil { return fmt.Errorf("unable to remove: %s", err) } @@ -81,6 +85,8 @@ Commands removes packages matching from local repository snapshots, they can be removed completely (including files) by running 'aptly db cleanup'. +Use '@file' to read package queries from file or '@-' for stdin. + Example: $ aptly repo remove testing 'myapp (=0.1.12)' diff --git a/cmd/snapshot_filter.go b/cmd/snapshot_filter.go index 095f8661..d4e71dc4 100644 --- a/cmd/snapshot_filter.go +++ b/cmd/snapshot_filter.go @@ -60,12 +60,11 @@ 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:] { - var q string - q, err = getContent(arg) + value, err := GetStringOrFileContent(arg) if err != nil { return fmt.Errorf("unable to read package query from file %s: %w", arg, err) } - queries[i], err = query.Parse(q) + queries[i], err = query.Parse(value) if err != nil { return fmt.Errorf("unable to parse query: %s", err) } @@ -108,6 +107,8 @@ Command filter does filtering in snapshot , producing another snapshot . Packages could be specified simply as 'package-name' or as package queries. +Use '@file' syntax to read package queries from file and '@-' to read from stdin. + Example: $ aptly snapshot filter wheezy-main wheezy-required 'Priority (required)' diff --git a/cmd/snapshot_pull.go b/cmd/snapshot_pull.go index 3e9b99b5..f73afab7 100644 --- a/cmd/snapshot_pull.go +++ b/cmd/snapshot_pull.go @@ -88,12 +88,11 @@ 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:] { - var q string - q, err = getContent(arg) + value, err := GetStringOrFileContent(arg) if err != nil { return fmt.Errorf("unable to read package query from file %s: %w", arg, err) } - queries[i], err = query.Parse(q) + queries[i], err = query.Parse(value) if err != nil { return fmt.Errorf("unable to parse query: %s", err) } @@ -172,6 +171,8 @@ versions from following dependencies. New snapshot is created as a result of this process. Packages could be specified simply as 'package-name' or as package queries. +Use '@file' syntax to read package queries from file and '@-' to read from stdin. + Example: $ aptly snapshot pull wheezy-main wheezy-backports wheezy-new-xorg xorg-server-server diff --git a/cmd/snapshot_search.go b/cmd/snapshot_search.go index 7af78e1e..24da005d 100644 --- a/cmd/snapshot_search.go +++ b/cmd/snapshot_search.go @@ -78,7 +78,11 @@ func aptlySnapshotMirrorRepoSearch(cmd *commander.Command, args []string) error list.PrepareIndex() if len(args) == 2 { - q, err = query.Parse(args[1]) + value, err := GetStringOrFileContent(args[1]) + if err != nil { + return fmt.Errorf("unable to read package query from file %s: %w", args[1], err) + } + q, err = query.Parse(value) if err != nil { return fmt.Errorf("unable to search: %s", err) } @@ -134,6 +138,8 @@ Command search displays list of packages in snapshot that match package query If query is not specified, all the packages are displayed. +Use '@file' syntax to read package query from file and '@-' to read from stdin. + Example: $ aptly snapshot search wheezy-main '$Architecture (i386), Name (% *-dev)' diff --git a/cmd/string_or_file_flag.go b/cmd/string_or_file_flag.go new file mode 100644 index 00000000..aee234f3 --- /dev/null +++ b/cmd/string_or_file_flag.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "io" + "os" + "strings" + + "github.com/smira/flag" +) + +// StringOrFileFlag is a custom flag type that can handle both string input and file input. +// If the input starts with '@', it is treated as a filename and the contents are read from the file. +// If the input is '@-', the contents are read from stdin. +type StringOrFileFlag struct { + value string +} + +func (s *StringOrFileFlag) String() string { + return s.value +} + +func (s *StringOrFileFlag) Set(value string) error { + var err error + s.value, err = GetStringOrFileContent(value) + return err +} + +func (s *StringOrFileFlag) Get() any { + return s.value +} + +func AddStringOrFileFlag(flagSet *flag.FlagSet, name string, value string, usage string) *StringOrFileFlag { + result := &StringOrFileFlag{value: value} + flagSet.Var(result, name, usage) + return result +} + +func GetStringOrFileContent(value string) (string, error) { + if !strings.HasPrefix(value, "@") { + return value, nil + } + + filename := strings.TrimPrefix(value, "@") + var data []byte + var err error + if filename == "-" { // Read from stdin + data, err = io.ReadAll(os.Stdin) + } else { + data, err = os.ReadFile(filename) + } + if err != nil { + return "", err + } + return string(data), nil +} From 2467674fcad4ced8c6b188616ec8ad0a10304671 Mon Sep 17 00:00:00 2001 From: Gordian Schoenherr Date: Thu, 19 Dec 2024 16:05:21 +0900 Subject: [PATCH 4/7] Update system tests --- system/t03_help/MirrorCreateHelpTest_gold | 2 +- system/t03_help/MirrorCreateTest_gold | 2 +- system/t03_help/WrongFlagTest_gold | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/system/t03_help/MirrorCreateHelpTest_gold b/system/t03_help/MirrorCreateHelpTest_gold index 1816e6d3..c0cc3886 100644 --- a/system/t03_help/MirrorCreateHelpTest_gold +++ b/system/t03_help/MirrorCreateHelpTest_gold @@ -21,7 +21,7 @@ Options: -dep-follow-source: when processing dependencies, follow from binary to Source packages -dep-follow-suggests: when processing dependencies, follow Suggests -dep-verbose-resolve: when processing dependencies, print detailed logs - -filter="": filter packages in mirror + -filter=: filter packages in mirror, use '@file' to read filter from file or '@-' for stdin -filter-with-deps: when filtering, include dependencies of matching packages as well -force-architectures: (only with architecture list) skip check that requested architectures are listed in Release file -force-components: (only with component list) skip check that requested components are listed in Release file diff --git a/system/t03_help/MirrorCreateTest_gold b/system/t03_help/MirrorCreateTest_gold index 74502216..30bcfd86 100644 --- a/system/t03_help/MirrorCreateTest_gold +++ b/system/t03_help/MirrorCreateTest_gold @@ -12,7 +12,7 @@ Options: -dep-follow-source: when processing dependencies, follow from binary to Source packages -dep-follow-suggests: when processing dependencies, follow Suggests -dep-verbose-resolve: when processing dependencies, print detailed logs - -filter="": filter packages in mirror + -filter=: filter packages in mirror, use '@file' to read filter from file or '@-' for stdin -filter-with-deps: when filtering, include dependencies of matching packages as well -force-architectures: (only with architecture list) skip check that requested architectures are listed in Release file -force-components: (only with component list) skip check that requested components are listed in Release file diff --git a/system/t03_help/WrongFlagTest_gold b/system/t03_help/WrongFlagTest_gold index 4dc0cb19..fb928b1e 100644 --- a/system/t03_help/WrongFlagTest_gold +++ b/system/t03_help/WrongFlagTest_gold @@ -13,7 +13,7 @@ Options: -dep-follow-source: when processing dependencies, follow from binary to Source packages -dep-follow-suggests: when processing dependencies, follow Suggests -dep-verbose-resolve: when processing dependencies, print detailed logs - -filter="": filter packages in mirror + -filter=: filter packages in mirror, use '@file' to read filter from file or '@-' for stdin -filter-with-deps: when filtering, include dependencies of matching packages as well -force-architectures: (only with architecture list) skip check that requested architectures are listed in Release file -force-components: (only with component list) skip check that requested components are listed in Release file From 8830354027e5faa8ee80d16b466a4edcf30aa107 Mon Sep 17 00:00:00 2001 From: Gordian Schoenherr Date: Fri, 20 Dec 2024 09:39:20 +0900 Subject: [PATCH 5/7] Extend system tests for @file filter syntax --- system/t04_mirror/CreateMirror36Test_gold | 4 ++ .../t04_mirror/CreateMirror36Test_mirror_show | 22 ++++++++++ system/t04_mirror/CreateMirror37Test_gold | 4 ++ .../t04_mirror/CreateMirror37Test_mirror_show | 22 ++++++++++ system/t04_mirror/create.py | 43 +++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 system/t04_mirror/CreateMirror36Test_gold create mode 100644 system/t04_mirror/CreateMirror36Test_mirror_show create mode 100644 system/t04_mirror/CreateMirror37Test_gold create mode 100644 system/t04_mirror/CreateMirror37Test_mirror_show diff --git a/system/t04_mirror/CreateMirror36Test_gold b/system/t04_mirror/CreateMirror36Test_gold new file mode 100644 index 00000000..eb089d7e --- /dev/null +++ b/system/t04_mirror/CreateMirror36Test_gold @@ -0,0 +1,4 @@ +Downloading: http://repo.aptly.info/system-tests/archive.debian.org/debian-security/dists/stretch/updates/Release + +Mirror [mirror36]: http://repo.aptly.info/system-tests/archive.debian.org/debian-security/ stretch/updates successfully added. +You can run 'aptly mirror update mirror36' to download repository contents. diff --git a/system/t04_mirror/CreateMirror36Test_mirror_show b/system/t04_mirror/CreateMirror36Test_mirror_show new file mode 100644 index 00000000..c6188417 --- /dev/null +++ b/system/t04_mirror/CreateMirror36Test_mirror_show @@ -0,0 +1,22 @@ +Name: mirror36 +Archive Root URL: http://repo.aptly.info/system-tests/archive.debian.org/debian-security/ +Distribution: stretch/updates +Components: main +Architectures: amd64, arm64, armel, armhf, i386 +Download Sources: no +Download .udebs: no +Filter: nginx | Priority (required) +Filter With Deps: no +Last update: never + +Information from release file: +Acquire-By-Hash: yes +Architectures: amd64 arm64 armel armhf i386 +Codename: stretch +Components: updates/main updates/contrib updates/non-free +Description: Debian 9 Security Updates + +Label: Debian-Security +Origin: Debian +Suite: oldoldstable +Version: 9 diff --git a/system/t04_mirror/CreateMirror37Test_gold b/system/t04_mirror/CreateMirror37Test_gold new file mode 100644 index 00000000..ef68aabc --- /dev/null +++ b/system/t04_mirror/CreateMirror37Test_gold @@ -0,0 +1,4 @@ +Downloading: http://repo.aptly.info/system-tests/archive.debian.org/debian-security/dists/stretch/updates/Release + +Mirror [mirror37]: http://repo.aptly.info/system-tests/archive.debian.org/debian-security/ stretch/updates successfully added. +You can run 'aptly mirror update mirror37' to download repository contents. diff --git a/system/t04_mirror/CreateMirror37Test_mirror_show b/system/t04_mirror/CreateMirror37Test_mirror_show new file mode 100644 index 00000000..aa2750a2 --- /dev/null +++ b/system/t04_mirror/CreateMirror37Test_mirror_show @@ -0,0 +1,22 @@ +Name: mirror37 +Archive Root URL: http://repo.aptly.info/system-tests/archive.debian.org/debian-security/ +Distribution: stretch/updates +Components: main +Architectures: amd64, arm64, armel, armhf, i386 +Download Sources: no +Download .udebs: no +Filter: nginx | Priority (required) +Filter With Deps: no +Last update: never + +Information from release file: +Acquire-By-Hash: yes +Architectures: amd64 arm64 armel armhf i386 +Codename: stretch +Components: updates/main updates/contrib updates/non-free +Description: Debian 9 Security Updates + +Label: Debian-Security +Origin: Debian +Suite: oldoldstable +Version: 9 diff --git a/system/t04_mirror/create.py b/system/t04_mirror/create.py index ecf3b6d4..a2cdbf74 100644 --- a/system/t04_mirror/create.py +++ b/system/t04_mirror/create.py @@ -1,3 +1,4 @@ +from pathlib import Path import re import os @@ -489,3 +490,45 @@ class CreateMirror35Test(BaseTest): def check(self): self.check_output() self.check_cmd_output("aptly mirror show mirror35", "mirror_show") + + +class CreateMirror36Test(BaseTest): + """ + create mirror: mirror with filter read from file + """ + filterFilePath = os.path.join(os.environ["HOME"], ".aptly-filter.tmp") + fixtureCmds = [f"bash -c \"echo -n 'nginx | Priority (required)' > {filterFilePath}\""] + runCmd = f"aptly mirror create -ignore-signatures -filter='@{filterFilePath}' mirror36 http://repo.aptly.info/system-tests/archive.debian.org/debian-security/ stretch/updates main" + + def check(self): + def removeDates(s): + return re.sub(r"(Date|Valid-Until): [,0-9:+A-Za-z -]+\n", "", s) + + self.check_output() + self.check_cmd_output("aptly mirror show mirror36", + "mirror_show", match_prepare=removeDates) + + +class CreateMirror37Test(BaseTest): + """ + create mirror: mirror with filter read from stdin + """ + aptly_testing_bin = Path(__file__).parent.parent.parent / "aptly.test" + # Hack: Normally the test system detects if runCmd is an aptly command and then + # substitutes the aptly_testing_bin path and deletes the last three lines of output. + # However, I need to run it in bash to control stdin, so I have to do it manually. + runCmd = [ + "bash", + "-c", + f"echo -n 'nginx | Priority (required)' | {aptly_testing_bin} mirror create " + + "-ignore-signatures -filter='@-' mirror37 http://repo.aptly.info/system-tests/archive.debian.org/debian-security/ stretch/updates main " + + "| grep -vE '^(EXIT|PASS|coverage:)'" + ] + + def check(self): + def removeDates(s): + return re.sub(r"(Date|Valid-Until): [,0-9:+A-Za-z -]+\n", "", s) + + self.check_output() + self.check_cmd_output("aptly mirror show mirror37", + "mirror_show", match_prepare=removeDates) From 50d36768474cce56117f9ee18257d55ad8adbc86 Mon Sep 17 00:00:00 2001 From: Gordian Schoenherr Date: Fri, 20 Dec 2024 12:55:10 +0900 Subject: [PATCH 6/7] Update man page --- man/aptly.1 | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/man/aptly.1 b/man/aptly.1 index 523440f8..5a323fb3 100644 --- a/man/aptly.1 +++ b/man/aptly.1 @@ -588,7 +588,7 @@ Options: . .TP \-\fBfilter\fR= -filter packages in mirror +filter packages in mirror, use \(cq@file\(cq to read filter from file or \(cq@\-\(cq for stdin . .TP \-\fBfilter\-with\-deps\fR @@ -771,7 +771,7 @@ archive url is the root of archive . .TP \-\fBfilter\fR= -filter packages in mirror +filter packages in mirror, use \(cq@file\(cq to read filter from file or \(cq@\-\(cq for stdin . .TP \-\fBfilter\-with\-deps\fR @@ -1016,6 +1016,9 @@ display list in machine\-readable format Command move moves packages matching \fIpackage\-query\fR from local repo \fIsrc\-name\fR to local repo \fIdst\-name\fR\. . .P +Use \(cq@file\(cq to read package queries from file or \(cq@\-\(cq for stdin\. +. +.P Example: . .P @@ -1039,6 +1042,9 @@ follow dependencies when processing package\-spec Commands removes packages matching \fIpackage\-query\fR from local repository \fIname\fR\. If removed packages are not referenced by other repos or snapshots, they can be removed completely (including files) by running \(cqaptly db cleanup\(cq\. . .P +Use \(cq@file\(cq to read package queries from file or \(cq@\-\(cq for stdin\. +. +.P Example: . .P @@ -1263,6 +1269,9 @@ $ aptly snapshot verify wheezy\-main wheezy\-contrib wheezy\-non\-free Command pull pulls new packages along with its\(cq dependencies to snapshot \fIname\fR from snapshot \fIsource\fR\. Pull can upgrade package version in \fIname\fR with versions from \fIsource\fR following dependencies\. New snapshot \fIdestination\fR is created as a result of this process\. Packages could be specified simply as \(cqpackage\-name\(cq or as package queries\. . .P +Use \(cq@file\(cq syntax to read package queries from file and \(cq@\-\(cq to read from stdin\. +. +.P Example: . .IP "" 4 @@ -1398,6 +1407,9 @@ Command search displays list of packages in snapshot that match package query If query is not specified, all the packages are displayed\. . .P +Use \(cq@file\(cq syntax to read package query from file and \(cq@\-\(cq to read from stdin\. +. +.P Example: . .IP "" 4 @@ -1428,6 +1440,9 @@ include dependencies into search results Command filter does filtering in snapshot \fIsource\fR, producing another snapshot \fIdestination\fR\. Packages could be specified simply as \(cqpackage\-name\(cq or as package queries\. . .P +Use \(cq@file\(cq syntax to read package queries from file and \(cq@\-\(cq to read from stdin\. +. +.P Example: . .IP "" 4 @@ -2206,7 +2221,7 @@ don\(cqt sign Release files with GPG Command search displays list of packages in whole DB that match package query\. . .P -If query is not specified, all the packages are displayed\. +Use \(cq@file\(cq to read query from file or \(cq@\-\(cq for stdin\. If query is not specified, all the packages are displayed\. . .P Example: @@ -2235,6 +2250,9 @@ custom format for result printing Command shows displays detailed meta\-information about packages matching query\. Information from Debian control file is displayed\. Optionally information about package files and inclusion into mirrors/snapshots/local repos is shown\. . .P +Use \(cq@file\(cq to read query from file or \(cq@\-\(cq for stdin\. +. +.P Example: . .IP "" 4 @@ -2653,5 +2671,8 @@ Blake Kostner (https://github\.com/btkostner) .IP "\[ci]" 4 Leigh London (https://github\.com/leighlondon) . +.IP "\[ci]" 4 +Gordian Schönherr (https://github\.com/schoenherrg) +. .IP "" 0 From c6bb5f76f77ef63e471f762b8c855d5d0ca7e993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Roth?= Date: Sat, 21 Dec 2024 11:37:15 +0100 Subject: [PATCH 7/7] cmd filter: add comment and cleanup --- cmd/mirror_create.go | 5 +---- cmd/mirror_edit.go | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/cmd/mirror_create.go b/cmd/mirror_create.go index 9c4dbeee..049dd2eb 100644 --- a/cmd/mirror_create.go +++ b/cmd/mirror_create.go @@ -46,15 +46,12 @@ 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 = context.Flags().Lookup("filter").Value.String() // allows file/stdin with @ 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 84c4f0b5..f95d9736 100644 --- a/cmd/mirror_edit.go +++ b/cmd/mirror_edit.go @@ -32,7 +32,7 @@ func aptlyMirrorEdit(cmd *commander.Command, args []string) error { context.Flags().Visit(func(flag *flag.Flag) { switch flag.Name { case "filter": - repo.Filter = flag.Value.String() + repo.Filter = flag.Value.String() // allows file/stdin with @ case "filter-with-deps": repo.FilterWithDeps = flag.Value.Get().(bool) case "with-installer":