vault/internalshared/configutil/lint.go
hashicorp-copywrite[bot] 0b12cdcfd1
[COMPLIANCE] License changes (#22290)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License.

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUS-1.1

* Fix test that expected exact offset on hcl file

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Sarah Thompson <sthompson@hashicorp.com>
Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
2023-08-10 18:14:03 -07:00

63 lines
1.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package configutil
import (
"fmt"
"github.com/asaskevich/govalidator"
"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/hashicorp/hcl/hcl/token"
)
type UnusedKeyMap map[string][]token.Pos
type ConfigError struct {
Problem string
Position token.Pos
}
func (c *ConfigError) String() string {
return fmt.Sprintf("%s at %s", c.Problem, c.Position.String())
}
type ValidatableConfig interface {
Validate() []ConfigError
}
// Creates the ConfigErrors for unused fields, which occur in various structs
func ValidateUnusedFields(unusedKeyPositions UnusedKeyMap, sourceFilePath string) []ConfigError {
if unusedKeyPositions == nil {
return nil
}
var errors []ConfigError
for field, positions := range unusedKeyPositions {
problem := fmt.Sprintf("unknown or unsupported field %s found in configuration", field)
for _, pos := range positions {
if pos.Filename == "" && sourceFilePath != "" {
pos.Filename = sourceFilePath
}
errors = append(errors, ConfigError{
Problem: problem,
Position: pos,
})
}
}
return errors
}
// UnusedFieldDifference returns all the keys in map a that are not present in map b, and also not present in foundKeys.
func UnusedFieldDifference(a, b UnusedKeyMap, foundKeys []string) UnusedKeyMap {
if a == nil {
return nil
}
res := make(UnusedKeyMap)
for k, v := range a {
if _, ok := b[k]; !ok && !strutil.StrListContainsCaseInsensitive(foundKeys, govalidator.UnderscoreToCamelCase(k)) {
res[k] = v
}
}
return res
}