requirements.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ansiblev2
  15. import "gopkg.in/yaml.v2"
  16. type RoleSource struct {
  17. Name string
  18. Src string
  19. Version string
  20. }
  21. func (rs *RoleSource) MarshalYAML() (interface{}, error) {
  22. r := map[string]string{
  23. "src": rs.Src,
  24. }
  25. if len(rs.Name) > 0 {
  26. r["name"] = rs.Name
  27. }
  28. if len(rs.Version) > 0 {
  29. r["version"] = rs.Version
  30. }
  31. return r, nil
  32. }
  33. type Requirements struct {
  34. RoleSources []RoleSource
  35. }
  36. func NewRequirements(rss ...RoleSource) *Requirements {
  37. return &Requirements{
  38. RoleSources: rss,
  39. }
  40. }
  41. func (rm *Requirements) AddRoleSource(rss ...RoleSource) {
  42. rm.RoleSources = append(rm.RoleSources, rss...)
  43. }
  44. func (rm *Requirements) MarshalYAML() (interface{}, error) {
  45. var err error
  46. r := make([]interface{}, len(rm.RoleSources))
  47. for i := range rm.RoleSources {
  48. r[i], err = rm.RoleSources[i].MarshalYAML()
  49. if err != nil {
  50. return nil, err
  51. }
  52. }
  53. return r, nil
  54. }
  55. func (rm *Requirements) String() string {
  56. r, err := yaml.Marshal(rm)
  57. if err != nil {
  58. panic(err)
  59. }
  60. return string(r)
  61. }