Skip to content

Conversation

@thaJeztah
Copy link

The SetAnnotation method returns an error if the given flag does not exist. While handling the error should normally be the recommendation, in many cases the list of flags is static, and adding error handling for each annotation can either introduce unwanted boilerplating, or may not be possible in cases where there's no error return.

Unfortunately, also makes this method prone to copy/paste mistakes where either the flag doesn't exist, or an annotation is already set, potentially overwriting another flag's annotation (which is a bug I recently discovered in one of our projects (see 1)). Such bugs can be hard to discover if they have no immediate impact.

This patch introduces a MustSetAnnotation that can be used as a replacement for SetAnnotation. Unlike SetAnnotation, it produces a panic instead of returning an error, and uses stricter validation; not allowing an annotation to be set if the flag already has the annotation set.

The SetAnnotation method returns an error if the given flag does not exist.
While handling the error should normally be the recommendation, in many cases
the list of flags is static, and adding error handling for each annotation can
either introduce unwanted boilerplating, or may not be possible in cases where
there's no error return.

Unfortunately, also makes this method prone to copy/paste mistakes where either
the flag doesn't exist, or an annotation is already set, potentially overwriting
another flag's annotation (which is a bug I recently discovered in one of our
projects (see [1])). Such bugs can be hard to discover if they have no immediate
impact.

This patch introduces a `MustSetAnnotation` that can be used as a replacement
for `SetAnnotation`. Unlike `SetAnnotation`, it produces a panic instead of
returning an error, and uses stricter validation; not allowing an annotation
to be set if the flag already has the annotation set.

[1]: https://github.com/docker/cli/blob/93fa57bbcd08f2f5be7f6cf22f4273a2b5a49e71/cli/command/service/opts.go#L905-L910

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the add_MustSetAnnotation branch from 06e04a2 to 918e819 Compare December 22, 2025 11:27
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 this pull request may close these issues.

1 participant