@@ -22,9 +22,14 @@ import (
2222 "github.com/compose-spec/compose-go/v2/types"
2323)
2424
25- // Will update the environment variables for the format {- VAR} (without interpolation)
26- // This function should resolve context environment vars for include (passed in env_file)
27- func resolveServicesEnvironment (dict map [string ]any , config types.ConfigDetails ) {
25+ // ResolveEnvironment update the environment variables for the format {- VAR} (without interpolation)
26+ func ResolveEnvironment (dict map [string ]any , environment types.Mapping ) {
27+ resolveServicesEnvironment (dict , environment )
28+ resolveSecretsEnvironment (dict , environment )
29+ resolveConfigsEnvironment (dict , environment )
30+ }
31+
32+ func resolveServicesEnvironment (dict map [string ]any , environment types.Mapping ) {
2833 services , ok := dict ["services" ].(map [string ]any )
2934 if ! ok {
3035 return
@@ -45,7 +50,7 @@ func resolveServicesEnvironment(dict map[string]any, config types.ConfigDetails)
4550 if ! ok {
4651 continue
4752 }
48- if found , ok := config . Environment [varEnv ]; ok {
53+ if found , ok := environment [varEnv ]; ok {
4954 envs = append (envs , fmt .Sprintf ("%s=%s" , varEnv , found ))
5055 } else {
5156 // either does not exist or it was already resolved in interpolation
@@ -57,3 +62,49 @@ func resolveServicesEnvironment(dict map[string]any, config types.ConfigDetails)
5762 }
5863 dict ["services" ] = services
5964}
65+
66+ func resolveSecretsEnvironment (dict map [string ]any , environment types.Mapping ) {
67+ secrets , ok := dict ["secrets" ].(map [string ]any )
68+ if ! ok {
69+ return
70+ }
71+
72+ for name , cfg := range secrets {
73+ secret , ok := cfg .(map [string ]any )
74+ if ! ok {
75+ continue
76+ }
77+ env , ok := secret ["environment" ].(string )
78+ if ! ok {
79+ continue
80+ }
81+ if found , ok := environment [env ]; ok {
82+ secret ["content" ] = found
83+ }
84+ secrets [name ] = secret
85+ }
86+ dict ["secrets" ] = secrets
87+ }
88+
89+ func resolveConfigsEnvironment (dict map [string ]any , environment types.Mapping ) {
90+ configs , ok := dict ["configs" ].(map [string ]any )
91+ if ! ok {
92+ return
93+ }
94+
95+ for name , cfg := range configs {
96+ config , ok := cfg .(map [string ]any )
97+ if ! ok {
98+ continue
99+ }
100+ env , ok := config ["environment" ].(string )
101+ if ! ok {
102+ continue
103+ }
104+ if found , ok := environment [env ]; ok {
105+ config ["content" ] = found
106+ }
107+ configs [name ] = config
108+ }
109+ dict ["configs" ] = configs
110+ }
0 commit comments