1 // Copyright 2021 Palantir Technologies, 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 appconfig 16 17 import ( 18 "errors" 19 20 "gopkg.in/yaml.v2" 21 ) 22 23 // YAMLRemoteRefParser parses b as a YAML-encoded RemoteRef. It assumes all 24 // parsing errors mean the content is not a RemoteRef. 25 func YAMLRemoteRefParser(path string, b []byte) (*RemoteRef, error) { 26 var maybeRef struct { 27 Remote *string `yaml:"remote"` 28 Path string `yaml:"path"` 29 Ref string `yaml:"ref"` 30 } 31 32 if err := yaml.UnmarshalStrict(b, &maybeRef); err != nil { 33 // assume errors mean this isn't a remote config 34 return nil, nil 35 } 36 if maybeRef.Remote == nil { 37 return nil, nil 38 } 39 40 ref := RemoteRef{ 41 Remote: *maybeRef.Remote, 42 Path: maybeRef.Path, 43 Ref: maybeRef.Ref, 44 } 45 if ref.Remote == "" { 46 return nil, errors.New("invalid remote reference: empty \"remote\" field") 47 } 48 return &ref, nil 49 } 50