@@ -7,11 +7,7 @@ import (
77
88 "github.com/goccy/go-yaml"
99
10- v0 "github.com/docker/cagent/pkg/config/v0"
11- v1 "github.com/docker/cagent/pkg/config/v1"
12- v2 "github.com/docker/cagent/pkg/config/v2"
13- latest "github.com/docker/cagent/pkg/config/v3" //nolint:staticcheck // This is used everywhere we reference the latest version
14- v3 "github.com/docker/cagent/pkg/config/v3" //nolint:staticcheck // This is used for migrations to v3
10+ "github.com/docker/cagent/pkg/config/latest"
1511 "github.com/docker/cagent/pkg/environment"
1612 "github.com/docker/cagent/pkg/filesystem"
1713)
@@ -76,57 +72,24 @@ func CheckRequiredEnvVars(ctx context.Context, cfg *latest.Config, modelsGateway
7672}
7773
7874func parseCurrentVersion (data []byte , version string ) (any , error ) {
79- options := []yaml.DecodeOption {yaml .Strict ()}
80-
81- switch version {
82- case v0 .Version :
83- var cfg v0.Config
84- err := yaml .UnmarshalWithOptions (data , & cfg , options ... )
85- return cfg , err
86- case v1 .Version :
87- var cfg v1.Config
88- err := yaml .UnmarshalWithOptions (data , & cfg , options ... )
89- return cfg , err
90- case v2 .Version :
91- var cfg v2.Config
92- err := yaml .UnmarshalWithOptions (data , & cfg , options ... )
93- return cfg , err
94- case v3 .Version :
95- var cfg v3.Config
96- err := yaml .UnmarshalWithOptions (data , & cfg , options ... )
97- return cfg , err
98- default :
75+ parser , found := Parsers ()[version ]
76+ if ! found {
9977 return nil , fmt .Errorf ("unsupported config version: %v" , version )
10078 }
79+ return parser (data )
10180}
10281
10382func migrateToLatestConfig (c any ) (latest.Config , error ) {
10483 var err error
105- for {
106- if old , ok := c .(v0.Config ); ok {
107- c , err = v1 .UpgradeFrom (old )
108- if err != nil {
109- return latest.Config {}, err
110- }
111- continue
112- }
113- if old , ok := c .(v1.Config ); ok {
114- c , err = v2 .UpgradeFrom (old )
115- if err != nil {
116- return latest.Config {}, err
117- }
118- continue
119- }
120- if old , ok := c .(v2.Config ); ok {
121- c , err = v3 .UpgradeFrom (old )
122- if err != nil {
123- return latest.Config {}, err
124- }
125- continue
126- }
12784
128- return c .(latest.Config ), nil
85+ for _ , upgrade := range Upgrades () {
86+ c , err = upgrade (c )
87+ if err != nil {
88+ return latest.Config {}, err
89+ }
12990 }
91+
92+ return c .(latest.Config ), nil
13093}
13194
13295func validateConfig (cfg * latest.Config ) error {
0 commit comments