ReadSpecObjectsFromFile returns the spec objects present in the file
(fileName string)
| 42 | |
| 43 | // ReadSpecObjectsFromFile returns the spec objects present in the file |
| 44 | func 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 | |
| 93 | func makeSpecString(raw string) []string { |
| 94 | lines := strings.Split(strings.Replace(raw, "\r\n", "\n", -1), "\n") |