Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Suggestion: Support Reading Filter Expressions from a File #1409

Closed
schoenherrg opened this issue Dec 13, 2024 · 0 comments · Fixed by #1411
Closed

Feature Suggestion: Support Reading Filter Expressions from a File #1409

schoenherrg opened this issue Dec 13, 2024 · 0 comments · Fixed by #1411
Assignees

Comments

@schoenherrg
Copy link
Contributor

We have an internal patch for Aptly to support reading filter expressions from a file or from stdin. For example:

# from file
$ echo libc6 > test.filter; aptly mirror create -filter="@test.filter" ...
# from stdin
$ echo "vim" | aptly mirror create -filter='@-' ...

If there is any chance of getting this feature merged, I would clean up our code a bit and make a PR.

What do you think?

Detailed Description

For us it's mainly important that this works in the mirror create and mirror edit subcommands' -filter argument.
However, there are also other places in Aptly where a package query can be provided, so there is the question of whether we should support it everywhere for consistency, e.g.

aptly snapshot pull <name> <source> <destination> <package-query> ...

supports multiple queries, so should we allow the @file syntax there (then you could theoretically specify multiple different files and even mix it with plain queries like @file1 vim @file2 ...)?

Also, this is technically a breaking change, though I doubt many users have existing filters that start with @ because that is not a valid character for a Debian package name and thus would not match any packages.

Context

The reason we have implemented this is that we have filter expressions that exceed the command line length limit.

Possible Implementation

Since the flag library used by Aptly (smira/flag) allows creating custom types of flags, I just made a StringOrFileFlag like this:

// 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) Set(value string) error {
	var err error
	s.value, err = GetStringOrFileContent(value)
	return err
}

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
}

// ...

This way, the file/error handling logic is in one place and you can just use it like this without having to change any other code in other places:

- 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")

Your Environment

N/A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants