66 "fmt"
77 "os"
88 "path/filepath"
9+ "slices"
910 "strings"
1011 "sync"
1112 "time"
@@ -64,6 +65,7 @@ type RawConfig struct {
6465 HotReload * HotReloadSection `toml:"hot_reload_section"`
6566 Notifications * Notifications `toml:"notifications"`
6667 StaticTemplateValues map [string ]string `toml:"static_template_values"`
68+ KeysOrder []string `toml:"-"`
6769}
6870
6971type HotReloadSection struct {
@@ -172,6 +174,7 @@ type Profile struct {
172174 IsFallbackProfile bool `toml:"-"`
173175 PostApplyExec * string `toml:"post_apply_exec"`
174176 PreApplyExec * string `toml:"pre_apply_exec"`
177+ KeyOrder int `toml:"-"`
175178}
176179
177180type PowerStateType int
@@ -246,12 +249,18 @@ func Load(configPath string) (*RawConfig, error) {
246249 logrus .Debugf ("Config contents: %s" , contents )
247250
248251 var config RawConfig
249- if _ , err := toml .DecodeFile (configPath , & config ); err != nil {
252+ m , err := toml .DecodeFile (configPath , & config )
253+ if err != nil {
250254 return nil , fmt .Errorf ("failed to decode TOML: %w" , err )
251255 }
256+ keys := []string {}
257+ for _ , k := range m .Keys () {
258+ keys = append (keys , strings .Join (k , "." ))
259+ }
252260
253261 config .ConfigPath = absConfig
254262 config .ConfigDirPath = filepath .Dir (config .ConfigPath )
263+ config .KeysOrder = keys
255264
256265 if err := config .Validate (); err != nil {
257266 return nil , fmt .Errorf ("invalid configuration: %w" , err )
@@ -262,6 +271,22 @@ func Load(configPath string) (*RawConfig, error) {
262271 return & config , nil
263272}
264273
274+ // OrderedProfileKeys returns the profile names in the order they appear in the toml file
275+ func (c * RawConfig ) OrderedProfileKeys () []string {
276+ profileNames := make ([]string , 0 , len (c .Profiles ))
277+ for name := range c .Profiles {
278+ profileNames = append (profileNames , name )
279+ }
280+
281+ slices .SortFunc (profileNames , func (a , b string ) int {
282+ orderA := c .Profiles [a ].KeyOrder
283+ orderB := c .Profiles [b ].KeyOrder
284+ return orderA - orderB
285+ })
286+
287+ return profileNames
288+ }
289+
265290func (c * RawConfig ) Validate () error {
266291 if c .ConfigPath == "" {
267292 return errors .New ("config path cant be empty" )
@@ -292,6 +317,8 @@ func (c *RawConfig) Validate() error {
292317 for name , profile := range c .Profiles {
293318 profile .Name = name
294319 profile .IsFallbackProfile = false
320+ profile .KeyOrder = slices .Index (c .KeysOrder , "profiles." + name )
321+ logrus .Debugf ("Profile %s has order %d" , profile .Name , profile .KeyOrder )
295322 if err := profile .Validate (c .ConfigDirPath ); err != nil {
296323 return fmt .Errorf ("profile %s validation failed: %w" , name , err )
297324 }
0 commit comments