open-nomad/helper/warning.go
Charlie Voiselle d93ba0cf32
Add warnings to var put for non-alphanumeric keys. (#15933)
* Warn when Items key isn't directly accessible

Go template requires that map keys are alphanumeric for direct access
using the dotted reference syntax. This warns users when they create
keys that run afoul of this requirement.

- cli: use regex to detect invalid indentifiers in var keys
- test: fix slash in escape test case
- api: share warning formatting function between API and CLI
- ui: warn if var key has characters other than _, letter, or number

---------
Co-authored-by: Charlie Voiselle <464492+angrycub@users.noreply.github.com>
Co-authored-by: Luiz Aoqui <luiz@hashicorp.com>
2023-02-13 16:14:59 -05:00

43 lines
836 B
Go

package helper
import (
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
)
// MergeMultierrorWarnings takes warnings and merges them into a returnable
// string. This method is used to return API and CLI errors.
func MergeMultierrorWarnings(errs ...error) string {
if len(errs) == 0 {
return ""
}
var mErr multierror.Error
_ = multierror.Append(&mErr, errs...)
mErr.ErrorFormat = warningsFormatter
return mErr.Error()
}
// warningsFormatter is used to format warnings.
func warningsFormatter(es []error) string {
sb := strings.Builder{}
switch len(es) {
case 0:
return ""
case 1:
sb.WriteString("1 warning:\n")
default:
sb.WriteString(fmt.Sprintf("%d warnings:\n", len(es)))
}
for _, err := range es {
sb.WriteString(fmt.Sprintf("\n* %s", strings.TrimSpace(err.Error())))
}
return sb.String()
}