package internal import ( "errors" "os" "gopkg.in/yaml.v3" ) type Config struct { Repository string `yaml:"repository"` Owner string `yaml:"owner"` BaseBranch string `yaml:"base_branch"` MergeBranch string `yaml:"merge_branch"` CommitBranch string `yaml:"commit_branch"` ApkoLocations []string `yaml:"apko_locations"` DryRun bool `yaml:"dry_run"` GHPrivateKeyPath string `yaml:"gh_app_private_key_path"` GHPrivateKey string `yaml:"gh_app_private_key"` GHAppID int64 `yaml:"gh_app_id"` GHInstallationID int64 `yaml:"gh_installation_id"` } func NewConfigFromPath(path string) (*Config, error) { configBytes, err := os.ReadFile(path) if err != nil { return nil, err } config := &Config{} if err := yaml.Unmarshal(configBytes, config); err != nil { return nil, err } // ensure required fields if err := validateConfig(config); err != nil { return nil, err } return config, nil } func validateConfig(config *Config) error { // ensure required fields are filled out var missingFieldsErrors []error if config.Owner == "" { missingFieldsErrors = append(missingFieldsErrors, errors.New("required field owner missing")) } if config.Repository == "" { missingFieldsErrors = append(missingFieldsErrors, errors.New("required field repository missing")) } if config.BaseBranch == "" { missingFieldsErrors = append(missingFieldsErrors, errors.New("required field base-branch missing")) } if config.MergeBranch == "" { missingFieldsErrors = append(missingFieldsErrors, errors.New("required field merge-branch missing")) } if config.CommitBranch == "" { missingFieldsErrors = append(missingFieldsErrors, errors.New("required field commit-branch missing")) } if config.ApkoLocations == nil { missingFieldsErrors = append(missingFieldsErrors, errors.New("required field apko-locations missing")) } if config.GHPrivateKey == "" && config.GHPrivateKeyPath == "" { missingFieldsErrors = append(missingFieldsErrors, errors.New("either gh-app-private-key or gh-app-private-key-path must be set for Github app authentication")) } if config.GHPrivateKey != "" && config.GHPrivateKeyPath != "" { missingFieldsErrors = append(missingFieldsErrors, errors.New("gh-app-private-key-path and gh-app-private-key cannot both be set")) } if config.GHAppID == 0 { missingFieldsErrors = append(missingFieldsErrors, errors.New("required field gh-app-id missing")) } if len(missingFieldsErrors) == 0 { return nil } return errors.Join(missingFieldsErrors...) }