Generalize to read filter from file or stdin.

This commit is contained in:
Christof Warlich authored and Gordian Schoenherr committed 2024-12-13 11:24:54 +09:00
1 parent a5d322252a
commit 005114839a
5 files changed
+55 -42

No files matched your search

+35
View File
@@ -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
}