-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
142 lines (118 loc) · 4.03 KB
/
yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright Mia srl
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
import (
"bytes"
"errors"
"io/fs"
"os"
"path/filepath"
"github.com/mia-platform/vab/pkg/apis/vab.mia-platform.eu/v1alpha1"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"sigs.k8s.io/kustomize/api/konfig"
kustomize "sigs.k8s.io/kustomize/api/types"
)
const (
defaultFilePermissions = 0644
yamlDefaultIndent = 2
)
type WrongFileNameError struct {
expectedFileName string
actualFileName string
}
func NewWrongFileNameError(expected string, actual string) error {
return WrongFileNameError{
expectedFileName: expected,
actualFileName: actual,
}
}
func (e WrongFileNameError) Error() string {
return "expected file name " + e.expectedFileName + " but found " + e.actualFileName
}
// ReadConfig reads a configuration file into a ClustersConfiguration struct
func ReadConfig(configPath string) (*v1alpha1.ClustersConfiguration, error) {
configFile, readErr := os.ReadFile(configPath)
if readErr != nil {
return nil, readErr
}
output := &v1alpha1.ClustersConfiguration{}
yamlErr := yaml.Unmarshal(configFile, output)
if yamlErr != nil {
return nil, yamlErr
}
return output, nil
}
// writeYamlFile marshals the interface passed as argument, and writes it to a
// YAML file
func writeYamlFile(file interface{}, dstPath string) error {
var b bytes.Buffer
yamlEncoder := yaml.NewEncoder(&b)
yamlEncoder.SetIndent(yamlDefaultIndent)
if err := yamlEncoder.Encode(&file); err != nil {
return err
}
if writeErr := os.WriteFile(dstPath, b.Bytes(), defaultFilePermissions); writeErr != nil {
return writeErr
}
return nil
}
// WriteConfig creates and writes an empty vab configuration file
func WriteConfig(config v1alpha1.ClustersConfiguration, dirOrFilePath string) error {
dirOrFile, err := os.Stat(dirOrFilePath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
var dstPath string
if err == nil && dirOrFile.IsDir() {
dstPath = filepath.Join(dirOrFilePath, DefaultConfigFilename)
} else {
dstPath = dirOrFilePath
}
return writeYamlFile(config, dstPath)
}
// WriteKustomization creates and writes an empty kustomization file
func WriteKustomization(kustomization kustomize.Kustomization, dirOrFilePath string) error {
dirOrFile, err := os.Stat(dirOrFilePath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
var dstPath string
var dstPathCond bool
switch dstPathCond {
case dstPathCond == (err == nil && dirOrFile.IsDir()):
dstPath = filepath.Join(dirOrFilePath, konfig.DefaultKustomizationFileName())
case dstPathCond == (!slices.Contains(konfig.RecognizedKustomizationFileNames(), filepath.Base(dirOrFilePath))):
return NewWrongFileNameError(konfig.DefaultKustomizationFileName(), filepath.Base(dirOrFilePath))
default:
dstPath = dirOrFilePath
}
return writeYamlFile(kustomization, dstPath)
}
// EmptyKustomization return a valid empty kustomization with valid kind and apiVersion fields
func EmptyKustomization() kustomize.Kustomization {
// mini hack for generating a valid kustomization structure as kustomize intend
empty := kustomize.Kustomization{}
empty.FixKustomizationPostUnmarshalling()
return empty
}
// EmptyKustomization return a valid empty kustomization with valid kind and apiVersion fields
func EmptyComponent() kustomize.Kustomization {
// mini hack for generating a valid kustomization structure as kustomize intend
empty := kustomize.Kustomization{}
empty.Kind = kustomize.ComponentKind
empty.FixKustomizationPostUnmarshalling()
return empty
}