github.com/drone/runner-go@v1.12.0/manifest/env.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 manifest 6 7 type ( 8 // Variable represents an environment variable that 9 // can be defined as a string literal or as a reference 10 // to a secret. 11 Variable struct { 12 Value string `json:"value,omitempty"` 13 Secret string `json:"from_secret,omitempty" yaml:"from_secret"` 14 } 15 16 // variable is a temporary type used to unmarshal 17 // variables with references to secrets. 18 variable struct { 19 Value string 20 Secret string `yaml:"from_secret"` 21 } 22 ) 23 24 // UnmarshalYAML implements yaml unmarshalling. 25 func (v *Variable) UnmarshalYAML(unmarshal func(interface{}) error) error { 26 d := new(variable) 27 err := unmarshal(&d.Value) 28 if err != nil { 29 err = unmarshal(d) 30 } 31 v.Value = d.Value 32 v.Secret = d.Secret 33 return err 34 } 35 36 // MarshalYAML implements yaml marshalling. 37 func (v *Variable) MarshalYAML() (interface{}, error) { 38 if v.Secret != "" { 39 m := map[string]interface{}{} 40 m["from_secret"] = v.Secret 41 return m, nil 42 } 43 if v.Value != "" { 44 return v.Value, nil 45 } 46 return nil, nil 47 }