diff --git a/private/buf/bufformat/bufformat.go b/private/buf/bufformat/bufformat.go index 19e58841d8..7f7b548cb3 100644 --- a/private/buf/bufformat/bufformat.go +++ b/private/buf/bufformat/bufformat.go @@ -16,18 +16,20 @@ package bufformat import ( "context" + "io" "github.com/bufbuild/buf/private/bufpkg/bufmodule" "github.com/bufbuild/buf/private/pkg/storage" "github.com/bufbuild/buf/private/pkg/storage/storagemem" "github.com/bufbuild/buf/private/pkg/thread" + "github.com/bufbuild/protocompile/ast" "github.com/bufbuild/protocompile/parser" "github.com/bufbuild/protocompile/reporter" "go.uber.org/multierr" ) -// Format formats and writes the target module files into a read bucket. -func Format(ctx context.Context, module bufmodule.Module) (_ storage.ReadBucket, retErr error) { +// FormatModule formats and writes the target module files into a read bucket. +func FormatModule(ctx context.Context, module bufmodule.Module) (_ storage.ReadBucket, retErr error) { fileInfos, err := module.TargetFileInfos(ctx) if err != nil { return nil, err @@ -55,7 +57,7 @@ func Format(ctx context.Context, module bufmodule.Module) (_ storage.ReadBucket, defer func() { retErr = multierr.Append(retErr, writeObjectCloser.Close()) }() - if err := newFormatter(writeObjectCloser, fileNode).Run(); err != nil { + if err := FormatFileNode(writeObjectCloser, fileNode); err != nil { return err } return writeObjectCloser.SetExternalPath(moduleFile.ExternalPath()) @@ -66,3 +68,9 @@ func Format(ctx context.Context, module bufmodule.Module) (_ storage.ReadBucket, } return readWriteBucket, nil } + +// FormatFileNode formats the given file node and writ the result to dest. +func FormatFileNode(dest io.Writer, fileNode *ast.FileNode) error { + formatter := newFormatter(dest, fileNode) + return formatter.Run() +} diff --git a/private/buf/bufformat/formatter_test.go b/private/buf/bufformat/formatter_test.go index 3335510966..55876c855c 100644 --- a/private/buf/bufformat/formatter_test.go +++ b/private/buf/bufformat/formatter_test.go @@ -71,7 +71,7 @@ func testFormatNoDiff(t *testing.T, path string) { require.NoError(t, err) module, err := bufmodule.NewModuleForBucket(ctx, moduleBucket) require.NoError(t, err) - readBucket, err := Format(ctx, module) + readBucket, err := FormatModule(ctx, module) require.NoError(t, err) require.NoError( t, diff --git a/private/buf/cmd/buf/command/format/format.go b/private/buf/cmd/buf/command/format/format.go index 92d0cfe65e..49b490cc27 100644 --- a/private/buf/cmd/buf/command/format/format.go +++ b/private/buf/cmd/buf/command/format/format.go @@ -473,7 +473,7 @@ func formatModule( return false, err } // Note that external paths are set properly for the files in this read bucket. - formattedReadBucket, err := bufformat.Format(ctx, module) + formattedReadBucket, err := bufformat.FormatModule(ctx, module) if err != nil { return false, err }