-
Notifications
You must be signed in to change notification settings - Fork 77
wip: allow passing defs path #2073
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Simon de Vlieger <[email protected]>
Signed-off-by: Simon de Vlieger <[email protected]>
Signed-off-by: Simon de Vlieger <[email protected]>
Signed-off-by: Simon de Vlieger <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing I don't like about this is that a lot of functions now have an (only or first) argument that in many cases will be set to an empty string to get the default behaviour. I think that if the defs dir being set externally is the exception, then this should be reflected in the way the functions are called and the types are initialised. It should be simpler to get the common default behaviour than the exceptional one. So, instead of:
d := distrofactory.NewDefault("") // default casee := distrofactory.NewDefault(somevalue) // exceptional caseI find it's nicer to have
d := distrofactory.NewDefault() // default casee := distrofactory.NewDefault()
if defsPath != "" {
e.DefsPath(defsPath) // exceptional case
}
This is nicer, I'll rewrite it to have a |
I agree, but would like to offer two different approaches that may be nicer and more idiomatic in the long run. Functional options patternpackage distrofactory
type option func(*Factory)
func WithDefsPath(path string) option {
return func(c *Factory) {
// Do the loading from the custom path here.
}
}
func NewDefault(opts ...option) *Factory {
f := getDefault()
// In our case, the logic would be different, because we would probably construst a new Factory object from scratch from the different path.
for _, opt := range opts {
opt(f)
}
return f
}f := distrofactory.NewDefault() // default casef := distrofactory.NewDefault(distrofactory.WithDefsPath("/dev/null")) // exceptional caseIMHO, this approach is the most efficient, given how distro defs are loaded and how they work. This is also a standard when it comes to big libraries like cloud provider SDKs. Dysfunctional options pattern(inspired by https://rednafi.com/go/dysfunctional_options_pattern/#dysfunctional-options-pattern) package distrofactory
func NewDefault() *Factory {
f := getDefault()
return f
}
func (f *Factory) WithDefsPath(path string) *Factory {
f.doSomething(path)
return f
}f := distrofactory.NewDefault() // default casef := distrofactory.NewDefault().WithDefsPath("/dev/null") // exceptional caseNot ideal, because defs would be loaded twice AFAICT. |
I didn't inspect the code flow too deeply, but could we maybe not use |
This is nice. I like it! Maybe we should start doing this everywhere :)
I was about to say we can delay loading until it's necessary but I think loading only happens when images/pkg/distrofactory/distrofactory.go Lines 29 to 41 in dbc3543
|
This is mostly a request for comments as everything here is work-in-progress. I'd really like it if we can pass the definitions path directly from
image-builderwithout having to go through an environment variable but I don't quite like what I've done here.Does someone know a better way to do this?
An open problem is
ParseIDwhich directly loads definitions without going through the loader's state.