...

Source file src/edge-infra.dev/hack/tools/apko-updater-bot/internal/config.go

Documentation: edge-infra.dev/hack/tools/apko-updater-bot/internal

     1  package internal
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	"gopkg.in/yaml.v3"
     8  )
     9  
    10  type Config struct {
    11  	Repository       string   `yaml:"repository"`
    12  	Owner            string   `yaml:"owner"`
    13  	BaseBranch       string   `yaml:"base_branch"`
    14  	MergeBranch      string   `yaml:"merge_branch"`
    15  	CommitBranch     string   `yaml:"commit_branch"`
    16  	ApkoLocations    []string `yaml:"apko_locations"`
    17  	DryRun           bool     `yaml:"dry_run"`
    18  	GHPrivateKeyPath string   `yaml:"gh_app_private_key_path"`
    19  	GHPrivateKey     string   `yaml:"gh_app_private_key"`
    20  	GHAppID          int64    `yaml:"gh_app_id"`
    21  	GHInstallationID int64    `yaml:"gh_installation_id"`
    22  }
    23  
    24  func NewConfigFromPath(path string) (*Config, error) {
    25  	configBytes, err := os.ReadFile(path)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	config := &Config{}
    30  	if err := yaml.Unmarshal(configBytes, config); err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	// ensure required fields
    35  	if err := validateConfig(config); err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return config, nil
    40  }
    41  
    42  func validateConfig(config *Config) error {
    43  	// ensure required fields are filled out
    44  	var missingFieldsErrors []error
    45  
    46  	if config.Owner == "" {
    47  		missingFieldsErrors = append(missingFieldsErrors, errors.New("required field owner missing"))
    48  	}
    49  
    50  	if config.Repository == "" {
    51  		missingFieldsErrors = append(missingFieldsErrors, errors.New("required field repository missing"))
    52  	}
    53  
    54  	if config.BaseBranch == "" {
    55  		missingFieldsErrors = append(missingFieldsErrors, errors.New("required field base-branch missing"))
    56  	}
    57  
    58  	if config.MergeBranch == "" {
    59  		missingFieldsErrors = append(missingFieldsErrors, errors.New("required field merge-branch missing"))
    60  	}
    61  
    62  	if config.CommitBranch == "" {
    63  		missingFieldsErrors = append(missingFieldsErrors, errors.New("required field commit-branch missing"))
    64  	}
    65  
    66  	if config.ApkoLocations == nil {
    67  		missingFieldsErrors = append(missingFieldsErrors, errors.New("required field apko-locations missing"))
    68  	}
    69  
    70  	if config.GHPrivateKey == "" && config.GHPrivateKeyPath == "" {
    71  		missingFieldsErrors = append(missingFieldsErrors, errors.New("either gh-app-private-key or gh-app-private-key-path must be set for Github app authentication"))
    72  	}
    73  
    74  	if config.GHPrivateKey != "" && config.GHPrivateKeyPath != "" {
    75  		missingFieldsErrors = append(missingFieldsErrors, errors.New("gh-app-private-key-path and gh-app-private-key cannot both be set"))
    76  	}
    77  
    78  	if config.GHAppID == 0 {
    79  		missingFieldsErrors = append(missingFieldsErrors, errors.New("required field gh-app-id missing"))
    80  	}
    81  
    82  	if len(missingFieldsErrors) == 0 {
    83  		return nil
    84  	}
    85  
    86  	return errors.Join(missingFieldsErrors...)
    87  }
    88  

View as plain text