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

plugins: add support for plugin configs #6613

Merged
merged 1 commit into from
Aug 31, 2019
Merged

Conversation

Stebalien
Copy link
Member

For now, configs specified in daemon --init-config and init CONFIG are not available. We should fix this eventually but isn't necessary for now (and supporting this will be annoying).

@github-actions github-actions bot requested a review from hsanjuan August 29, 2019 21:02
@Stebalien Stebalien requested review from djdv and removed request for hsanjuan August 29, 2019 21:05
pluginpath := filepath.Join(repoPath, "plugins")

plugins, err := loader.NewPluginLoader()
plugins, err := loader.NewPluginLoader(repoPath)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed the repo path so I figured I might as well move a bunch of the plugin loading logic into the loader.

if err != nil {
return nil, fmt.Errorf("error loading preloaded plugins: %s", err)
}

// check if repo is accessible before loading plugins
ok, err := checkPermissions(repoPath)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was kind of useless so I didn't bother replicating it. Instead of checking permissions, we just do the thing and see if it fails.

return nil, err
}
if ok {
if err := plugins.LoadDirectory(pluginpath); err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Called in NewPluginLoader

@@ -57,6 +57,12 @@ func (c *Context) GetNode() (*core.IpfsNode, error) {
return nil, errors.New("nil ConstructNode function")
}
c.node, err = c.ConstructNode()
if err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this change doesn't have anything to do with this PR. I just noticed that we were parsing the config twice and figured we could just do it once.

For now, configs specified in `daemon --init-config` and `init CONFIG` are not
available. We should fix this eventually but isn't necessary for now (and
supporting this will be annoying).
@Stebalien
Copy link
Member Author

@djdv CI is passing. This should be ready for review.

}

// NewPluginLoader creates new plugin loader
func NewPluginLoader() (*PluginLoader, error) {
loader := &PluginLoader{plugins: make(map[string]plugin.Plugin, len(preloadPlugins))}
func NewPluginLoader(repo string) (*PluginLoader, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo a Plugin receives from the Environment should probably be normalized into absolute paths.
Not sure though, there may be a case for accepting relatives.

Abs patch 1/2

Suggested change
func NewPluginLoader(repo string) (*PluginLoader, error) {
func NewPluginLoader(repo string) (*PluginLoader, error) {
var err error
if repo, err = filepath.Abs(repo); err != nil {
return nil, err
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we don't do this anywhere else, I'd like to leave this as it is for consistency (that way the repo path is the same everywhere). I believe it'll usually be an absolute path anyways.

If we do run into issues, we should be consistent and make this path absolute at a higher layer.

for _, v := range preloadPlugins {
if err := loader.Load(v); err != nil {
return nil, err
}
}

if err := loader.LoadDirectory(filepath.Join(repo, "plugins")); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abs patch 2/2

Suggested change
if err := loader.LoadDirectory(filepath.Join(repo, "plugins")); err != nil {
if err = loader.LoadDirectory(filepath.Join(repo, "plugins")); err != nil {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I don't like using = in assignments inside if statements. The point of using an if statement like this is to scope the err variable to the if statement. Using a normal assignment lets it escape the scope and can be really confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, this should either be left as-is or should be:

err = loader.LoadDirectory(...)
if err != nil {
  // ...
}

Copy link
Contributor

@djdv djdv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me after reading.
Also tested it against my own plugin here: djdv@fefafe7#diff-8d243ab8514fa1b77d41df37a81641c9 with no obvious issues. (Doesn't run when disabled, config values work as expected)

Should simplifying plugin logic a little for plugin developers.

@Stebalien Stebalien merged commit d5977fc into master Aug 31, 2019
@Stebalien Stebalien deleted the feat/plugin-config branch September 6, 2019 05:32
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.

2 participants