Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ func SetupCommands(ctx context.Context, version string) {

configCmd.AddCommand(configListCmd)

configCmd.AddCommand(configResolveCmd)

RootCmd.AddCommand(configCmd)

RootCmd.AddCommand(setupComposeCommand())
Expand Down Expand Up @@ -970,6 +972,34 @@ var configListCmd = &cobra.Command{
},
}

var configResolveCmd = &cobra.Command{
Use: "resolve",
Annotations: authNeededAnnotation,
Args: cobra.NoArgs,
Aliases: []string{"final"},
Short: "Show the final resolved config for the project",
RunE: func(cmd *cobra.Command, args []string) error {
loader := configureLoader(cmd)

provider, err := newProviderChecked(cmd.Context(), loader)
if err != nil {
return err
}

project, err := loader.LoadProject(cmd.Context())
if err != nil {
return err
}

config, err := provider.ListConfig(cmd.Context(), &defangv1.ListConfigsRequest{Project: project.Name})
if err != nil {
return err
}

return compose.PrintConfigResolutionSummary(*project, config.Names)
},
}

var debugCmd = &cobra.Command{
Use: "debug [SERVICE...]",
Annotations: authNeededAnnotation,
Expand Down
99 changes: 99 additions & 0 deletions src/pkg/cli/compose/configResolution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package compose

import (
"sort"

"github.com/DefangLabs/defang/src/pkg/term"
)

type configOutput struct {
Service string `json:"service"`
Name string `json:"name"`
Value string `json:"value,omitempty"`
Source Source `json:"source,omitempty"`
}

type Source int

const (
SourceUnknown Source = iota
SourceComposeFile
SourceDefangConfig
SourceDefangAndComposeFile
)

var sourceNames = map[Source]string{
SourceUnknown: "unknown",
SourceComposeFile: "compose_file",
SourceDefangConfig: "defang_config",
SourceDefangAndComposeFile: "compose_file and defang_config",
}

func (s Source) String() string {
if name, ok := sourceNames[s]; ok {
return name
}
return sourceNames[SourceUnknown]
}

// determineConfigSource determines the source of an environment variable
// and returns the appropriate source type and value to display
func determineConfigSource(envKey string, envValue *string, defangConfigs map[string]string) (Source, string) {
// If the key itself is a defang config, mask it
if _, isDefangConfig := defangConfigs[envKey]; isDefangConfig {
return SourceDefangConfig, configMaskedValue
}

// If value is nil, it's from the compose file with empty value
if envValue == nil {
return SourceComposeFile, ""
}

// Check if the value contains references to defang configs
interpolatedVariables := DetectInterpolationVariables(*envValue)
if len(interpolatedVariables) > 0 {
for _, varName := range interpolatedVariables {
if _, isDefangConfig := defangConfigs[varName]; isDefangConfig {
return SourceDefangAndComposeFile, *envValue
}
}
}

// Otherwise, it's from the compose file
return SourceComposeFile, *envValue
}

const configMaskedValue = "*****"

func PrintConfigResolutionSummary(project Project, defangConfig []string) error {
configset := make(map[string]string)
for _, name := range defangConfig {
configset[name] = ""
}

projectEnvVars := []configOutput{}

for serviceName, service := range project.Services {
for envKey, envValue := range service.Environment {
source, value := determineConfigSource(envKey, envValue, configset)
projectEnvVars = append(projectEnvVars, configOutput{
Service: serviceName,
Name: envKey,
Value: value,
Source: source,
})
}
}

// Sort by Service, then by Name within each service
sort.Slice(projectEnvVars, func(i, j int) bool {
if projectEnvVars[i].Service != projectEnvVars[j].Service {
return projectEnvVars[i].Service < projectEnvVars[j].Service
}
return projectEnvVars[i].Name < projectEnvVars[j].Name
})

term.Println("\033[1mENVIRONMENT VARIABLES RESOLUTION SUMMARY:\033[0m")

return term.Table(projectEnvVars, "Service", "Name", "Value", "Source")
}
30 changes: 21 additions & 9 deletions src/pkg/cli/compose/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,20 @@ func getResourceReservations(r composeTypes.Resources) *composeTypes.Resource {
// Copied from shared/utils.ts but slightly modified to remove the negative-lookahead assertion
var interpolationRegex = regexp.MustCompile(`(?i)\$(\$)|\$(?:{([^}]+)}|([_a-z][_a-z0-9]*))|([^$]+)`) // [1] escaped dollar, [2] curly braces, [3] variable name, [4] literal

func DetectInterpolationVariables(value string) []string {
var names []string
// check for variables used during interpolation
for _, match := range interpolationRegex.FindAllStringSubmatch(value, -1) {
if match[2] != "" {
names = append(names, match[2])
}
if match[3] != "" {
names = append(names, match[3])
}
}
return names
}

func ValidateProjectConfig(ctx context.Context, composeProject *composeTypes.Project, listConfigNamesFunc ListConfigNamesFunc) error {
var names []string
// make list of secrets
Expand All @@ -444,15 +458,8 @@ func ValidateProjectConfig(ctx context.Context, composeProject *composeTypes.Pro
names = append(names, key)
continue
}
// check for variables used during interpolation
for _, match := range interpolationRegex.FindAllStringSubmatch(*value, -1) {
if match[2] != "" {
names = append(names, match[2])
}
if match[3] != "" {
names = append(names, match[3])
}
}
detectedNames := DetectInterpolationVariables(*value)
names = append(names, detectedNames...)
}
}

Expand All @@ -465,6 +472,11 @@ func ValidateProjectConfig(ctx context.Context, composeProject *composeTypes.Pro
return err
}

err = PrintConfigResolutionSummary(*composeProject, configs)
if err != nil {
return err
}

// Deduplicate (sort + uniq)
slices.Sort(names)
names = slices.Compact(names)
Expand Down
Loading