anchore / fangs

Apache License 2.0
5 stars 1 forks source link

fix: slice handling #16

Closed kzantow closed 1 year ago

kzantow commented 1 year ago

This PR corrects 2 issues: 1) Slices (including those with embedded structs/more slices) are now properly summarized in valid YAML such that a user is able to copy/paste the entire configuration to a file and edit from there 2) Slices were being modified instead of using the values from a configuration file

An example of the second issue is:

type Config struct {
  Exclude []string
}

func NewConfig() *Config {
  return &Config{
    Exclude: []string{ "some", "default", "values" },
  }
}

Make a config file with a different set of excludes:

Exclude:
  - set
  - value

Then load this configuration file into the struct with defaults:

c := NewConfig()
fangs.Load(..., c)

... Viper will modify entries instead of using the config file as the source of truth: c.Exclude contains: "set", "value", "values", including a mix of defaults if the default list is longer than the config provided one.

Example (trimmed) before/after from Chronicle:

Before:

# output format to use: [md json] (env: CHRONICLE_OUTPUT)
output: 'md'

github:
  # (env: CHRONICLE_GITHUB_HOST)
  host: 'github.com'

  # (env: CHRONICLE_GITHUB_EXCLUDE_LABELS)
  exclude-labels: [duplicate question invalid wontfix wont-fix release-ignore changelog-ignore ignore]

  # (env: CHRONICLE_GITHUB_CONSIDER_PR_MERGE_COMMITS)
  consider-pr-merge-commits: true

  # (env: CHRONICLE_GITHUB_CHANGES)
  changes: [{security-fixes Security Fixes patch [security vulnerability]} {added-feature Added Features minor [enhancement feature minor]} {bug-fix Bug Fixes patch [bug fix bug-fix patch]} {breaking-feature Breaking Changes major [breaking backwards-incompatible breaking-change breaking-feature major]} {removed-feature Removed Features major [removed]} {deprecated-feature Deprecated Features minor [deprecated]} {unknown Additional Changes  []}]

After:

# output format to use: [md json] (env: CHRONICLE_OUTPUT)
output: 'md'

github:
  # (env: CHRONICLE_GITHUB_HOST)
  host: 'github.com'

  # (env: CHRONICLE_GITHUB_EXCLUDE_LABELS)
  exclude-labels: 
    - 'duplicate'
    - 'question'
    - 'invalid'
    - 'wontfix'
    - 'wont-fix'
    - 'release-ignore'
    - 'changelog-ignore'
    - 'ignore'

  # (env: CHRONICLE_GITHUB_CONSIDER_PR_MERGE_COMMITS)
  consider-pr-merge-commits: true

  # (env: CHRONICLE_GITHUB_CHANGES)
  changes: 
    - name: 'security-fixes'
      title: 'Security Fixes'
      semver-field: 'patch'
      labels: 
        - 'security'
        - 'vulnerability'

    - name: 'added-feature'
      title: 'Added Features'
      semver-field: 'minor'
      labels: 
        - 'enhancement'
        - 'feature'
        - 'minor'

    - name: 'bug-fix'
      title: 'Bug Fixes'
      semver-field: 'patch'
      labels: 
        - 'bug'
        - 'fix'
        - 'bug-fix'
        - 'patch'
  ...