(path string)
| 97 | } |
| 98 | |
| 99 | func (r *SpecReader) loadSpecsFromFile(path string) error { |
| 100 | data, err := os.ReadFile(path) |
| 101 | if err != nil { |
| 102 | return fmt.Errorf("failed to read file %s: %w", path, err) |
| 103 | } |
| 104 | |
| 105 | // support multiple yamls in one file |
| 106 | // this should work both on Windows and Unix |
| 107 | normalizedConfig := bytes.ReplaceAll(data, []byte("\r\n"), []byte("\n")) |
| 108 | |
| 109 | sections := bytes.Split(normalizedConfig, []byte("\n---\n")) |
| 110 | for i, doc := range sections { |
| 111 | doc, err = stripYamlComments(doc) |
| 112 | if err != nil { |
| 113 | return fmt.Errorf("failed to strip yaml comments in file %s (section %d): %w", path, i+1, err) |
| 114 | } |
| 115 | doc, err = expandFileConfig(doc) |
| 116 | if err != nil { |
| 117 | return fmt.Errorf("failed to expand file variable in file %s (section %d): %w", path, i+1, err) |
| 118 | } |
| 119 | doc, err = expandTime(doc) |
| 120 | if err != nil { |
| 121 | return fmt.Errorf("failed to expand time variable in file %s (section %d): %w", path, i+1, err) |
| 122 | } |
| 123 | doc, err = expandEnv(doc) |
| 124 | if err != nil { |
| 125 | return fmt.Errorf("failed to expand environment variable in file %s (section %d): %w", path, i+1, err) |
| 126 | } |
| 127 | var s Spec |
| 128 | if err := SpecUnmarshalYamlStrict(doc, &s); err != nil { |
| 129 | return fmt.Errorf("failed to unmarshal file %s: %w", path, err) |
| 130 | } |
| 131 | switch s.Kind { |
| 132 | case KindSource: |
| 133 | source := s.Spec.(*Source) |
| 134 | if r.sourcesMap[source.Name] != nil { |
| 135 | return fmt.Errorf("duplicate source name %s", source.Name) |
| 136 | } |
| 137 | r.sourceWarningsMap[source.Name] = source.GetWarnings() |
| 138 | source.SetDefaults() |
| 139 | if err := source.Validate(r.relaxed); err != nil { |
| 140 | return fmt.Errorf("failed to validate source %s: %w", source.Name, err) |
| 141 | } |
| 142 | if source.Registry == RegistryGitHub { |
| 143 | log.Warn(). |
| 144 | Str("name", source.Name). |
| 145 | Str("kind", "source"). |
| 146 | Msg("registry: github is deprecated & will be removed in future releases") |
| 147 | } |
| 148 | r.sourcesMap[source.Name] = source |
| 149 | r.Sources = append(r.Sources, source) |
| 150 | case KindDestination: |
| 151 | destination := s.Spec.(*Destination) |
| 152 | if r.destinationsMap[destination.Name] != nil { |
| 153 | return fmt.Errorf("duplicate destination name %s", destination.Name) |
| 154 | } |
| 155 | r.destinationWarningsMap[destination.Name] = destination.GetWarnings() |
| 156 | destination.SetDefaults() |
no test coverage detected