plugin_responses.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package types // import "github.com/docker/docker/api/types"
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "sort"
  6. )
  7. // PluginsListResponse contains the response for the Engine API
  8. type PluginsListResponse []*Plugin
  9. // UnmarshalJSON implements json.Unmarshaler for PluginInterfaceType
  10. func (t *PluginInterfaceType) UnmarshalJSON(p []byte) error {
  11. versionIndex := len(p)
  12. prefixIndex := 0
  13. if len(p) < 2 || p[0] != '"' || p[len(p)-1] != '"' {
  14. return fmt.Errorf("%q is not a plugin interface type", p)
  15. }
  16. p = p[1 : len(p)-1]
  17. loop:
  18. for i, b := range p {
  19. switch b {
  20. case '.':
  21. prefixIndex = i
  22. case '/':
  23. versionIndex = i
  24. break loop
  25. }
  26. }
  27. t.Prefix = string(p[:prefixIndex])
  28. t.Capability = string(p[prefixIndex+1 : versionIndex])
  29. if versionIndex < len(p) {
  30. t.Version = string(p[versionIndex+1:])
  31. }
  32. return nil
  33. }
  34. // MarshalJSON implements json.Marshaler for PluginInterfaceType
  35. func (t *PluginInterfaceType) MarshalJSON() ([]byte, error) {
  36. return json.Marshal(t.String())
  37. }
  38. // String implements fmt.Stringer for PluginInterfaceType
  39. func (t PluginInterfaceType) String() string {
  40. return fmt.Sprintf("%s.%s/%s", t.Prefix, t.Capability, t.Version)
  41. }
  42. // PluginPrivilege describes a permission the user has to accept
  43. // upon installing a plugin.
  44. type PluginPrivilege struct {
  45. Name string
  46. Description string
  47. Value []string
  48. }
  49. // PluginPrivileges is a list of PluginPrivilege
  50. type PluginPrivileges []PluginPrivilege
  51. func (s PluginPrivileges) Len() int {
  52. return len(s)
  53. }
  54. func (s PluginPrivileges) Less(i, j int) bool {
  55. return s[i].Name < s[j].Name
  56. }
  57. func (s PluginPrivileges) Swap(i, j int) {
  58. sort.Strings(s[i].Value)
  59. sort.Strings(s[j].Value)
  60. s[i], s[j] = s[j], s[i]
  61. }