github.com/amplia-iiot/yutil@v1.0.1-0.20231229120411-5d96a4c5a136/pkg/merge/files.go (about) 1 /* 2 Copyright (c) 2021 amplia-iiot 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in all 12 copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 SOFTWARE. 21 */ 22 23 package merge 24 25 import ( 26 "errors" 27 28 "github.com/amplia-iiot/yutil/internal/io" 29 ) 30 31 // MergeFiles returns the result of merging two yaml files. A yaml leaf node in 32 // the 'changes' file takes precedence over and replaces the value in the 'base' 33 // file. 34 func MergeFiles(base string, changes string) (string, error) { 35 baseContent, err := io.ReadAsString(base) 36 if err != nil { 37 return "", err 38 } 39 changesContent, err := io.ReadAsString(changes) 40 if err != nil { 41 return "", err 42 } 43 return MergeContents(baseContent, changesContent) 44 } 45 46 // MergeAllFiles returns the result of merging all yaml files, which should be 47 // ordered in ascending level of importance in the hierarchy. A yaml leaf node in 48 // the last file takes precedence over and replaces the value in any previous 49 // file. 50 func MergeAllFiles(files []string) (string, error) { 51 if len(files) < 2 { 52 return "", errors.New("slice must contain at least two files") 53 } 54 contents := make([]string, len(files)) 55 56 for i, f := range files { 57 var err error 58 contents[i], err = io.ReadAsString(f) 59 if err != nil { 60 return "", err 61 } 62 } 63 return MergeAllContents(contents) 64 } 65 66 // MergeStdinWithFiles returns the result of merging stdin as yaml content with 67 // all yaml files, which should be ordered in ascending level of importance in 68 // the hierarchy. Stdin is the least important yaml. A yaml leaf node in the last 69 // file takes precedence over and replaces the value in any previous file, 70 // including values in stdin. 71 func MergeStdinWithFiles(files []string) (string, error) { 72 if len(files) < 1 { 73 return "", errors.New("slice must contain at least one file") 74 } 75 var err error 76 77 contents := make([]string, len(files)+1) 78 79 contents[0], err = io.ReadStdin() 80 if err != nil { 81 return "", err 82 } 83 84 for i, f := range files { 85 contents[i+1], err = io.ReadAsString(f) 86 if err != nil { 87 return "", err 88 } 89 } 90 return MergeAllContents(contents) 91 } 92 93 // MergeAllFilesToFile writes to an output file the result of merging all yaml 94 // files, which should be ordered in ascending level of importance in the 95 // hierarchy. A yaml leaf node in the last file takes precedence over and 96 // replaces the value in any previous file. 97 func MergeAllFilesToFile(files []string, output string) error { 98 merged, err := MergeAllFiles(files) 99 if err != nil { 100 return err 101 } 102 return io.WriteToFile(output, merged) 103 }