Skip to content

Commit c451fc7

Browse files
authored
fix: init env vars (#4586)
## Changes <!-- Brief summary of your changes that is easy to understand --> ## Why <!-- Why are these changes needed? Provide the context that the reviewer might be missing. For example, were there any decisions behind the change that are not reflected in the code itself? --> ## Tests <!-- How have you tested the changes? --> <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. --> --------- Co-authored-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
1 parent 61a1131 commit c451fc7

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

cmd/apps/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ type templateVars struct {
268268
BundleVariables string
269269
BundleResources string
270270
TargetVariables string
271+
AppEnv string
271272
DotEnv string
272273
DotEnvExample string
273274
}
@@ -772,6 +773,7 @@ func runCreate(ctx context.Context, opts createOptions) error {
772773
BundleVariables: bundleVars,
773774
BundleResources: bundleRes,
774775
TargetVariables: targetVars,
776+
AppEnv: generator.GenerateAppEnv(selectedPluginList, genConfig),
775777
DotEnv: generator.GenerateDotEnv(selectedPluginList, genConfig),
776778
DotEnvExample: generator.GenerateDotEnvExample(selectedPluginList),
777779
}
@@ -1159,6 +1161,7 @@ func templateData(vars templateVars) map[string]string {
11591161
"variables": vars.BundleVariables,
11601162
"resources": vars.BundleResources,
11611163
"target_variables": vars.TargetVariables,
1164+
"app_env": vars.AppEnv,
11621165
"dotenv": vars.DotEnv,
11631166
"dotenv_example": vars.DotEnvExample,
11641167
}

libs/apps/generator/generator.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,44 @@ func GenerateDotEnvExample(plugins []manifest.Plugin) string {
221221
return strings.Join(lines, "\n")
222222
}
223223

224+
// GenerateAppEnv generates the env entries for app.yaml.
225+
// Each resource field with an Env mapping produces a YAML list entry with
226+
// name (the env var) and valueFrom (the resource key in databricks.yml resources).
227+
// Includes both required resources and optional resources that have values.
228+
func GenerateAppEnv(plugins []manifest.Plugin, cfg Config) string {
229+
var lines []string
230+
231+
for _, p := range plugins {
232+
for _, r := range p.Resources.Required {
233+
lines = append(lines, appEnvLines(r)...)
234+
}
235+
for _, r := range p.Resources.Optional {
236+
if hasResourceValues(r, cfg) {
237+
lines = append(lines, appEnvLines(r)...)
238+
}
239+
}
240+
}
241+
242+
return strings.Join(lines, "\n")
243+
}
244+
245+
// appEnvLines returns app.yaml env entries for a resource.
246+
// Each field with an Env produces "- name: <ENV>\n valueFrom: <resourceKey>".
247+
func appEnvLines(r manifest.Resource) []string {
248+
var lines []string
249+
for _, fieldName := range r.FieldNames() {
250+
field := r.Fields[fieldName]
251+
if field.Env == "" || !validEnvVar.MatchString(field.Env) {
252+
continue
253+
}
254+
lines = append(lines,
255+
" - name: "+field.Env,
256+
" valueFrom: "+r.Key(),
257+
)
258+
}
259+
return lines
260+
}
261+
224262
// appResourceSpec defines how a manifest resource type maps to DABs AppResource YAML.
225263
type appResourceSpec struct {
226264
yamlKey string // DABs YAML key under the resource entry (e.g., "sql_warehouse", "uc_securable")

0 commit comments

Comments
 (0)