apply_flags.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package v2
  2. import (
  3. "encoding/json"
  4. )
  5. // OperationType represents a JSON patch operation type
  6. type OperationType string
  7. const (
  8. // AddOperation indicates that a value should be added to a JSON object or array
  9. AddOperation OperationType = "add"
  10. // RemoveOperation indicates that a value should be removed from a JSON object or array
  11. RemoveOperation OperationType = "remove"
  12. // ReplaceOperation indicates that a value should be replaced in a JSON object or array
  13. ReplaceOperation OperationType = "replace"
  14. // MoveOperation indicates that a value should be moved within a JSON object or array
  15. MoveOperation OperationType = "move"
  16. // CopyOperation indicates that a value should be copied within a JSON object or array
  17. CopyOperation OperationType = "copy"
  18. )
  19. // PatchOperation represents a full JSON patch operation
  20. type PatchOperation struct {
  21. // Operation is the type of operation to perform
  22. Operation OperationType `json:"op"`
  23. // Path is the JSON pointer to the value to be operated on
  24. Path string `json:"path"`
  25. // Value is the value to be added, replaced, or moved
  26. Value interface{} `json:"value,omitempty"`
  27. }
  28. func (op PatchOperation) String() (string, error) {
  29. var res string
  30. by, err := json.Marshal(op)
  31. if err != nil {
  32. return res, err
  33. }
  34. return string(by), nil
  35. }
  36. // ApplyFlag is an interface that represents a flag that can be applied to a PorterApp
  37. // removal operations are handled separately
  38. type ApplyFlag interface {
  39. AsPatchOperations() []PatchOperation
  40. }
  41. // SetName is a flag that can be applied to a PorterApp to set the name
  42. type SetName struct {
  43. Name string
  44. }
  45. // AsPatchOperations returns the patch operations to set the name
  46. func (f SetName) AsPatchOperations() []PatchOperation {
  47. return []PatchOperation{
  48. {
  49. Operation: AddOperation,
  50. Path: "/name",
  51. Value: f.Name,
  52. },
  53. }
  54. }
  55. // AttachEnvGroupsFlag is a flag that can be applied to a PorterApp to attach environment groups
  56. type AttachEnvGroupsFlag struct {
  57. EnvGroups []string
  58. }
  59. // envGroupWithoutVersion is a struct that represents an environment group without a version
  60. // the version will be auto added on hydrate
  61. type envGroupWithoutVersion struct {
  62. Name string `json:"name"`
  63. }
  64. // AsPatchOperations returns the patch operations to attach the environment groups
  65. func (f AttachEnvGroupsFlag) AsPatchOperations() []PatchOperation {
  66. var envGroups []envGroupWithoutVersion
  67. for _, envGroup := range f.EnvGroups {
  68. envGroups = append(envGroups, envGroupWithoutVersion{
  69. Name: envGroup,
  70. })
  71. }
  72. var ops []PatchOperation
  73. for _, envGroup := range envGroups {
  74. ops = append(ops, PatchOperation{
  75. Operation: AddOperation,
  76. Path: "/envGroups/-",
  77. Value: envGroup,
  78. })
  79. }
  80. return ops
  81. }
  82. // SetBuildContext is a flag that can be applied to a PorterApp to set the build context
  83. type SetBuildContext struct {
  84. Context string
  85. }
  86. // AsPatchOperations returns the patch operations to set the build context
  87. func (f SetBuildContext) AsPatchOperations() []PatchOperation {
  88. return []PatchOperation{
  89. {
  90. Operation: AddOperation,
  91. Path: "/build/context",
  92. Value: f.Context,
  93. },
  94. }
  95. }
  96. // SetBuildMethod is a flag that can be applied to a PorterApp to set the build method
  97. type SetBuildMethod struct {
  98. Method string
  99. }
  100. // AsPatchOperations returns the patch operations to set the build method
  101. func (f SetBuildMethod) AsPatchOperations() []PatchOperation {
  102. return []PatchOperation{
  103. {
  104. Operation: AddOperation,
  105. Path: "/build/method",
  106. Value: f.Method,
  107. },
  108. }
  109. }
  110. // SetBuildDockerfile is a flag that can be applied to a PorterApp to set the build Dockerfile
  111. type SetBuildDockerfile struct {
  112. Dockerfile string
  113. }
  114. // AsPatchOperations returns the patch operations to set the build Dockerfile
  115. func (f SetBuildDockerfile) AsPatchOperations() []PatchOperation {
  116. return []PatchOperation{
  117. {
  118. Operation: AddOperation,
  119. Path: "/build/dockerfile",
  120. Value: f.Dockerfile,
  121. },
  122. }
  123. }
  124. // AttachBuildpacks is a flag that can be applied to a PorterApp to attach buildpacks
  125. type AttachBuildpacks struct {
  126. Buildpacks []string
  127. }
  128. // AsPatchOperations returns the patch operations to attach the buildpacks
  129. func (f AttachBuildpacks) AsPatchOperations() []PatchOperation {
  130. var ops []PatchOperation
  131. for _, buildpack := range f.Buildpacks {
  132. ops = append(ops, PatchOperation{
  133. Operation: AddOperation,
  134. Path: "/build/buildpacks/-",
  135. Value: buildpack,
  136. })
  137. }
  138. return ops
  139. }
  140. // SetBuilder is a flag that can be applied to a PorterApp to set the builder
  141. type SetBuilder struct {
  142. Builder string
  143. }
  144. // AsPatchOperations returns the patch operations to set the builder
  145. func (f SetBuilder) AsPatchOperations() []PatchOperation {
  146. return []PatchOperation{
  147. {
  148. Operation: AddOperation,
  149. Path: "/build/builder",
  150. Value: f.Builder,
  151. },
  152. }
  153. }
  154. // SetImageRepo is a flag that can be applied to a PorterApp to set the image repo
  155. type SetImageRepo struct {
  156. Repo string
  157. }
  158. // AsPatchOperations returns the patch operations to set the image repo
  159. func (f SetImageRepo) AsPatchOperations() []PatchOperation {
  160. return []PatchOperation{
  161. {
  162. Operation: AddOperation,
  163. Path: "/image/repository",
  164. Value: f.Repo,
  165. },
  166. }
  167. }
  168. // SetImageTag is a flag that can be applied to a PorterApp to set the image tag
  169. type SetImageTag struct {
  170. Tag string
  171. }
  172. // AsPatchOperations returns the patch operations to set the image tag
  173. func (f SetImageTag) AsPatchOperations() []PatchOperation {
  174. return []PatchOperation{
  175. {
  176. Operation: AddOperation,
  177. Path: "/image/tag",
  178. Value: f.Tag,
  179. },
  180. }
  181. }