Skip to content

Conversation

@marwen-abid
Copy link
Contributor

What

Environment file loading improvements:

  • Added a new utility (cmd/utils/env_loader.go) to load environment variables from a file, prioritizing the --env-file flag, then the ENV_FILE environment variable, and finally falling back to .env in the working directory. This replaces the previous single .env loading logic.
  • Updated the root command to recognize the --env-file flag, ensuring Cobra-based CLI parsing works seamlessly with environment file selection.

Setup and config generation improvements:

  • Updated the setup tool to generate environment files by merging .env.example with user-specified configuration, ensuring all expected keys are present and custom values are correctly applied.

Why

  • Provide a better experience to developers working on the stellar disbursement platform backend repository. Allow them to generate a full .env file that can be used for running Docker compose and start the application directly.

Checklist

  • Title follows SDP-1234: Add new feature or Chore: Refactor package xyz format. The Jira ticket code was included if available.
  • PR has a focused scope and doesn't mix features with refactoring
  • Tests are included (if applicable)
  • CHANGELOG.md is updated (if applicable)
  • CONFIG/SECRETS changes are updated in helmcharts and deployments (if applicable)
  • Preview deployment works as expected
  • Ready for production

@marwen-abid marwen-abid requested a review from Copilot December 2, 2025 05:18
@marwen-abid marwen-abid self-assigned this Dec 2, 2025
@marwen-abid marwen-abid temporarily deployed to Receiver Registration - E2E Integration Tests (Stellar) December 2, 2025 05:18 — with GitHub Actions Inactive
@marwen-abid marwen-abid temporarily deployed to Internal SEP Tests December 2, 2025 05:18 — with GitHub Actions Inactive
@stellar-jenkins
Copy link

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds configurable environment file loading to support multiple deployment contexts, improving the developer experience for working with the Stellar Disbursement Platform backend.

Key Changes:

  • Introduced a new environment file loading utility with precedence order: --env-file flag > ENV_FILE environment variable > .env in working directory
  • Updated the setup tool to generate complete environment files by merging .env.example with user-specified configuration
  • Enhanced documentation with three distinct deployment options (Setup Wizard, Docker Compose, Local Go)

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
cmd/utils/env_loader.go New utility implementing configurable env file loading with priority-based path resolution
cmd/utils/env_loader_test.go Comprehensive test coverage for the new env loader utility
cmd/utils/global_options.go Added EnvFile field to store the env file path flag value
cmd/root.go Registered --env-file flag with Cobra to prevent unknown flag errors
main.go Replaced direct godotenv.Load() with the new LoadEnvFile() utility, with explicit error handling
tools/sdp-setup/internal/config/env.go Updated Write function to merge .env.example with config values for complete env file generation
dev/.env.example Added default tenant configuration variables for initial tenant setup
dev/README.md Expanded documentation with three deployment options and clearer instructions for local development
Comments suppressed due to low confidence (1)

tools/sdp-setup/internal/config/env.go:275

  • The updated Write function now has more complex logic with .env.example merging, but there are no tests covering this new behavior. Consider adding tests to verify:
  1. Correct merging when .env.example exists
  2. Correct fallback when .env.example doesn't exist
  3. Override behavior - ensuring configMap values take precedence over .env.example values
  4. File creation in non-existent directories

Since other parts of the codebase (e.g., cmd/utils/, internal/utils/) have comprehensive test coverage, this function should also be tested to maintain code quality standards.

func Write(cfg Config, path string) error {
	envConfigDir := filepath.Dir(path)
	if err := os.MkdirAll(envConfigDir, 0o755); err != nil {
		return fmt.Errorf("creating directory for %s: %w", path, err)
	}

	singleTenantModeStr := "true"
	if !cfg.SingleTenantMode {
		singleTenantModeStr = "false"
	}

	// 1. Start with the configuration values we want to set
	configMap := map[string]string{
		"NETWORK_TYPE":                               cfg.NetworkType,
		"NETWORK_PASSPHRASE":                         cfg.NetworkPassphrase,
		"HORIZON_URL":                                cfg.HorizonURL,
		"DATABASE_NAME":                              cfg.DatabaseName,
		"DISABLE_MFA":                                cfg.DisableMFA,
		"SINGLE_TENANT_MODE":                         singleTenantModeStr,
		"SETUP_NAME":                                 cfg.SetupName,
		"SEP10_SIGNING_PUBLIC_KEY":                   cfg.SEP10PublicKey,
		"SEP10_SIGNING_PRIVATE_KEY":                  cfg.SEP10PrivateKey,
		"DISTRIBUTION_PUBLIC_KEY":                    cfg.DistributionPublic,
		"DISTRIBUTION_SEED":                          cfg.DistributionSeed,
		"CHANNEL_ACCOUNT_ENCRYPTION_PASSPHRASE":      cfg.DistributionSeed,
		"DISTRIBUTION_ACCOUNT_ENCRYPTION_PASSPHRASE": cfg.DistributionSeed,
		"USE_HTTPS":                                  strconv.FormatBool(cfg.UseHTTPS),
		"SDP_UI_BASE_URL":                            cfg.FrontendBaseURL("localhost"),
		"BASE_URL":                                   "http://localhost:8000",
		"DATABASE_URL":                               fmt.Sprintf("postgres://postgres@db:5432/%s?sslmode=disable", cfg.DatabaseName),
	}

	if cfg.NetworkType == "pubnet" {
		configMap["TENANT_XLM_BOOTSTRAP_AMOUNT"] = "1"
		configMap["NUM_CHANNEL_ACCOUNTS"] = "1"
	}

	// 2. Load .env.example to use as a base
	examplePath := filepath.Join(envConfigDir, ".env.example")

	finalMap := make(map[string]string)

	if exampleMap, err := godotenv.Read(examplePath); err == nil {
		finalMap = exampleMap
	}

	// 3. Override with our configuration values
	for k, v := range configMap {
		finalMap[k] = v
	}

	if err := godotenv.Write(finalMap, path); err != nil {
		return fmt.Errorf("writing env file %s: %w", path, err)
	}

	return nil

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@marwen-abid marwen-abid force-pushed the feat/SDP-1934-configurable-env-file branch from 6b06c6b to 4b6fa17 Compare December 2, 2025 17:24
@marwen-abid marwen-abid temporarily deployed to Receiver Registration - E2E Integration Tests (Stellar) December 2, 2025 17:24 — with GitHub Actions Inactive
@marwen-abid marwen-abid temporarily deployed to Internal SEP Tests December 2, 2025 17:24 — with GitHub Actions Inactive
@stellar-jenkins
Copy link

Copy link
Contributor

@philipliu philipliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@marwen-abid marwen-abid temporarily deployed to Internal SEP Tests December 2, 2025 19:12 — with GitHub Actions Inactive
@marwen-abid marwen-abid temporarily deployed to Receiver Registration - E2E Integration Tests (Stellar) December 2, 2025 19:12 — with GitHub Actions Inactive
@marwen-abid marwen-abid force-pushed the feat/SDP-1934-configurable-env-file branch from 864ddce to c92d3b7 Compare December 2, 2025 19:13
@marwen-abid marwen-abid temporarily deployed to Receiver Registration - E2E Integration Tests (Stellar) December 2, 2025 19:13 — with GitHub Actions Inactive
@marwen-abid marwen-abid temporarily deployed to Internal SEP Tests December 2, 2025 19:13 — with GitHub Actions Inactive
@stellar-jenkins
Copy link

@stellar-jenkins
Copy link

Something went wrong with PR preview build please check

@marwen-abid marwen-abid merged commit 04c07ff into develop Dec 2, 2025
14 of 15 checks passed
@marwen-abid marwen-abid deleted the feat/SDP-1934-configurable-env-file branch December 2, 2025 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants