github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/yaml/steps.go (about) 1 // Copyright 2022 Harness, Inc. 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 15 package yaml 16 17 import ( 18 "errors" 19 ) 20 21 type ( 22 Step struct { 23 AddSSHKeys *AddSSHKeys 24 AttachWorkspace *AttachWorkspace 25 Checkout *Checkout 26 PersistToWorkspace *PersistToWorkspace 27 RestoreCache *RestoreCache 28 Run *Run 29 SaveCache *SaveCache 30 SetupRemoteDocker *SetupRemoteDocker 31 StoreArtifacts *StoreArtifacts 32 StoreTestResults *StoreTestResults 33 Unless *Unless 34 When *When 35 Custom *Custom 36 } 37 38 step struct { 39 AddSSHKeys *AddSSHKeys `yaml:"add_ssh_keys,omitempty"` 40 AttachWorkspace *AttachWorkspace `yaml:"attach_workspace,omitempty"` 41 Checkout *Checkout `yaml:"checkout,omitempty"` 42 PersistToWorkspace *PersistToWorkspace `yaml:"persist_to_workspace,omitempty"` 43 RestoreCache *RestoreCache `yaml:"restore_cache,omitempty"` 44 Run *Run `yaml:"run,omitempty"` 45 SaveCache *SaveCache `yaml:"save_cache,omitempty"` 46 SetupRemoteDocker *SetupRemoteDocker `yaml:"setup_remote_docker,omitempty"` 47 StoreArtifacts *StoreArtifacts `yaml:"store_artifacts,omitempty"` 48 StoreTestResults *StoreTestResults `yaml:"store_test_results,omitempty"` 49 Unless *Unless `yaml:"unless,omitempty"` 50 When *When `yaml:"when,omitempty"` 51 Custom map[string]interface{} `yaml:",inline"` 52 } 53 ) 54 55 // UnmarshalYAML implements the unmarshal interface. 56 func (v *Step) UnmarshalYAML(unmarshal func(interface{}) error) error { 57 var out1 string 58 var out2 *step 59 60 if err := unmarshal(&out1); err == nil { 61 switch out1 { 62 case "add_ssh_keys": 63 v.AddSSHKeys = new(AddSSHKeys) 64 case "attach_workspace": 65 v.AttachWorkspace = new(AttachWorkspace) 66 case "checkout": 67 v.Checkout = new(Checkout) 68 case "persist_to_workspace": 69 v.PersistToWorkspace = new(PersistToWorkspace) 70 case "restore_cache": 71 v.RestoreCache = new(RestoreCache) 72 case "run": 73 v.Run = new(Run) 74 case "save_cache": 75 v.SaveCache = new(SaveCache) 76 case "setup_remote_docker": 77 v.SetupRemoteDocker = new(SetupRemoteDocker) 78 case "store_artifacts": 79 v.StoreArtifacts = new(StoreArtifacts) 80 case "store_test_results": 81 v.StoreTestResults = new(StoreTestResults) 82 case "unless": 83 v.Unless = new(Unless) 84 case "when": 85 v.When = new(When) 86 default: 87 v.Custom = new(Custom) 88 v.Custom.Name = out1 89 v.Custom.Params = map[string]interface{}{} 90 } 91 return nil 92 } 93 94 if err := unmarshal(&out2); err == nil { 95 v.AddSSHKeys = out2.AddSSHKeys 96 v.AttachWorkspace = out2.AttachWorkspace 97 v.Checkout = out2.Checkout 98 v.PersistToWorkspace = out2.PersistToWorkspace 99 v.RestoreCache = out2.RestoreCache 100 v.Run = out2.Run 101 v.SaveCache = out2.SaveCache 102 v.SetupRemoteDocker = out2.SetupRemoteDocker 103 v.StoreArtifacts = out2.StoreArtifacts 104 v.StoreTestResults = out2.StoreTestResults 105 v.Unless = out2.Unless 106 v.When = out2.When 107 for name, params := range out2.Custom { 108 v.Custom = new(Custom) 109 v.Custom.Name = name 110 if vv, ok := params.(map[string]interface{}); ok { 111 v.Custom.Params = vv 112 } 113 } 114 115 return nil 116 } 117 118 return errors.New("failed to unmarshal step") 119 } 120 121 // MarshalYAML implements the marshal interface. 122 func (v *Step) MarshalYAML() (interface{}, error) { 123 if v.Custom != nil { 124 return map[string]interface{}{ 125 v.Custom.Name: v.Custom.Params, 126 }, nil 127 } 128 return &step{ 129 AddSSHKeys: v.AddSSHKeys, 130 AttachWorkspace: v.AttachWorkspace, 131 Checkout: v.Checkout, 132 PersistToWorkspace: v.PersistToWorkspace, 133 RestoreCache: v.RestoreCache, 134 Run: v.Run, 135 SaveCache: v.SaveCache, 136 SetupRemoteDocker: v.SetupRemoteDocker, 137 StoreArtifacts: v.StoreArtifacts, 138 StoreTestResults: v.StoreTestResults, 139 Unless: v.Unless, 140 When: v.When, 141 }, nil 142 } 143 144 // 145 // Step Types 146 // 147 148 type ( 149 AddSSHKeys struct { 150 Fingerprints []string `yaml:"fingerprints,omitempty"` 151 Name string `yaml:"name,omitempty"` 152 } 153 154 AttachWorkspace struct { 155 At string `yaml:"at,omitempty"` 156 Name *string `yaml:"name,omitempty"` 157 } 158 159 Checkout struct { 160 Name string `yaml:"name,omitempty"` 161 Path string `yaml:"path,omitempty"` 162 } 163 164 Custom struct { 165 Name string 166 Params map[string]interface{} 167 } 168 169 PersistToWorkspace struct { 170 Name string `yaml:"name,omitempty"` 171 Paths Stringorslice `yaml:"paths,omitempty"` 172 Root string `yaml:"root,omitempty"` 173 } 174 175 RestoreCache struct { 176 Key string `yaml:"key,omitempty"` 177 Name string `yaml:"name,omitempty"` 178 Keys Stringorslice `yaml:"keys,omitempty"` 179 } 180 181 SaveCache struct { 182 Key string `yaml:"key,omitempty"` 183 Name string `yaml:"name,omitempty"` 184 Paths Stringorslice `yaml:"paths,omitempty"` 185 When string `yaml:"when,omitempty"` 186 } 187 188 SetupRemoteDocker struct { 189 DockerLayerCaching bool `yaml:"docker_layer_caching,omitempty"` 190 Name string `yaml:"name,omitempty"` 191 Version string `yaml:"version,omitempty"` 192 } 193 194 StoreArtifacts struct { 195 Destination string `yaml:"destination,omitempty"` 196 Name string `yaml:"name,omitempty"` 197 Path string `yaml:"path,omitempty"` 198 } 199 200 StoreTestResults struct { 201 Name string `yaml:"name,omitempty"` 202 Path string `yaml:"path,omitempty"` 203 } 204 205 Unless struct { 206 Condition *Logical `json:"condition,omitempty"` 207 Steps []*Step `json:"steps,omitempty"` 208 } 209 210 When struct { 211 Condition *Logical `json:"condition,omitempty"` 212 Steps []*Step `json:"steps,omitempty"` 213 } 214 )