diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-24 17:40:45 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-24 17:40:45 -0600 |
| commit | d48fe690c3c071cb5c8e3aa4d4672a32230a5e2d (patch) | |
| tree | 414f9e91877e901cb3de12be6f466cb4929f55ab | |
| parent | 7257c213887c6a80f727642b016606ec10340ed9 (diff) | |
refactor: extract job to process relationship updates in background
34 files changed, 298 insertions, 119 deletions
diff --git a/app/init.go b/app/init.go index c22628c..ea67e48 100644 --- a/app/init.go +++ b/app/init.go @@ -2,12 +2,9 @@ package app import ( "context" - "fmt" "net/http" "os" - "strings" - v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" "github.com/authzed/authzed-go/v1" "github.com/rs/zerolog" "github.com/xlgmokha/x/pkg/env" @@ -20,6 +17,7 @@ import ( "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/controllers/sparkles" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/db" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/jobs" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/authz" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/web" ) @@ -83,52 +81,11 @@ func init() { } }) - ioc.MustResolve[*event.TypedAggregator[*domain.Sparkle]](c).SubscribeTo("after.create", func(item *domain.Sparkle) { - client := ioc.MustResolve[*authzed.Client](c) + logger := ioc.MustResolve[*zerolog.Logger](c) + ctx := logger.WithContext(context.Background()) + client := ioc.MustResolve[*authzed.Client](c) - resource := item.ToGID().ToObjectReference() - - response, err := client.WriteRelationships(context.Background(), &v1.WriteRelationshipsRequest{ - Updates: []*v1.RelationshipUpdate{ - &v1.RelationshipUpdate{ - Operation: v1.RelationshipUpdate_OPERATION_CREATE, - Relationship: &v1.Relationship{ - Resource: resource, - Relation: "sparkler", - Subject: item.Author.ToSubjectReference(), - }, - }, - &v1.RelationshipUpdate{ - Operation: v1.RelationshipUpdate_OPERATION_CREATE, - Relationship: &v1.Relationship{ - Resource: resource, - Relation: "sparklee", - Subject: &v1.SubjectReference{ - Object: &v1.ObjectReference{ - ObjectType: "user", - ObjectId: strings.TrimPrefix(item.Sparklee, "@"), - }, - }, - }, - }, - &v1.RelationshipUpdate{ - Operation: v1.RelationshipUpdate_OPERATION_CREATE, - Relationship: &v1.Relationship{ - Resource: resource, - Relation: "reader", - Subject: &v1.SubjectReference{ - Object: &v1.ObjectReference{ - ObjectType: "user", - ObjectId: "*", - }, - }, - }, - }, - }, - }) - if err != nil { - fmt.Printf("%v\n", err) - } - fmt.Printf("%v\n", response) - }) + ioc. + MustResolve[*event.TypedAggregator[*domain.Sparkle]](c). + SubscribeTo("after.create", jobs.NewCreateSparkle(ctx, client).Run) } diff --git a/app/jobs/create_sparkle.go b/app/jobs/create_sparkle.go new file mode 100644 index 0000000..3a03b1f --- /dev/null +++ b/app/jobs/create_sparkle.go @@ -0,0 +1,75 @@ +package jobs + +import ( + "context" + "strings" + + v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" + "github.com/authzed/authzed-go/v1" + "github.com/containerd/log" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls" +) + +type CreateSparkle struct { + client *authzed.Client + ctx context.Context +} + +func NewCreateSparkle(ctx context.Context, client *authzed.Client) Job[*domain.Sparkle] { + return &CreateSparkle{ + client: client, + ctx: ctx, + } +} + +func (job *CreateSparkle) Run(item *domain.Sparkle) { + response, err := job.client.WriteRelationships(job.ctx, job.requestFor(item)) + if err != nil { + pls.LogErrorNow(job.ctx, err) + } + pls.LogNow(job.ctx, log.Fields{"response": response}) +} + +func (job *CreateSparkle) requestFor(sparkle *domain.Sparkle) *v1.WriteRelationshipsRequest { + resource := sparkle.ToGID().ToObjectReference() + + return &v1.WriteRelationshipsRequest{ + Updates: []*v1.RelationshipUpdate{ + &v1.RelationshipUpdate{ + Operation: v1.RelationshipUpdate_OPERATION_CREATE, + Relationship: &v1.Relationship{ + Resource: resource, + Relation: "sparkler", + Subject: sparkle.Author.ToSubjectReference(), + }, + }, + &v1.RelationshipUpdate{ + Operation: v1.RelationshipUpdate_OPERATION_CREATE, + Relationship: &v1.Relationship{ + Resource: resource, + Relation: "sparklee", + Subject: &v1.SubjectReference{ + Object: &v1.ObjectReference{ + ObjectType: "user", + ObjectId: strings.TrimPrefix(sparkle.Sparklee, "@"), + }, + }, + }, + }, + &v1.RelationshipUpdate{ + Operation: v1.RelationshipUpdate_OPERATION_CREATE, + Relationship: &v1.Relationship{ + Resource: resource, + Relation: "reader", + Subject: &v1.SubjectReference{ + Object: &v1.ObjectReference{ + ObjectType: "user", + ObjectId: "*", + }, + }, + }, + }, + }, + } +} diff --git a/app/jobs/job.go b/app/jobs/job.go new file mode 100644 index 0000000..3864c76 --- /dev/null +++ b/app/jobs/job.go @@ -0,0 +1,5 @@ +package jobs + +type Job[T any] interface { + Run(T) +} @@ -4,6 +4,7 @@ go 1.24 require ( github.com/authzed/authzed-go v1.4.1 + github.com/containerd/log v0.1.0 github.com/coreos/go-oidc/v3 v3.14.1 github.com/docker/docker v28.3.2+incompatible github.com/docker/go-connections v0.5.0 @@ -58,7 +59,6 @@ require ( github.com/bits-and-blooms/bitset v1.13.0 // indirect github.com/bits-and-blooms/bloom/v3 v3.7.0 // indirect github.com/bmatcuk/doublestar/v4 v4.7.1 // indirect - github.com/braydonk/yaml v0.9.0 // indirect github.com/bufbuild/protocompile v0.14.1 // indirect github.com/ccoveille/go-safecast v1.6.1 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect @@ -72,7 +72,6 @@ require ( github.com/cncf/xds/go v0.0.0-20250501225837-2ac532fd4443 // indirect github.com/containerd/errdefs v1.0.0 // indirect github.com/containerd/errdefs/pkg v0.3.0 // indirect - github.com/containerd/log v0.1.0 // indirect github.com/containerd/platforms v0.2.1 // indirect github.com/cpuguy83/dockercfg v0.3.2 // indirect github.com/creasty/defaults v1.8.0 // indirect @@ -111,7 +110,7 @@ require ( github.com/google/go-querystring v1.1.0 // indirect github.com/google/jsonapi v1.0.0 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/google/yamlfmt v0.16.0 // indirect + github.com/google/yamlfmt v0.17.2 // indirect github.com/gookit/color v1.5.4 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.1 // indirect @@ -112,8 +112,6 @@ github.com/bits-and-blooms/bloom/v3 v3.7.0 h1:VfknkqV4xI+PsaDIsoHueyxVDZrfvMn56j github.com/bits-and-blooms/bloom/v3 v3.7.0/go.mod h1:VKlUSvp0lFIYqxJjzdnSsZEw4iHb1kOL2tfHTgyJBHg= github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q= github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= -github.com/braydonk/yaml v0.9.0 h1:ewGMrVmEVpsm3VwXQDR388sLg5+aQ8Yihp6/hc4m+h4= -github.com/braydonk/yaml v0.9.0/go.mod h1:hcm3h581tudlirk8XEUPDBAimBPbmnL0Y45hCRl47N4= github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4= github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs= github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw= @@ -307,8 +305,8 @@ github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0= github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/yamlfmt v0.16.0 h1:5auoxqdx2CxOb022XGBElFFVH8uE/lAJDCWKRMq4mT8= -github.com/google/yamlfmt v0.16.0/go.mod h1:/fF8jQmFopG3InQoWYG3gTORPXqLwNkcBqAT4UA4ab0= +github.com/google/yamlfmt v0.17.2 h1:TkXxhmj7dnpmOnlWGOXog92Gs6MWcTZqnf3kuyp8yFQ= +github.com/google/yamlfmt v0.17.2/go.mod h1:gs0UEklJOYkUJ+OOCG0hg9n+DzucKDPlJElTUasVNK8= github.com/googleapis/enterprise-certificate-proxy v0.3.6 h1:GW/XbdyBFQ8Qe+YAmFU9uHLo7OnF5tL52HFAgMmyrf4= github.com/googleapis/enterprise-certificate-proxy v0.3.6/go.mod h1:MkHOF77EYAE7qfSuSS9PU6g4Nt4e11cnsDUowfwewLA= github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q= diff --git a/vendor/github.com/google/yamlfmt/Makefile b/vendor/github.com/google/yamlfmt/Makefile index 2790ae3..9993d6d 100644 --- a/vendor/github.com/google/yamlfmt/Makefile +++ b/vendor/github.com/google/yamlfmt/Makefile @@ -15,7 +15,12 @@ test: .PHONY: test_v test_v: - go test -v ./... + @go test -v $$(go list ./... | grep -v "pkg/yaml") + @go test ./pkg/yaml/formattest + +.PHONY: vet +vet: + go vet $$(go list ./... | grep -v "pkg/yaml") YAMLFMT_BIN ?= $(shell pwd)/dist/yamlfmt .PHONY: integrationtest @@ -36,7 +41,7 @@ integrationtest_stdout: .PHONY: integrationtest_update integrationtest_update: $(MAKE) build - go test -tags=integration_test ./integrationtest/command -update + go test -tags=integration_test -v ./integrationtest/command -update .PHONY: command_test_case command_test_case: @@ -53,10 +58,12 @@ install: install_tools: go install github.com/google/addlicense@latest +ADDLICENSE = addlicense -ignore "**/testdata/**" -ignore "**/pkg/yaml/**" -c "Google LLC" -l apache + .PHONY: addlicense addlicense: - addlicense -ignore "**/testdata/**" -c "Google LLC" -l apache . + $(ADDLICENSE) . .PHONY: addlicense_check addlicense_check: - addlicense -check -ignore "**/testdata/**" -c "Google LLC" -l apache . + $(ADDLICENSE) -check . diff --git a/vendor/github.com/google/yamlfmt/command/command.go b/vendor/github.com/google/yamlfmt/command/command.go index 3224f46..1204b94 100644 --- a/vendor/github.com/google/yamlfmt/command/command.go +++ b/vendor/github.com/google/yamlfmt/command/command.go @@ -23,9 +23,8 @@ import ( "github.com/google/yamlfmt" "github.com/google/yamlfmt/engine" + "github.com/google/yamlfmt/pkg/yaml" "github.com/mitchellh/mapstructure" - - "github.com/braydonk/yaml" ) type FormatterConfig struct { @@ -58,6 +57,7 @@ type Command struct { Registry *yamlfmt.Registry Config *Config Quiet bool + Verbose bool } func (c *Command) Run() error { @@ -75,6 +75,7 @@ func (c *Command) Run() error { LineSepCharacter: lineSepChar, Formatter: formatter, Quiet: c.Quiet, + Verbose: c.Verbose, ContinueOnError: c.Config.ContinueOnError, OutputFormat: c.Config.OutputFormat, } diff --git a/vendor/github.com/google/yamlfmt/engine.go b/vendor/github.com/google/yamlfmt/engine.go index b98ee89..bd43f0c 100644 --- a/vendor/github.com/google/yamlfmt/engine.go +++ b/vendor/github.com/google/yamlfmt/engine.go @@ -17,6 +17,7 @@ package yamlfmt import ( "fmt" "os" + "slices" "github.com/google/yamlfmt/internal/collections" "github.com/google/yamlfmt/internal/multilinediff" @@ -88,7 +89,9 @@ func (fds FileDiffs) Add(diff *FileDiff) error { func (fds FileDiffs) StrOutput() string { result := "" - for _, fd := range fds { + sortedPaths := fds.sortedPaths() + for _, path := range sortedPaths { + fd := fds[path] if fd.Diff.Changed() { result += fd.StrOutput() } @@ -98,7 +101,9 @@ func (fds FileDiffs) StrOutput() string { func (fds FileDiffs) StrOutputQuiet() string { result := "" - for _, fd := range fds { + sortedPaths := fds.sortedPaths() + for _, path := range sortedPaths { + fd := fds[path] if fd.Diff.Changed() { result += fd.StrOutputQuiet() } @@ -125,3 +130,12 @@ func (fds FileDiffs) ChangedCount() int { } return changed } + +func (fds FileDiffs) sortedPaths() []string { + pathKeys := []string{} + for path := range fds { + pathKeys = append(pathKeys, path) + } + slices.Sort(pathKeys) + return pathKeys +} diff --git a/vendor/github.com/google/yamlfmt/engine/consecutive_engine.go b/vendor/github.com/google/yamlfmt/engine/consecutive_engine.go index 650d1b7..1ec241a 100644 --- a/vendor/github.com/google/yamlfmt/engine/consecutive_engine.go +++ b/vendor/github.com/google/yamlfmt/engine/consecutive_engine.go @@ -19,15 +19,17 @@ import ( "os" "github.com/google/yamlfmt" + "github.com/google/yamlfmt/internal/logger" ) // Engine that will process each file one by one consecutively. type ConsecutiveEngine struct { LineSepCharacter string Formatter yamlfmt.Formatter - Quiet bool ContinueOnError bool OutputFormat EngineOutputFormat + Quiet bool + Verbose bool } func (e *ConsecutiveEngine) FormatContent(content []byte) ([]byte, error) { @@ -36,6 +38,13 @@ func (e *ConsecutiveEngine) FormatContent(content []byte) ([]byte, error) { func (e *ConsecutiveEngine) Format(paths []string) (fmt.Stringer, error) { formatDiffs, formatErrs := e.formatAll(paths) + + // Debug format diff output. + logger.Debug( + logger.DebugCodeDiffs, + fmt.Sprintf("The following files were modified:\n%s", formatDiffs.StrOutput()), + ) + if len(formatErrs) > 0 { if e.ContinueOnError { fmt.Print(formatErrs) @@ -44,7 +53,11 @@ func (e *ConsecutiveEngine) Format(paths []string) (fmt.Stringer, error) { return nil, formatErrs } } - return nil, formatDiffs.ApplyAll() + applyErr := formatDiffs.ApplyAll() + if applyErr != nil { + return nil, applyErr + } + return getEngineOutput(e.OutputFormat, yamlfmt.OperationFormat, formatDiffs, e.Quiet, e.Verbose) } func (e *ConsecutiveEngine) Lint(paths []string) (fmt.Stringer, error) { @@ -55,7 +68,7 @@ func (e *ConsecutiveEngine) Lint(paths []string) (fmt.Stringer, error) { if formatDiffs.ChangedCount() == 0 { return nil, nil } - return getEngineOutput(e.OutputFormat, yamlfmt.OperationLint, formatDiffs, e.Quiet) + return getEngineOutput(e.OutputFormat, yamlfmt.OperationLint, formatDiffs, e.Quiet, e.Verbose) } func (e *ConsecutiveEngine) DryRun(paths []string) (fmt.Stringer, error) { @@ -66,7 +79,7 @@ func (e *ConsecutiveEngine) DryRun(paths []string) (fmt.Stringer, error) { if formatDiffs.ChangedCount() == 0 { return nil, nil } - return getEngineOutput(e.OutputFormat, yamlfmt.OperationDry, formatDiffs, e.Quiet) + return getEngineOutput(e.OutputFormat, yamlfmt.OperationDry, formatDiffs, e.Quiet, e.Verbose) } func (e *ConsecutiveEngine) formatAll(paths []string) (yamlfmt.FileDiffs, FormatErrors) { diff --git a/vendor/github.com/google/yamlfmt/engine/output.go b/vendor/github.com/google/yamlfmt/engine/output.go index 2d35e84..c6a2e9e 100644 --- a/vendor/github.com/google/yamlfmt/engine/output.go +++ b/vendor/github.com/google/yamlfmt/engine/output.go @@ -32,10 +32,10 @@ const ( EngineOutputGitlab EngineOutputFormat = "gitlab" ) -func getEngineOutput(t EngineOutputFormat, operation yamlfmt.Operation, files yamlfmt.FileDiffs, quiet bool) (fmt.Stringer, error) { +func getEngineOutput(t EngineOutputFormat, operation yamlfmt.Operation, files yamlfmt.FileDiffs, quiet bool, verbose bool) (fmt.Stringer, error) { switch t { case EngineOutputDefault: - return engineOutput{Operation: operation, Files: files, Quiet: quiet}, nil + return engineOutput{Operation: operation, Files: files, Quiet: quiet, Verbose: verbose}, nil case EngineOutputSingeLine: return engineOutputSingleLine{Operation: operation, Files: files, Quiet: quiet}, nil case EngineOutputGitlab: @@ -49,11 +49,21 @@ type engineOutput struct { Operation yamlfmt.Operation Files yamlfmt.FileDiffs Quiet bool + Verbose bool } func (eo engineOutput) String() string { var msg string switch eo.Operation { + case yamlfmt.OperationFormat: + // Formatting only produces output in verbose mode. + if !eo.Verbose { + return "" + } + + msg = "The following files were modified:\n" + msg += eo.Files.StrOutputQuiet() + return msg case yamlfmt.OperationLint: msg = "The following formatting differences were found:" if eo.Quiet { @@ -65,7 +75,7 @@ func (eo engineOutput) String() string { msg = "The following files would be formatted:" } } else { - return "No files will formatted." + return "No files will be formatted." } } var result string diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/config.go b/vendor/github.com/google/yamlfmt/formatters/basic/config.go index 82e67eb..2a32d05 100644 --- a/vendor/github.com/google/yamlfmt/formatters/basic/config.go +++ b/vendor/github.com/google/yamlfmt/formatters/basic/config.go @@ -18,25 +18,28 @@ import ( "runtime" "github.com/google/yamlfmt" + yamlFeatures "github.com/google/yamlfmt/formatters/basic/features" ) 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"` + 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"` + DisableAliasKeyCorrection bool `mapstructure:"disable_alias_key_correction"` + ForceArrayStyle yamlFeatures.SequenceStyle `mapstructure:"force_array_style"` } func DefaultConfig() *Config { diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/features.go b/vendor/github.com/google/yamlfmt/formatters/basic/features.go index de736f4..fc933f4 100644 --- a/vendor/github.com/google/yamlfmt/formatters/basic/features.go +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features.go @@ -15,9 +15,8 @@ package basic import ( - "github.com/braydonk/yaml" "github.com/google/yamlfmt" - "github.com/google/yamlfmt/formatters/basic/anchors" + yamlFeatures "github.com/google/yamlfmt/formatters/basic/features" "github.com/google/yamlfmt/internal/features" "github.com/google/yamlfmt/internal/hotfix" ) @@ -55,24 +54,19 @@ func ConfigureFeaturesFromConfig(config *Config) yamlfmt.FeatureList { 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 ConfigureYAMLFeaturesFromConfig(config *Config) yamlFeatures.YAMLFeatureList { + var featureList yamlFeatures.YAMLFeatureList -func (y YAMLFeatureList) ApplyFeatures(node yaml.Node) error { - for _, f := range y { - if err := f(node); err != nil { - return err - } + if config.DisallowAnchors { + featureList = append(featureList, yamlFeatures.Check) } - return nil -} -func ConfigureYAMLFeaturesFromConfig(config *Config) YAMLFeatureList { - var features YAMLFeatureList - if config.DisallowAnchors { - features = append(features, anchors.Check) + if config.ForceArrayStyle != "" { + featureList = append( + featureList, + yamlFeatures.FeatureForceSequenceStyle(config.ForceArrayStyle), + ) } - return features + + return featureList } diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go b/vendor/github.com/google/yamlfmt/formatters/basic/features/check.go index aef3070..368de5c 100644 --- a/vendor/github.com/google/yamlfmt/formatters/basic/anchors/check.go +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features/check.go @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -package anchors +package features import ( "errors" "fmt" - "github.com/braydonk/yaml" + "github.com/google/yamlfmt/pkg/yaml" ) func Check(n yaml.Node) error { diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/features/force_sequence.go b/vendor/github.com/google/yamlfmt/formatters/basic/features/force_sequence.go new file mode 100644 index 0000000..f3f956c --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features/force_sequence.go @@ -0,0 +1,43 @@ +// Copyright 2025 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 features + +import "github.com/google/yamlfmt/pkg/yaml" + +type SequenceStyle string + +const ( + SequenceStyleBlock SequenceStyle = "block" + SequenceStyleFlow SequenceStyle = "flow" +) + +func FeatureForceSequenceStyle(style SequenceStyle) YAMLFeatureFunc { + var styleVal yaml.Style + if style == SequenceStyleFlow { + styleVal = yaml.FlowStyle + } + var forceStyle YAMLFeatureFunc + forceStyle = func(n yaml.Node) error { + var err error + for _, c := range n.Content { + if c.Kind == yaml.SequenceNode { + c.Style = styleVal + } + err = forceStyle(*c) + } + return err + } + return forceStyle +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/features/yaml_feature.go b/vendor/github.com/google/yamlfmt/formatters/basic/features/yaml_feature.go new file mode 100644 index 0000000..2657a53 --- /dev/null +++ b/vendor/github.com/google/yamlfmt/formatters/basic/features/yaml_feature.go @@ -0,0 +1,31 @@ +// Copyright 2025 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 features + +import "github.com/google/yamlfmt/pkg/yaml" + +// 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 +} diff --git a/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go b/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go index 26bcc45..1e69bdb 100644 --- a/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go +++ b/vendor/github.com/google/yamlfmt/formatters/basic/formatter.go @@ -20,8 +20,9 @@ import ( "errors" "io" - "github.com/braydonk/yaml" "github.com/google/yamlfmt" + yamlFeature "github.com/google/yamlfmt/formatters/basic/features" + "github.com/google/yamlfmt/pkg/yaml" "github.com/mitchellh/mapstructure" ) @@ -30,7 +31,7 @@ const BasicFormatterType string = "basic" type BasicFormatter struct { Config *Config Features yamlfmt.FeatureList - YAMLFeatures YAMLFeatureList + YAMLFeatures yamlFeature.YAMLFeatureList } // yamlfmt.Formatter interface @@ -63,6 +64,10 @@ func (f *BasicFormatter) Format(input []byte) ([]byte, error) { documents = append(documents, docNode) } + if len(documents) == 0 { + return input, nil + } + // Run all YAML features. for _, d := range documents { if err := f.YAMLFeatures.ApplyFeatures(d); err != nil { @@ -119,6 +124,13 @@ func (f *BasicFormatter) getNewEncoder(buf *bytes.Buffer) *yaml.Encoder { } e.SetIndentRootArray(f.Config.IndentRootArray) + // Yes I know I could SetCorrectAliasKeys(!f.Config.DisableAliasKeyCorrection) + // but I know myself and I know I'll get confused and have to go look up + // the source again next time I look and forget. + if !f.Config.DisableAliasKeyCorrection { + e.SetCorrectAliasKeys(true) + } + return e } diff --git a/vendor/github.com/google/yamlfmt/internal/logger/debug.go b/vendor/github.com/google/yamlfmt/internal/logger/debug.go index a75463c..5a82c3c 100644 --- a/vendor/github.com/google/yamlfmt/internal/logger/debug.go +++ b/vendor/github.com/google/yamlfmt/internal/logger/debug.go @@ -26,13 +26,15 @@ const ( DebugCodeAny DebugCode = iota DebugCodeConfig DebugCodePaths + DebugCodeDiffs ) var ( supportedDebugCodes = map[string][]DebugCode{ "config": {DebugCodeConfig}, "paths": {DebugCodePaths}, - "all": {DebugCodeConfig, DebugCodePaths}, + "diffs": {DebugCodeDiffs}, + "all": {DebugCodeConfig, DebugCodePaths, DebugCodeDiffs}, } activeDebugCodes = collections.Set[DebugCode]{} ) diff --git a/vendor/github.com/braydonk/yaml/LICENSE b/vendor/github.com/google/yamlfmt/pkg/yaml/LICENSE index 2683e4b..2683e4b 100644 --- a/vendor/github.com/braydonk/yaml/LICENSE +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/LICENSE diff --git a/vendor/github.com/braydonk/yaml/NOTICE b/vendor/github.com/google/yamlfmt/pkg/yaml/NOTICE index 866d74a..866d74a 100644 --- a/vendor/github.com/braydonk/yaml/NOTICE +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/NOTICE diff --git a/vendor/github.com/braydonk/yaml/README.md b/vendor/github.com/google/yamlfmt/pkg/yaml/README.md index 3a9ddf3..105ee6b 100644 --- a/vendor/github.com/braydonk/yaml/README.md +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/README.md @@ -1,4 +1,4 @@ -# YAML support for the Go language +# The Artist formerly known as gopkg.in/yaml.v3 FORK ------------ diff --git a/vendor/github.com/braydonk/yaml/apic.go b/vendor/github.com/google/yamlfmt/pkg/yaml/apic.go index 76cc0e4..e58b481 100644 --- a/vendor/github.com/braydonk/yaml/apic.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/apic.go @@ -214,6 +214,11 @@ func yaml_emitter_set_pad_line_comments(emitter *yaml_emitter_t, pad_line_commen emitter.pad_line_comments = pad_line_comments } +// Set correct alias keys. +func yaml_emitter_set_correct_alias_keys(emitter *yaml_emitter_t, correct_alias_keys bool) { + emitter.correct_alias_keys = correct_alias_keys +} + ///* // * Destroy a token object. // */ diff --git a/vendor/github.com/braydonk/yaml/decode.go b/vendor/github.com/google/yamlfmt/pkg/yaml/decode.go index 0173b69..0173b69 100644 --- a/vendor/github.com/braydonk/yaml/decode.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/decode.go diff --git a/vendor/github.com/braydonk/yaml/emitterc.go b/vendor/github.com/google/yamlfmt/pkg/yaml/emitterc.go index 6a93554..3a1aede 100644 --- a/vendor/github.com/braydonk/yaml/emitterc.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/emitterc.go @@ -888,7 +888,7 @@ func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t, switch event.typ { case yaml_ALIAS_EVENT: - return yaml_emitter_emit_alias(emitter, event) + return yaml_emitter_emit_alias(emitter) case yaml_SCALAR_EVENT: return yaml_emitter_emit_scalar(emitter, event) case yaml_SEQUENCE_START_EVENT: @@ -902,10 +902,16 @@ func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t, } // Expect ALIAS. -func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool { +func yaml_emitter_emit_alias(emitter *yaml_emitter_t) bool { if !yaml_emitter_process_anchor(emitter) { return false } + if emitter.correct_alias_keys && emitter.simple_key_context && + len(emitter.states) > 1 && emitter.states[len(emitter.states)-2] == yaml_EMIT_BLOCK_MAPPING_KEY_STATE { + if !put(emitter, ' ') { + return false + } + } emitter.state = emitter.states[len(emitter.states)-1] emitter.states = emitter.states[:len(emitter.states)-1] return true diff --git a/vendor/github.com/braydonk/yaml/encode.go b/vendor/github.com/google/yamlfmt/pkg/yaml/encode.go index ba1c854..ba1c854 100644 --- a/vendor/github.com/braydonk/yaml/encode.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/encode.go diff --git a/vendor/github.com/braydonk/yaml/parserc.go b/vendor/github.com/google/yamlfmt/pkg/yaml/parserc.go index 23f3d6a..23f3d6a 100644 --- a/vendor/github.com/braydonk/yaml/parserc.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/parserc.go diff --git a/vendor/github.com/braydonk/yaml/readerc.go b/vendor/github.com/google/yamlfmt/pkg/yaml/readerc.go index b7de0a8..b7de0a8 100644 --- a/vendor/github.com/braydonk/yaml/readerc.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/readerc.go diff --git a/vendor/github.com/braydonk/yaml/resolve.go b/vendor/github.com/google/yamlfmt/pkg/yaml/resolve.go index 64ae888..64ae888 100644 --- a/vendor/github.com/braydonk/yaml/resolve.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/resolve.go diff --git a/vendor/github.com/braydonk/yaml/scannerc.go b/vendor/github.com/google/yamlfmt/pkg/yaml/scannerc.go index 7faa856..7faa856 100644 --- a/vendor/github.com/braydonk/yaml/scannerc.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/scannerc.go diff --git a/vendor/github.com/braydonk/yaml/sorter.go b/vendor/github.com/google/yamlfmt/pkg/yaml/sorter.go index 9210ece..9210ece 100644 --- a/vendor/github.com/braydonk/yaml/sorter.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/sorter.go diff --git a/vendor/github.com/braydonk/yaml/writerc.go b/vendor/github.com/google/yamlfmt/pkg/yaml/writerc.go index b8a116b..b8a116b 100644 --- a/vendor/github.com/braydonk/yaml/writerc.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/writerc.go diff --git a/vendor/github.com/braydonk/yaml/yaml.go b/vendor/github.com/google/yamlfmt/pkg/yaml/yaml.go index b336163..5fcdc1a 100644 --- a/vendor/github.com/braydonk/yaml/yaml.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/yaml.go @@ -343,6 +343,11 @@ func (e *Encoder) SetPadLineComments(padLineComments int) { yaml_emitter_set_pad_line_comments(&e.encoder.emitter, padLineComments) } +// SetCorrectAliasKeys enables alias key syntax correction. +func (e *Encoder) SetCorrectAliasKeys(correctAliasKeys bool) { + yaml_emitter_set_correct_alias_keys(&e.encoder.emitter, correctAliasKeys) +} + // Close closes the encoder by writing any remaining data. // It does not write a stream terminating string "...". func (e *Encoder) Close() (err error) { diff --git a/vendor/github.com/braydonk/yaml/yamlh.go b/vendor/github.com/google/yamlfmt/pkg/yaml/yamlh.go index 92ab866..a8ccef7 100644 --- a/vendor/github.com/braydonk/yaml/yamlh.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/yamlh.go @@ -741,6 +741,7 @@ type yaml_emitter_t struct { explicit_document_start bool // Force an explicit document start assume_folded_as_literal bool // Assume blocks were scanned as literals pad_line_comments int // The number of spaces to insert before line comments. + correct_alias_keys bool // Whether to correct alias nodes used as map keys. state yaml_emitter_state_t // The current emitter state. states []yaml_emitter_state_t // The stack of states. diff --git a/vendor/github.com/braydonk/yaml/yamlprivateh.go b/vendor/github.com/google/yamlfmt/pkg/yaml/yamlprivateh.go index 50e1532..50e1532 100644 --- a/vendor/github.com/braydonk/yaml/yamlprivateh.go +++ b/vendor/github.com/google/yamlfmt/pkg/yaml/yamlprivateh.go diff --git a/vendor/modules.txt b/vendor/modules.txt index 03fec83..4dab1c0 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -276,9 +276,6 @@ github.com/bits-and-blooms/bloom/v3 # github.com/bmatcuk/doublestar/v4 v4.7.1 ## explicit; go 1.16 github.com/bmatcuk/doublestar/v4 -# github.com/braydonk/yaml v0.9.0 -## explicit; go 1.11 -github.com/braydonk/yaml # github.com/bufbuild/protocompile v0.14.1 ## explicit; go 1.21 github.com/bufbuild/protocompile @@ -560,20 +557,21 @@ github.com/google/jsonapi # github.com/google/uuid v1.6.0 ## explicit github.com/google/uuid -# github.com/google/yamlfmt v0.16.0 +# github.com/google/yamlfmt v0.17.2 ## explicit; go 1.21 github.com/google/yamlfmt github.com/google/yamlfmt/cmd/yamlfmt github.com/google/yamlfmt/command github.com/google/yamlfmt/engine github.com/google/yamlfmt/formatters/basic -github.com/google/yamlfmt/formatters/basic/anchors +github.com/google/yamlfmt/formatters/basic/features github.com/google/yamlfmt/internal/collections github.com/google/yamlfmt/internal/features github.com/google/yamlfmt/internal/gitlab github.com/google/yamlfmt/internal/hotfix github.com/google/yamlfmt/internal/logger github.com/google/yamlfmt/internal/multilinediff +github.com/google/yamlfmt/pkg/yaml # github.com/gookit/color v1.5.4 ## explicit; go 1.18 github.com/gookit/color |
