forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml.go
72 lines (54 loc) · 1.06 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
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"os"
"sort"
yaml "gopkg.in/yaml.v2"
)
const fileMode os.FileMode = 0600
func readYAML(file string) (*LabelsFile, error) {
bytes, err := os.ReadFile(file)
if err != nil {
return nil, err
}
lf := LabelsFile{}
err = yaml.Unmarshal(bytes, &lf)
if err != nil {
return nil, err
}
sort.Sort(lf.Labels)
sort.Sort(lf.Categories)
clean(&lf)
err = check(&lf)
if err != nil {
return nil, fmt.Errorf("file was not in expected format: %v", err)
}
return &lf, nil
}
func writeYAML(lf *LabelsFile, file string) error {
bytes, err := yaml.Marshal(lf)
if err != nil {
return err
}
return os.WriteFile(file, bytes, fileMode)
}
func checkYAML(file string) error {
// read and check
_, err := readYAML(file)
if err == nil {
fmt.Printf("Checked file %v\n", file)
}
return err
}
func sortYAML(fromFile, toFile string) error {
// read and sort
lf, err := readYAML(fromFile)
if err != nil {
return err
}
return writeYAML(lf, toFile)
}