diff options
Diffstat (limited to 'vendor/github.com/google/yamlfmt/formatters/basic')
7 files changed, 381 insertions, 0 deletions
diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/README.md b/vendor/github.com/google/yamlfmt/formatters/basic/README.md new file mode 100644 index 0000000..af50a64 --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/README.md @@ -0,0 +1,3 @@ +# Basic Formatter + +For formatter settings, see [the configuration docs](../../docs/config-file.md). diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go b/vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go new file mode 100644 index 0000000..aef3070 --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go @@ -0,0 +1,37 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package anchors + +import ( + "errors" + "fmt" + + "github.com/braydonk/yaml" +) + +func Check(n yaml.Node) error { + if n.Kind == yaml.AliasNode { + return errors.New("alias node found") + } + if n.Anchor != "" { + return fmt.Errorf("node references anchor %q", n.Anchor) + } + for _, c := range n.Content { + if err := Check(*c); err != nil { + return err + } + } + return nil +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/config.go b/vendor/github.com/google/yamlfmt/formatters/basic/config.go new file mode 100644 index 0000000..82e67eb --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/config.go @@ -0,0 +1,52 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package basic + +import ( + "runtime" + + "github.com/google/yamlfmt" +) + +type Config struct { + Indent int `mapstructure:"indent"` + IncludeDocumentStart bool `mapstructure:"include_document_start"` + LineEnding yamlfmt.LineBreakStyle `mapstructure:"line_ending"` + LineLength int `mapstructure:"max_line_length"` + RetainLineBreaks bool `mapstructure:"retain_line_breaks"` + RetainLineBreaksSingle bool `mapstructure:"retain_line_breaks_single"` + DisallowAnchors bool `mapstructure:"disallow_anchors"` + ScanFoldedAsLiteral bool `mapstructure:"scan_folded_as_literal"` + IndentlessArrays bool `mapstructure:"indentless_arrays"` + DropMergeTag bool `mapstructure:"drop_merge_tag"` + PadLineComments int `mapstructure:"pad_line_comments"` + TrimTrailingWhitespace bool `mapstructure:"trim_trailing_whitespace"` + EOFNewline bool `mapstructure:"eof_newline"` + StripDirectives bool `mapstructure:"strip_directives"` + ArrayIndent int `mapstructure:"array_indent"` + IndentRootArray bool `mapstructure:"indent_root_array"` +} + +func DefaultConfig() *Config { + lineBreakStyle := yamlfmt.LineBreakStyleLF + if runtime.GOOS == "windows" { + lineBreakStyle = yamlfmt.LineBreakStyleCRLF + } + return &Config{ + Indent: 2, + LineEnding: lineBreakStyle, + PadLineComments: 1, + } +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/errors.go b/vendor/github.com/google/yamlfmt/formatters/basic/errors.go new file mode 100644 index 0000000..02c0a89 --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/errors.go @@ -0,0 +1,33 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package basic + +import "fmt" + +type BasicFormatterError struct { + err error +} + +func (e BasicFormatterError) Error() string { + return fmt.Sprintf("basic formatter error: %v", e.err) +} + +func (e BasicFormatterError) Unwrap() error { + return e.err +} + +// func wrapBasicFormatterError(err error) error { +// return BasicFormatterError{err: err} +// } diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/factory.go b/vendor/github.com/google/yamlfmt/formatters/basic/factory.go new file mode 100644 index 0000000..eb536b0 --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/factory.go @@ -0,0 +1,45 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package basic + +import ( + "github.com/google/yamlfmt" + "github.com/mitchellh/mapstructure" +) + +type BasicFormatterFactory struct{} + +func (f *BasicFormatterFactory) Type() string { + return BasicFormatterType +} + +func (f *BasicFormatterFactory) NewFormatter(configData map[string]interface{}) (yamlfmt.Formatter, error) { + config := DefaultConfig() + if configData != nil { + err := mapstructure.Decode(configData, &config) + if err != nil { + return nil, err + } + } + return newFormatter(config), nil +} + +func newFormatter(config *Config) yamlfmt.Formatter { + return &BasicFormatter{ + Config: config, + Features: ConfigureFeaturesFromConfig(config), + YAMLFeatures: ConfigureYAMLFeaturesFromConfig(config), + } +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/features.go b/vendor/github.com/google/yamlfmt/formatters/basic/features.go new file mode 100644 index 0000000..de736f4 --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features.go @@ -0,0 +1,78 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package basic + +import ( + "github.com/braydonk/yaml" + "github.com/google/yamlfmt" + "github.com/google/yamlfmt/formatters/basic/anchors" + "github.com/google/yamlfmt/internal/features" + "github.com/google/yamlfmt/internal/hotfix" +) + +func ConfigureFeaturesFromConfig(config *Config) yamlfmt.FeatureList { + lineSep, err := config.LineEnding.Separator() + if err != nil { + lineSep = "\n" + } + configuredFeatures := []yamlfmt.Feature{} + if config.RetainLineBreaks || config.RetainLineBreaksSingle { + configuredFeatures = append( + configuredFeatures, + hotfix.MakeFeatureRetainLineBreak(lineSep, config.RetainLineBreaksSingle), + ) + } + if config.TrimTrailingWhitespace { + configuredFeatures = append( + configuredFeatures, + features.MakeFeatureTrimTrailingWhitespace(lineSep), + ) + } + if config.EOFNewline { + configuredFeatures = append( + configuredFeatures, + features.MakeFeatureEOFNewline(lineSep), + ) + } + if config.StripDirectives { + configuredFeatures = append( + configuredFeatures, + hotfix.MakeFeatureStripDirectives(lineSep), + ) + } + return configuredFeatures +} + +// These features will directly use the `yaml.Node` type and +// as such are specific to this formatter. +type YAMLFeatureFunc func(yaml.Node) error +type YAMLFeatureList []YAMLFeatureFunc + +func (y YAMLFeatureList) ApplyFeatures(node yaml.Node) error { + for _, f := range y { + if err := f(node); err != nil { + return err + } + } + return nil +} + +func ConfigureYAMLFeaturesFromConfig(config *Config) YAMLFeatureList { + var features YAMLFeatureList + if config.DisallowAnchors { + features = append(features, anchors.Check) + } + return features +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go b/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go new file mode 100644 index 0000000..26bcc45 --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go @@ -0,0 +1,133 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package basic + +import ( + "bytes" + "context" + "errors" + "io" + + "github.com/braydonk/yaml" + "github.com/google/yamlfmt" + "github.com/mitchellh/mapstructure" +) + +const BasicFormatterType string = "basic" + +type BasicFormatter struct { + Config *Config + Features yamlfmt.FeatureList + YAMLFeatures YAMLFeatureList +} + +// yamlfmt.Formatter interface + +func (f *BasicFormatter) Type() string { + return BasicFormatterType +} + +func (f *BasicFormatter) Format(input []byte) ([]byte, error) { + // Run all features with BeforeActions + ctx := context.Background() + ctx, yamlContent, err := f.Features.ApplyFeatures(ctx, input, yamlfmt.FeatureApplyBefore) + if err != nil { + return nil, err + } + + // Format the yaml content + reader := bytes.NewReader(yamlContent) + decoder := f.getNewDecoder(reader) + documents := []yaml.Node{} + for { + var docNode yaml.Node + err := decoder.Decode(&docNode) + if err != nil { + if errors.Is(err, io.EOF) { + break + } + return nil, err + } + documents = append(documents, docNode) + } + + // Run all YAML features. + for _, d := range documents { + if err := f.YAMLFeatures.ApplyFeatures(d); err != nil { + return nil, err + } + } + + var b bytes.Buffer + e := f.getNewEncoder(&b) + for _, doc := range documents { + err := e.Encode(&doc) + if err != nil { + return nil, err + } + } + + // Run all features with AfterActions + _, resultYaml, err := f.Features.ApplyFeatures(ctx, b.Bytes(), yamlfmt.FeatureApplyAfter) + if err != nil { + return nil, err + } + + return resultYaml, nil +} + +func (f *BasicFormatter) getNewDecoder(reader io.Reader) *yaml.Decoder { + d := yaml.NewDecoder(reader) + if f.Config.ScanFoldedAsLiteral { + d.SetScanBlockScalarAsLiteral(true) + } + return d +} + +func (f *BasicFormatter) getNewEncoder(buf *bytes.Buffer) *yaml.Encoder { + e := yaml.NewEncoder(buf) + e.SetIndent(f.Config.Indent) + + if f.Config.LineLength > 0 { + e.SetWidth(f.Config.LineLength) + } + + if f.Config.LineEnding == yamlfmt.LineBreakStyleCRLF { + e.SetLineBreakStyle(yaml.LineBreakStyleCRLF) + } + + e.SetExplicitDocumentStart(f.Config.IncludeDocumentStart) + e.SetAssumeBlockAsLiteral(f.Config.ScanFoldedAsLiteral) + e.SetIndentlessBlockSequence(f.Config.IndentlessArrays) + e.SetDropMergeTag(f.Config.DropMergeTag) + e.SetPadLineComments(f.Config.PadLineComments) + + if f.Config.ArrayIndent > 0 { + e.SetArrayIndent(f.Config.ArrayIndent) + } + e.SetIndentRootArray(f.Config.IndentRootArray) + + return e +} + +func (f *BasicFormatter) ConfigMap() (map[string]any, error) { + configMap := map[string]any{} + err := mapstructure.Decode(f.Config, &configMap) + if err != nil { + return nil, err + } + configMap["type"] = BasicFormatterType + return configMap, err +} |
