MCPcopy Index your code
hub / github.com/spacecloud-io/space-cloud / ReadSpecObjectsFromFile

Function ReadSpecObjectsFromFile

space-cli/cmd/utils/file.go:44–91  ·  view source on GitHub ↗

ReadSpecObjectsFromFile returns the spec objects present in the file

(fileName string)

Source from the content-addressed store, hash-verified

42
43// ReadSpecObjectsFromFile returns the spec objects present in the file
44func ReadSpecObjectsFromFile(fileName string) ([]*model.SpecObject, error) {
45 var specs []*model.SpecObject
46
47 var data []byte
48 var err error
49 if strings.HasPrefix(fileName, "http") {
50 valuesFileObj, err := ExtractValuesObj("", fileName)
51 if err != nil {
52 return nil, err
53 }
54 data, err = yaml.Marshal(valuesFileObj)
55 if err != nil {
56 return nil, err
57 }
58 } else {
59 // Read the file first
60 data, err = file.File.ReadFile(fileName)
61 if err != nil {
62 return nil, err
63 }
64 }
65
66 if len(data) == 0 {
67 logrus.Infoln("empty file provided")
68 return nil, nil
69 }
70
71 // Split the files into independent objects
72 dataStrings := makeSpecString(string(data))
73 for _, dataString := range dataStrings {
74
75 // Skip if string is too small to be a spec object
76 if len(dataString) <= 5 {
77 continue
78 }
79
80 // Unmarshal spec object
81 spec := new(model.SpecObject)
82 if err := UnmarshalYAML([]byte(dataString), spec); err != nil {
83 return nil, err
84 }
85
86 // Append the spec object into the array
87 specs = append(specs, spec)
88 }
89
90 return specs, nil
91}
92
93func makeSpecString(raw string) []string {
94 lines := strings.Split(strings.Replace(raw, "\r\n", "\n", -1), "\n")

Callers 1

Calls 4

ExtractValuesObjFunction · 0.85
makeSpecStringFunction · 0.85
UnmarshalYAMLFunction · 0.85
ReadFileMethod · 0.65

Tested by 1