plugin.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package plugin
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/client"
  7. )
  8. // InstallGrantAllPermissions installs the plugin named and grants it
  9. // all permissions it may require
  10. func InstallGrantAllPermissions(client client.APIClient, name string) error {
  11. ctx := context.Background()
  12. options := types.PluginInstallOptions{
  13. RemoteRef: name,
  14. AcceptAllPermissions: true,
  15. }
  16. responseReader, err := client.PluginInstall(ctx, "", options)
  17. if err != nil {
  18. return err
  19. }
  20. defer responseReader.Close()
  21. // we have to read the response out here because the client API
  22. // actually starts a goroutine which we can only be sure has
  23. // completed when we get EOF from reading responseBody
  24. _, err = ioutil.ReadAll(responseReader)
  25. return err
  26. }
  27. // Enable enables the named plugin
  28. func Enable(client client.APIClient, name string) error {
  29. ctx := context.Background()
  30. options := types.PluginEnableOptions{}
  31. return client.PluginEnable(ctx, name, options)
  32. }
  33. // Disable disables the named plugin
  34. func Disable(client client.APIClient, name string) error {
  35. ctx := context.Background()
  36. options := types.PluginDisableOptions{}
  37. return client.PluginDisable(ctx, name, options)
  38. }
  39. // Rm removes the named plugin
  40. func Rm(client client.APIClient, name string) error {
  41. return remove(client, name, false)
  42. }
  43. // RmF forces the removal of the named plugin
  44. func RmF(client client.APIClient, name string) error {
  45. return remove(client, name, true)
  46. }
  47. func remove(client client.APIClient, name string, force bool) error {
  48. ctx := context.Background()
  49. options := types.PluginRemoveOptions{Force: force}
  50. return client.PluginRemove(ctx, name, options)
  51. }