|
| 1 | +// Copyright 2025 Interlynk.io |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package profiles |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert/yaml" |
| 24 | +) |
| 25 | + |
| 26 | +// YAML schema + loader (from file or built-ins). |
| 27 | + |
| 28 | +// ReadProfileFile reads, unmarshal, validates and returns a config. |
| 29 | +func ReadProfileFile(path string) (*Config, error) { |
| 30 | + cfgData, err := os.ReadFile(path) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + var cfg Config |
| 36 | + if err := yaml.Unmarshal(cfgData, &cfg); err != nil { |
| 37 | + return nil, fmt.Errorf("profiles: yaml decode: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + if err := validateConfig(&cfg); err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return &cfg, nil |
| 44 | +} |
| 45 | + |
| 46 | +func validateConfig(cfg *Config) error { |
| 47 | + if cfg == nil { |
| 48 | + return errors.New("profiles: nil") |
| 49 | + } |
| 50 | + |
| 51 | + if len(cfg.Profiles) == 0 { |
| 52 | + return errors.New("profiles: no profiles found") |
| 53 | + } |
| 54 | + |
| 55 | + profileExist := make(map[string]bool) |
| 56 | + |
| 57 | + for i, profile := range cfg.Profiles { |
| 58 | + |
| 59 | + // validate profile name, duplicacy, etc |
| 60 | + if err := validateProfile(i, profile, profileExist); err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + // validate profile features |
| 65 | + if err := validateProfileFeatures(profile); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func validateProfileFeatures(profile Profile) error { |
| 74 | + // validate profile features |
| 75 | + featureExists := make(map[string]bool) |
| 76 | + for i, feat := range profile.Features { |
| 77 | + key := strings.TrimSpace(feat.Key) |
| 78 | + |
| 79 | + if key == "" { |
| 80 | + return fmt.Errorf("profiles: profile %q has empty feature.name at index %d", profile.Name, i) |
| 81 | + } |
| 82 | + |
| 83 | + if _, dup := featureExists[key]; dup { |
| 84 | + return fmt.Errorf("profiles: profile %q has duplicate feature %q", profile.Name, key) |
| 85 | + } |
| 86 | + |
| 87 | + featureExists[key] = true |
| 88 | + |
| 89 | + // now validate this key with that profile having list of keys already with it. |
| 90 | + } |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func validateProfile(i int, profile Profile, profileExist map[string]bool) error { |
| 95 | + pfName := strings.TrimSpace(profile.Name) |
| 96 | + if pfName == "" { |
| 97 | + return fmt.Errorf("profiles: profile at index %d has empty name", i) |
| 98 | + } |
| 99 | + |
| 100 | + if _, exists := profileExist[pfName]; exists { |
| 101 | + return fmt.Errorf("profiles: duplicate profile name %q", pfName) |
| 102 | + } |
| 103 | + |
| 104 | + profileExist[pfName] = true |
| 105 | + |
| 106 | + if len(profile.Features) == 0 { |
| 107 | + return fmt.Errorf("profiles: profile %q has no features", pfName) |
| 108 | + } |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
0 commit comments