|
| 1 | +package configfile |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + "sigs.k8s.io/yaml" |
| 9 | +) |
| 10 | + |
| 11 | +type E2EConfig struct { |
| 12 | + Common CommonConfig `json:"common" yaml:"common"` |
| 13 | + GitHub GitHubConfig `json:"github" yaml:"github"` |
| 14 | + GitHubEnterprise GitHubEnterpriseConfig `json:"github_enterprise" yaml:"github_enterprise"` |
| 15 | + GitLab GitLabConfig `json:"gitlab" yaml:"gitlab"` |
| 16 | + Gitea GiteaConfig `json:"gitea" yaml:"gitea"` |
| 17 | + BitbucketCloud BitbucketCloudConfig `json:"bitbucket_cloud" yaml:"bitbucket_cloud"` |
| 18 | + BitbucketServer BitbucketServerConfig `json:"bitbucket_server" yaml:"bitbucket_server"` |
| 19 | +} |
| 20 | + |
| 21 | +type CommonConfig struct { |
| 22 | + ControllerURL string `env:"TEST_EL_URL" json:"controller_url" yaml:"controller_url"` |
| 23 | + WebhookSecret string `env:"TEST_EL_WEBHOOK_SECRET" json:"webhook_secret" yaml:"webhook_secret"` |
| 24 | + NoCleanup string `env:"TEST_NOCLEANUP" json:"no_cleanup" yaml:"no_cleanup"` |
| 25 | +} |
| 26 | + |
| 27 | +type GitHubConfig struct { |
| 28 | + APIURL string `env:"TEST_GITHUB_API_URL" json:"api_url" yaml:"api_url"` |
| 29 | + Token string `env:"TEST_GITHUB_TOKEN" json:"token" yaml:"token"` |
| 30 | + RepoOwnerGithubApp string `env:"TEST_GITHUB_REPO_OWNER_GITHUBAPP" json:"repo_owner_githubapp" yaml:"repo_owner_githubapp"` |
| 31 | + RepoOwnerWebhook string `env:"TEST_GITHUB_REPO_OWNER_WEBHOOK" json:"repo_owner_webhook" yaml:"repo_owner_webhook"` |
| 32 | + RepoInstallationID string `env:"TEST_GITHUB_REPO_INSTALLATION_ID" json:"repo_installation_id" yaml:"repo_installation_id"` |
| 33 | + PrivateTaskName string `env:"TEST_GITHUB_PRIVATE_TASK_NAME" json:"private_task_name" yaml:"private_task_name"` |
| 34 | + PrivateTaskURL string `env:"TEST_GITHUB_PRIVATE_TASK_URL" json:"private_task_url" yaml:"private_task_url"` |
| 35 | +} |
| 36 | + |
| 37 | +type GitHubEnterpriseConfig struct { |
| 38 | + APIURL string `env:"TEST_GITHUB_SECOND_API_URL" json:"api_url" yaml:"api_url"` |
| 39 | + Token string `env:"TEST_GITHUB_SECOND_TOKEN" json:"token" yaml:"token"` |
| 40 | + ControllerURL string `env:"TEST_GITHUB_SECOND_EL_URL" json:"controller_url" yaml:"controller_url"` |
| 41 | + RepoOwnerGithubApp string `env:"TEST_GITHUB_SECOND_REPO_OWNER_GITHUBAPP" json:"repo_owner_githubapp" yaml:"repo_owner_githubapp"` |
| 42 | + RepoInstallationID string `env:"TEST_GITHUB_SECOND_REPO_INSTALLATION_ID" json:"repo_installation_id" yaml:"repo_installation_id"` |
| 43 | +} |
| 44 | + |
| 45 | +type GitLabConfig struct { |
| 46 | + APIURL string `env:"TEST_GITLAB_API_URL" json:"api_url" yaml:"api_url"` |
| 47 | + Token string `env:"TEST_GITLAB_TOKEN" json:"token" yaml:"token"` |
| 48 | + ProjectID string `env:"TEST_GITLAB_PROJECT_ID" json:"project_id" yaml:"project_id"` |
| 49 | +} |
| 50 | + |
| 51 | +type GiteaConfig struct { |
| 52 | + APIURL string `env:"TEST_GITEA_API_URL" json:"api_url" yaml:"api_url"` |
| 53 | + InternalURL string `env:"TEST_GITEA_INTERNAL_URL" json:"internal_url" yaml:"internal_url"` |
| 54 | + Password string `env:"TEST_GITEA_PASSWORD" json:"password" yaml:"password"` |
| 55 | + Username string `env:"TEST_GITEA_USERNAME" json:"username" yaml:"username"` |
| 56 | + RepoOwner string `env:"TEST_GITEA_REPO_OWNER" json:"repo_owner" yaml:"repo_owner"` |
| 57 | + SmeeURL string `env:"TEST_GITEA_SMEEURL" json:"smee_url" yaml:"smee_url"` |
| 58 | +} |
| 59 | + |
| 60 | +type BitbucketCloudConfig struct { |
| 61 | + APIURL string `env:"TEST_BITBUCKET_CLOUD_API_URL" json:"api_url" yaml:"api_url"` |
| 62 | + User string `env:"TEST_BITBUCKET_CLOUD_USER" json:"user" yaml:"user"` |
| 63 | + Token string `env:"TEST_BITBUCKET_CLOUD_TOKEN" json:"token" yaml:"token"` |
| 64 | + E2ERepository string `env:"TEST_BITBUCKET_CLOUD_E2E_REPOSITORY" json:"e2e_repository" yaml:"e2e_repository"` |
| 65 | +} |
| 66 | + |
| 67 | +type BitbucketServerConfig struct { |
| 68 | + APIURL string `env:"TEST_BITBUCKET_SERVER_API_URL" json:"api_url" yaml:"api_url"` |
| 69 | + User string `env:"TEST_BITBUCKET_SERVER_USER" json:"user" yaml:"user"` |
| 70 | + Token string `env:"TEST_BITBUCKET_SERVER_TOKEN" json:"token" yaml:"token"` |
| 71 | + E2ERepository string `env:"TEST_BITBUCKET_SERVER_E2E_REPOSITORY" json:"e2e_repository" yaml:"e2e_repository"` |
| 72 | + WebhookSecret string `env:"TEST_BITBUCKET_SERVER_WEBHOOK_SECRET" json:"webhook_secret" yaml:"webhook_secret"` |
| 73 | +} |
| 74 | + |
| 75 | +// LoadConfig reads a YAML config file and sets environment variables for any |
| 76 | +// field that has an `env` struct tag. Existing environment variables take |
| 77 | +// precedence over values from the config file. |
| 78 | +func LoadConfig(path string) error { |
| 79 | + data, err := os.ReadFile(path) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("reading config file %s: %w", path, err) |
| 82 | + } |
| 83 | + |
| 84 | + var cfg E2EConfig |
| 85 | + if err := yaml.Unmarshal(data, &cfg); err != nil { |
| 86 | + return fmt.Errorf("parsing config file %s: %w", path, err) |
| 87 | + } |
| 88 | + |
| 89 | + setEnvsFromStruct(reflect.ValueOf(cfg)) |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +// setEnvsFromStruct walks all fields of a struct (recursing into nested |
| 94 | +// structs) and calls os.Setenv for each field that has an `env` tag, but only |
| 95 | +// when the environment variable is not already set and the YAML value is |
| 96 | +// non-empty. |
| 97 | +func setEnvsFromStruct(v reflect.Value) { |
| 98 | + t := v.Type() |
| 99 | + for i := range t.NumField() { |
| 100 | + field := t.Field(i) |
| 101 | + value := v.Field(i) |
| 102 | + |
| 103 | + if field.Type.Kind() == reflect.Struct { |
| 104 | + setEnvsFromStruct(value) |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + envKey := field.Tag.Get("env") |
| 109 | + if envKey == "" { |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + yamlVal := value.String() |
| 114 | + if yamlVal == "" { |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + if _, exists := os.LookupEnv(envKey); exists { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + os.Setenv(envKey, yamlVal) |
| 123 | + } |
| 124 | +} |
0 commit comments