github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/model/core/ymlobj.go (about)

     1  // Ultimate Provisioner: UP cmd
     2  // Copyright (c) 2019 Stephen Cheng and contributors
     3  
     4  /* This Source Code Form is subject to the terms of the Mozilla Public
     5   * License, v. 2.0. If a copy of the MPL was not distributed with this
     6   * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
     7  
     8  package core
     9  
    10  import (
    11  	u "github.com/upcmd/up/utils"
    12  	yq "github.com/upcmd/yq/v3/cmd"
    13  	"gopkg.in/yaml.v2"
    14  	"io/ioutil"
    15  	"strings"
    16  )
    17  
    18  func ObjToYaml(obj interface{}) string {
    19  	ymlbytes, err := yaml.Marshal(&obj)
    20  	u.LogErrorAndPanic("obj to yaml converstion", err, "yml convesion failed")
    21  	yml := string(ymlbytes)
    22  	//TODO: revist for the extra \n at the end of string
    23  	cleaned := u.RemoveEmptyLines(yml)
    24  	if u.LineCount(cleaned) == 1 {
    25  		yml = strings.TrimSuffix(cleaned, "\n")
    26  	}
    27  	return yml
    28  }
    29  
    30  func LoadObjectFromFile(filepath string) interface{} {
    31  	data, err := ioutil.ReadFile(filepath)
    32  	u.LogErrorAndPanic("load object", err, "read file error")
    33  	return YamlToObj(string(data))
    34  }
    35  
    36  func YamlToObj(srcyml string) interface{} {
    37  	if srcyml == "" {
    38  		return ""
    39  	}
    40  	obj := new(interface{})
    41  	err := yaml.Unmarshal([]byte(srcyml), obj)
    42  	u.LogErrorAndContinue("yml to object:", err, u.Spf("please validate the ymal content\n---\n%s\n---\n", u.ContentWithLineNumber(srcyml)))
    43  	return obj
    44  }
    45  
    46  /*
    47  obj is a cache item
    48  path format: a.b.c(name=fr*).value
    49  prefix will be used to get the obj, rest will be used as yq path
    50  */
    51  func GetSubObjectFromCache(cache *Cache, path string, collect bool, verboseLevel string) interface{} {
    52  	yqresult := GetSubYmlFromCache(cache, path, collect, verboseLevel)
    53  	obj := YamlToObj(yqresult)
    54  	return obj
    55  }
    56  
    57  func GetSubObjectFromYml(ymlstr string, path string, collect bool, verboseLevel string) interface{} {
    58  	yqresult, err := yq.UpReadYmlStr(ymlstr, path, verboseLevel, collect)
    59  	u.LogErrorAndContinue("parse sub element in yml", err, u.Spf("please ensure correct yml query path: %s", path))
    60  	obj := YamlToObj(yqresult)
    61  	return obj
    62  }
    63  
    64  func GetSubObjectFromFile(ymlfile string, path string, collect bool, verboseLevel string) interface{} {
    65  	yqresult, err := yq.UpReadYmlFile(ymlfile, path, verboseLevel, collect)
    66  	u.LogErrorAndContinue("parse sub element in yml", err, u.Spf("please ensure correct yml query path: %s", path))
    67  	obj := YamlToObj(yqresult)
    68  	return obj
    69  }
    70  
    71  func GetSubYmlFromYml(ymlstr string, path string, collect bool, verboseLevel string) string {
    72  	yqresult, err := yq.UpReadYmlStr(ymlstr, path, verboseLevel, collect)
    73  	u.LogErrorAndContinue("parse sub element in yml", err, u.Spf("please ensure correct yml query path: %s", path))
    74  	return yqresult
    75  }
    76  
    77  func GetSubYmlFromFile(ymlfile string, path string, collect bool, verboseLevel string) string {
    78  	yqresult, err := yq.UpReadYmlFile(ymlfile, path, verboseLevel, collect)
    79  	u.LogErrorAndContinue("parse sub element in yml", err, u.Spf("please ensure correct yml query path: %s", path))
    80  	return yqresult
    81  }
    82  
    83  func GetSubYmlFromCache(cache *Cache, path string, collect bool, verboseLevel string) string {
    84  	//obj -> yml -> yq to get node in yml -> obj
    85  	elist := strings.Split(path, ".")
    86  	func() {
    87  		if elist[0] == "" {
    88  			u.InvalidAndPanic("yml path validation", u.Spf("path format is not correct, use format like:\n %s", u.Yq_read_hint))
    89  		}
    90  	}()
    91  	yqpath := strings.Join(elist[1:], ".")
    92  
    93  	cacheKey := elist[0]
    94  	obj := cache.Get(cacheKey)
    95  	ymlstr := ObjToYaml(obj)
    96  	u.Dvvvvv("sub yml str")
    97  	u.Dvvvvv(ymlstr)
    98  	yqresult, err := yq.UpReadYmlStr(ymlstr, yqpath, verboseLevel, collect)
    99  	u.LogErrorAndContinue("parse sub element in yml", err, u.Spf("please ensure correct yml query path: %s", yqpath))
   100  	return yqresult
   101  }