mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-01-12 03:21:33 +00:00
Generalize to read filter from file or stdin.
This commit is contained in:
committed by
Gordian Schoenherr
parent
a5d322252a
commit
005114839a
35
cmd/cmd.go
35
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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user