github.com/drone/runner-go@v1.12.0/pipeline/runtime/const.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package runtime 6 7 import ( 8 "bytes" 9 "encoding/json" 10 ) 11 12 // 13 // run policy 14 // 15 16 // RunPolicy defines the policy for starting containers 17 // based on the point-in-time pass or fail state of 18 // the pipeline. 19 type RunPolicy int 20 21 // RunPolicy enumeration. 22 const ( 23 RunOnSuccess RunPolicy = iota 24 RunOnFailure 25 RunAlways 26 RunNever 27 ) 28 29 func (r RunPolicy) String() string { 30 return runPolicyID[r] 31 } 32 33 var runPolicyID = map[RunPolicy]string{ 34 RunOnSuccess: "on-success", 35 RunOnFailure: "on-failure", 36 RunAlways: "always", 37 RunNever: "never", 38 } 39 40 var runPolicyName = map[string]RunPolicy{ 41 "": RunOnSuccess, 42 "on-success": RunOnSuccess, 43 "on-failure": RunOnFailure, 44 "always": RunAlways, 45 "never": RunNever, 46 } 47 48 // MarshalJSON marshals the string representation of the 49 // run type to JSON. 50 func (r *RunPolicy) MarshalJSON() ([]byte, error) { 51 buffer := bytes.NewBufferString(`"`) 52 buffer.WriteString(runPolicyID[*r]) 53 buffer.WriteString(`"`) 54 return buffer.Bytes(), nil 55 } 56 57 // UnmarshalJSON unmarshals the json representation of the 58 // run type from a string value. 59 func (r *RunPolicy) UnmarshalJSON(b []byte) error { 60 // unmarshal as string 61 var s string 62 err := json.Unmarshal(b, &s) 63 if err != nil { 64 return err 65 } 66 // lookup value 67 *r = runPolicyName[s] 68 return nil 69 } 70 71 // 72 // failure policy 73 // 74 75 // ErrPolicy defines the step error policy 76 type ErrPolicy int 77 78 // ErrPolicy enumeration. 79 const ( 80 ErrFail ErrPolicy = iota 81 ErrFailFast 82 ErrIgnore 83 ) 84 85 func (p ErrPolicy) String() string { 86 return errPolicyID[p] 87 } 88 89 var errPolicyID = map[ErrPolicy]string{ 90 ErrFail: "fail", 91 ErrFailFast: "fail-fast", 92 ErrIgnore: "ignore", 93 } 94 95 var errPolicyName = map[string]ErrPolicy{ 96 "": ErrFail, 97 "fail": ErrFail, 98 "fail-fast": ErrFailFast, 99 "fast": ErrFailFast, 100 "always": ErrFail, 101 "ignore": ErrIgnore, 102 } 103 104 // MarshalJSON marshals the string representation of the 105 // pull type to JSON. 106 func (p *ErrPolicy) MarshalJSON() ([]byte, error) { 107 buffer := bytes.NewBufferString(`"`) 108 buffer.WriteString(errPolicyID[*p]) 109 buffer.WriteString(`"`) 110 return buffer.Bytes(), nil 111 } 112 113 // UnmarshalJSON unmarshals the json representation of the 114 // pull type from a string value. 115 func (p *ErrPolicy) UnmarshalJSON(b []byte) error { 116 // unmarshal as string 117 var s string 118 err := json.Unmarshal(b, &s) 119 if err != nil { 120 return err 121 } 122 // lookup value 123 *p = errPolicyName[s] 124 return nil 125 }