@@ -11,6 +11,7 @@ package sysdig
1111import  (
1212	"context" 
1313	"fmt" 
14+ 	"unicode" 
1415
1516	"github.com/Jeffail/gabs/v2" 
1617	"github.com/rs/zerolog/log" 
@@ -27,11 +28,50 @@ func GetValueFromTemplate(what *gabs.Container) (string, *gabs.Container) {
2728		fallback  =  nil 
2829	default :
2930		fallback  =  what 
30- 		result  =  "placeholder: "   +   what .String ()
31+ 		result  =  what .String ()
3132	}
3233	return  result , fallback 
3334}
3435
36+ func  capitalize (key  string ) string  {
37+ 	r  :=  []rune (key )
38+ 	r [0 ] =  unicode .ToUpper (r [0 ])
39+ 	return  string (r )
40+ }
41+ 
42+ // updateKey recursively capitalizes the first letter of each key in the input object 
43+ func  updateKeys (inputMap  gabs.Container ) error  {
44+ 	// in this case, the object is probably an array, so update each child 
45+ 	if  len (inputMap .ChildrenMap ()) ==  0  {
46+ 		for  _ , child  :=  range  inputMap .Children () {
47+ 			err  :=  updateKeys (* child )
48+ 			if  err  !=  nil  {
49+ 				return  err 
50+ 			}
51+ 		}
52+ 	} else  {
53+ 		for  key , child  :=  range  inputMap .ChildrenMap () {
54+ 			_ , err  :=  inputMap .Set (child .Data (), capitalize (key ))
55+ 			if  err  !=  nil  {
56+ 				return  fmt .Errorf ("failed to update new key %s" , capitalize (key ))
57+ 			}
58+ 
59+ 			err  =  inputMap .Delete (key )
60+ 			if  err  !=  nil  {
61+ 				return  fmt .Errorf ("failed to delete old key %s"  +  key )
62+ 			}
63+ 
64+ 			// recurse to update child's keys 
65+ 			err  =  updateKeys (* child )
66+ 			if  err  !=  nil  {
67+ 				return  err 
68+ 			}
69+ 		}
70+ 	}
71+ 
72+ 	return  nil 
73+ }
74+ 
3575func  terraformPreModifications (ctx  context.Context , patchedStack  []byte ) ([]byte , error ) {
3676	l  :=  log .Ctx (ctx )
3777	template , err  :=  gabs .ParseJSON (patchedStack )
@@ -56,8 +96,8 @@ func terraformPreModifications(ctx context.Context, patchedStack []byte) ([]byte
5696				}
5797			}
5898
59- 			if  container .Exists ("Environment " ) {
60- 				for  _ , env  :=  range  container .S ("Environment " ).Children () {
99+ 			if  container .Exists ("environment " ) {
100+ 				for  _ , env  :=  range  container .S ("environment " ).Children () {
61101					if  env .Exists ("name" ) {
62102						name , _  :=  env .S ("name" ).Data ().(string )
63103						err  =  env .Delete ("name" )
@@ -81,22 +121,85 @@ func terraformPreModifications(ctx context.Context, patchedStack []byte) ([]byte
81121						}
82122					}
83123				}
124+ 
125+ 				passthrough , _  :=  GetValueFromTemplate (container .S ("environment" ))
126+ 				parsedPassthrough , _  :=  gabs .ParseJSON ([]byte (passthrough ))
127+ 				_ , err  =  container .Set (parsedPassthrough , "Environment" )
128+ 				if  err  !=  nil  {
129+ 					return  nil , fmt .Errorf ("Could not update Environment field: %v" , err )
130+ 				}
131+ 
132+ 				err  =  container .Delete ("environment" )
133+ 				if  err  !=  nil  {
134+ 					return  nil , fmt .Errorf ("could not delete environment in the Container definition: %w" , err )
135+ 				}
84136			}
85137
86138			if  container .Exists ("entryPoint" ) {
87- 				for  _ , arg  :=  range  container .S ("entryPoint" ).Children () {
88- 					passthrough , _  :=  GetValueFromTemplate (arg )
89- 					err  =  container .ArrayAppend (passthrough , "EntryPoint" )
90- 					if  err  !=  nil  {
91- 						return  nil , fmt .Errorf ("Could not append entrypoint values to EntryPoint: %v" , err )
92- 					}
139+ 				passthrough , _  :=  GetValueFromTemplate (container .S ("entryPoint" ))
140+ 				parsedPassthrough , _  :=  gabs .ParseJSON ([]byte (passthrough ))
141+ 				_ , err  =  container .Set (parsedPassthrough , "EntryPoint" )
142+ 				if  err  !=  nil  {
143+ 					return  nil , fmt .Errorf ("Could not update EntryPoint field: %v" , err )
93144				}
94145
95146				err  =  container .Delete ("entryPoint" )
96147				if  err  !=  nil  {
97148					return  nil , fmt .Errorf ("could not delete entryPoint in the Container definition: %w" , err )
98149				}
99150			}
151+ 
152+ 			if  container .Exists ("command" ) {
153+ 				passthrough , _  :=  GetValueFromTemplate (container .S ("command" ))
154+ 				parsedPassthrough , _  :=  gabs .ParseJSON ([]byte (passthrough ))
155+ 				_ , err  =  container .Set (parsedPassthrough , "Command" )
156+ 				if  err  !=  nil  {
157+ 					return  nil , fmt .Errorf ("Could not update Command field: %v" , err )
158+ 				}
159+ 
160+ 				err  =  container .Delete ("command" )
161+ 				if  err  !=  nil  {
162+ 					return  nil , fmt .Errorf ("could not delete command in the Container definition: %w" , err )
163+ 				}
164+ 			}
165+ 
166+ 			if  container .Exists ("volumesFrom" ) {
167+ 				err  =  updateKeys (* container .S ("volumesFrom" ))
168+ 				if  err  !=  nil  {
169+ 					return  nil , fmt .Errorf ("could not update volumesFrom items: %v" , err )
170+ 				}
171+ 
172+ 				passthrough , _  :=  GetValueFromTemplate (container .S ("volumesFrom" ))
173+ 				parsedPassthrough , _  :=  gabs .ParseJSON ([]byte (passthrough ))
174+ 				_ , err  =  container .Set (parsedPassthrough , "VolumesFrom" )
175+ 				if  err  !=  nil  {
176+ 					return  nil , fmt .Errorf ("could not update VolumesFrom field: %v" , err )
177+ 				}
178+ 
179+ 				err  =  container .Delete ("volumesFrom" )
180+ 				if  err  !=  nil  {
181+ 					return  nil , fmt .Errorf ("could not delete volumesFrom in the container definition: %w" , err )
182+ 				}
183+ 			}
184+ 
185+ 			if  container .Exists ("linuxParameters" ) {
186+ 				err  =  updateKeys (* container .S ("linuxParameters" ))
187+ 				if  err  !=  nil  {
188+ 					return  nil , fmt .Errorf ("could not update linuxParameters items: %v" , err )
189+ 				}
190+ 
191+ 				passthrough , _  :=  GetValueFromTemplate (container .S ("linuxParameters" ))
192+ 				parsedPassthrough , _  :=  gabs .ParseJSON ([]byte (passthrough ))
193+ 				_ , err  =  container .Set (parsedPassthrough , "LinuxParameters" )
194+ 				if  err  !=  nil  {
195+ 					return  nil , fmt .Errorf ("could not update LinuxParameters field: %v" , err )
196+ 				}
197+ 
198+ 				err  =  container .Delete ("linuxParameters" )
199+ 				if  err  !=  nil  {
200+ 					return  nil , fmt .Errorf ("could not delete linuxParameters in the COntainer definition: %w" , err )
201+ 				}
202+ 			}
100203		}
101204	}
102205
0 commit comments