github.com/spinnaker/spin@v1.30.0/cmd/output/output.go (about) 1 // Copyright (c) 2018, Google, 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 output 16 17 import ( 18 "bytes" 19 "encoding/json" 20 "errors" 21 "fmt" 22 "strings" 23 24 "k8s.io/client-go/util/jsonpath" 25 "sigs.k8s.io/yaml" 26 ) 27 28 type OutputFormater func(interface{}) ([]byte, error) 29 30 // ParseOutputFormat returns an OutputFormater based on the specified format. 31 // Accepted values include 'json', 'yaml', and 'jsonpath=PATH'. 32 // Empty string defaults to 'json'. 33 // For more about JSONPath, see https://goessner.net/articles/JsonPath/ 34 func ParseOutputFormat(outputFormat string) (OutputFormater, error) { 35 switch { 36 case outputFormat == "" || outputFormat == "json": 37 return MarshalToJson, nil 38 case outputFormat == "yaml": 39 return MarshalToYaml, nil 40 case strings.HasPrefix(outputFormat, "jsonpath=") && outputFormat != "jsonpath=": 41 toks := strings.Split(outputFormat, "=") 42 if len(toks) != 2 { 43 return nil, errors.New(fmt.Sprintf("Failed to parse output format flag value: %s", outputFormat)) 44 } 45 return MarshalToJsonPathWrapper(toks[1]), nil 46 default: 47 return nil, errors.New(fmt.Sprintf("Failed to parse output format flag value: %s", outputFormat)) 48 } 49 } 50 51 func MarshalToJson(input interface{}) ([]byte, error) { 52 pretty, err := json.MarshalIndent(input, "", " ") 53 if err != nil { 54 return nil, fmt.Errorf("Failed to marshal to json: %v", err) 55 } 56 return pretty, nil 57 } 58 59 // MarshalToJsonPathWrapper returns a MarshalToJsonPath function that uses the 60 // specified jsonpath expression. 61 // This leverages the kubernetes jsonpath library 62 // (https://kubernetes.io/docs/reference/kubectl/jsonpath/). 63 func MarshalToJsonPathWrapper(expression string) OutputFormater { 64 expr := expression 65 // aka MarshalToJsonPath 66 return func(input interface{}) ([]byte, error) { 67 jp := jsonpath.New("json-path") 68 69 if err := jp.Parse(expr); err != nil { 70 return nil, fmt.Errorf("Failed to parse jsonpath expression: %v", err) 71 } 72 73 values, err := jp.FindResults(input) 74 if err != nil { 75 return nil, fmt.Errorf("Failed to execute jsonpath %s on input %s: %v ", expr, input, err) 76 } 77 78 if values == nil || len(values) == 0 || len(values[0]) == 0 { 79 return nil, errors.New(fmt.Sprintf("Error parsing value from input %v using template %s: %v ", input, expr, err)) 80 } 81 82 json, err := MarshalToJson(values[0][0].Interface()) 83 if err != nil { 84 return nil, err 85 } 86 return unquote(json), err 87 } 88 } 89 90 func MarshalToYaml(input interface{}) ([]byte, error) { 91 pretty, err := yaml.Marshal(input) 92 if err != nil { 93 return nil, fmt.Errorf("Failed to marshal to yaml: %v", err) 94 } 95 return pretty, nil 96 } 97 98 func unquote(input []byte) []byte { 99 input = bytes.TrimLeft(input, "\"") 100 input = bytes.TrimRight(input, "\"") 101 return input 102 }