Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Dec 21, 2023
1 parent 290bbb8 commit 942d5d8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 93 deletions.
93 changes: 49 additions & 44 deletions private/buf/bufmigrate/bufmigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@ package bufmigrate
import (
"context"
"errors"
"fmt"
"io"
"path/filepath"

"github.com/bufbuild/buf/private/bufpkg/bufapi"
"github.com/bufbuild/buf/private/pkg/slicesext"
"github.com/bufbuild/buf/private/pkg/storage/storageos"
"go.uber.org/zap"
)

// Migrate migrate buf configuration files.
// Migrate migrates buf configuration files.
//
// TODO: document behavior with different examples of module and workspace directories, ie what happens
// when I specify a module directory that is within a workspace directory I always specify, what happens
// if I don't specify the module directory, what if I only specify some module directories in a
// workspace directory, etc.
func Migrate(
ctx context.Context,
logger *zap.Logger,
messageWriter io.Writer,
storageProvider storageos.Provider,
clientProvider bufapi.ClientProvider,
// TODO: use these values
workspaceDirPaths []string,
moduleDirPaths []string,
generateTemplatePaths []string,
options ...MigrateOption,
) (retErr error) {
migrateOptions := newMigrateOptions()
Expand All @@ -54,14 +59,14 @@ func Migrate(
destionationDirectory = migrateOptions.workspaceDirectory

Check failure on line 59 in private/buf/bufmigrate/bufmigrate.go

View workflow job for this annotation

GitHub Actions / test

migrateOptions.workspaceDirectory undefined (type *migrateOptions has no field or method workspaceDirectory)

Check failure on line 59 in private/buf/bufmigrate/bufmigrate.go

View workflow job for this annotation

GitHub Actions / lint

migrateOptions.workspaceDirectory undefined (type *migrateOptions has no field or method workspaceDirectory)
}
migrator := newMigrator(
logger,
messageWriter,
clientProvider,
bucket,
destionationDirectory,
)
if migrateOptions.workspaceDirectory != "" {

Check failure on line 67 in private/buf/bufmigrate/bufmigrate.go

View workflow job for this annotation

GitHub Actions / test

migrateOptions.workspaceDirectory undefined (type *migrateOptions has no field or method workspaceDirectory)

Check failure on line 67 in private/buf/bufmigrate/bufmigrate.go

View workflow job for this annotation

GitHub Actions / lint

migrateOptions.workspaceDirectory undefined (type *migrateOptions has no field or method workspaceDirectory)
// TODO: should we allow multiple workspaces?
if err := migrator.addWorkspace(
if err := migrator.addWorkspaceDirectory(
ctx,
migrateOptions.workspaceDirectory,

Check failure on line 71 in private/buf/bufmigrate/bufmigrate.go

View workflow job for this annotation

GitHub Actions / test

migrateOptions.workspaceDirectory undefined (type *migrateOptions has no field or method workspaceDirectory)

Check failure on line 71 in private/buf/bufmigrate/bufmigrate.go

View workflow job for this annotation

GitHub Actions / lint

migrateOptions.workspaceDirectory undefined (type *migrateOptions has no field or method workspaceDirectory)
); err != nil {
Expand All @@ -84,7 +89,7 @@ func Migrate(
}
}
if migrateOptions.dryRun {
return migrator.migrateAsDryRun(ctx, migrateOptions.dryRunWriter)
return migrator.migrateAsDryRun(ctx)
}
return migrator.migrate(ctx)
}
Expand All @@ -94,52 +99,52 @@ type MigrateOption func(*migrateOptions)

// MigrateAsDryRun write to the writer the summary of the changes to be made,
// without writing to the disk.
func MigrateAsDryRun(writer io.Writer) MigrateOption {
func MigrateAsDryRun() MigrateOption {
return func(migrateOptions *migrateOptions) {
migrateOptions.dryRun = true
migrateOptions.dryRunWriter = writer
}
}

// MigrateWorkspaceDirectory migrates buf.work.yaml, and all buf.yamls in directories
// pointed to by this workspace, as well as their co-resident buf.locks.
func MigrateWorkspaceDirectory(directory string) (MigrateOption, error) {
// TODO: Looking at IsLocal's doc, it seems to validate for what we want: relative and does not jump context.
if !filepath.IsLocal(directory) {
return nil, fmt.Errorf("%s is not a relative path", directory)
}
return func(migrateOptions *migrateOptions) {
migrateOptions.workspaceDirectory = filepath.Clean(directory)
}, nil
}
// TODO: move this validaton inside Migrate

// MigrateModuleDirectories migrates buf.yamls buf.locks in directories.
func MigrateModuleDirectories(directories []string) (MigrateOption, error) {
for _, path := range directories {
if !filepath.IsLocal(path) {
return nil, fmt.Errorf("%s is not a relative path", path)
}
}
return func(migrateOptions *migrateOptions) {
migrateOptions.moduleDirectories = slicesext.Map(directories, filepath.Clean)
}, nil
}
//// MigrateWorkspaceDirectory migrates buf.work.yaml, and all buf.yamls in directories
//// pointed to by this workspace, as well as their co-resident buf.locks.
//func MigrateWorkspaceDirectory(directory string) (MigrateOption, error) {
//// TODO: Looking at IsLocal's doc, it seems to validate for what we want: relative and does not jump context.
//if !filepath.IsLocal(directory) {
//return nil, fmt.Errorf("%s is not a relative path", directory)
//}
//return func(migrateOptions *migrateOptions) {
//migrateOptions.workspaceDirectory = filepath.Clean(directory)
//}, nil
//}

// MigrateGenerationTemplates migrates buf.gen.yamls.
func MigrateGenerationTemplates(paths []string) MigrateOption {
return func(migrateOptions *migrateOptions) {
migrateOptions.bufGenYAMLPaths = slicesext.Map(paths, filepath.Clean)
}
}
//// MigrateModuleDirectories migrates buf.yamls buf.locks in directories.
//func MigrateModuleDirectories(directories []string) (MigrateOption, error) {
//for _, path := range directories {
//if !filepath.IsLocal(path) {
//return nil, fmt.Errorf("%s is not a relative path", path)
//}
//}
//return func(migrateOptions *migrateOptions) {
//migrateOptions.moduleDirectories = slicesext.Map(directories, filepath.Clean)
//}, nil
//}

//// MigrateGenerationTemplates migrates buf.gen.yamls.
//func MigrateGenerationTemplates(paths []string) MigrateOption {
//return func(migrateOptions *migrateOptions) {
//migrateOptions.bufGenYAMLPaths = slicesext.Map(paths, filepath.Clean)
//}
//}

/// *** PRIVATE ***

type migrateOptions struct {
dryRun bool
dryRunWriter io.Writer
workspaceDirectory string
moduleDirectories []string
bufGenYAMLPaths []string
dryRun bool
//workspaceDirectory string
//moduleDirectories []string
//bufGenYAMLPaths []string
}

func newMigrateOptions() *migrateOptions {
Expand Down
22 changes: 0 additions & 22 deletions private/buf/bufmigrate/bufmigrate_test.go

This file was deleted.

66 changes: 39 additions & 27 deletions private/buf/bufmigrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ import (
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/syserror"
"go.uber.org/multierr"
"go.uber.org/zap"
)

type migrator struct {
logger *zap.Logger
messageWriter io.Writer
clientProvider bufapi.ClientProvider
// the bucket at "."
rootBucket storage.ReadWriteBucket
Expand All @@ -62,13 +61,14 @@ type migrator struct {
}

func newMigrator(
logger *zap.Logger,
// usually stderr
messageWriter io.Writer,
clientProvider bufapi.ClientProvider,
rootBucket storage.ReadWriteBucket,
destinationDir string,
) *migrator {
return &migrator{
logger: logger,
messageWriter: messageWriter,
clientProvider: clientProvider,
destinationDir: destinationDir,
rootBucket: rootBucket,
Expand All @@ -93,11 +93,12 @@ func (m *migrator) addBufGenYAML(
return err
}
if bufGenYAML.FileVersion() == bufconfig.FileVersionV2 {
m.logger.Sugar().Warnf("%s is already in v2", bufGenYAMLPath)
m.warnf("%s is a v2 file, no migration required", bufGenYAMLPath)
return nil
}
if typeConfig := bufGenYAML.GenerateConfig().GenerateTypeConfig(); typeConfig != nil && len(typeConfig.IncludeTypes()) > 0 {
m.logger.Sugar().Warnf(
// TODO: what does this sentence mean? Get someone else to read it and understand it without any explanation.
m.warnf(
"%s has types configuration and is migrated to v2 without it. To add these types your v2 configuration, first add an input and add these types to it.",
)
}
Expand All @@ -111,7 +112,8 @@ func (m *migrator) addBufGenYAML(
return nil
}

func (m *migrator) addWorkspace(
// TODO: document is workspaceDirectory always relative to root of bucket?
func (m *migrator) addWorkspaceDirectory(
ctx context.Context,
workspaceDirectory string,
) (retErr error) {
Expand All @@ -121,7 +123,7 @@ func (m *migrator) addWorkspace(
workspaceDirectory,
)
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("%q does not have a workspace configuration file", workspaceDirectory)
return fmt.Errorf("%q does not have a workspace configuration file (i.e. typically a buf.work.yaml)", workspaceDirectory)
}
if err != nil {
return err
Expand All @@ -135,7 +137,8 @@ func (m *migrator) addWorkspace(
return nil
}

// both buf.yaml and buf.lock
// both buf.yaml and buf.lock TODO what does this mean?
// TODO: document is workspaceDirectory always relative to root of bucket?
func (m *migrator) addModuleDirectory(
ctx context.Context,
// moduleDir is the relative path (relative to ".") to the module directory
Expand Down Expand Up @@ -212,7 +215,7 @@ func (m *migrator) addModuleDirectory(
// module configs, but they cannot share the same module full name.
moduleFullName := moduleConfig.ModuleFullName()
if len(moduleConfig.RootToExcludes()) > 1 && moduleFullName != nil {
m.logger.Sugar().Warnf(
m.warnf(
"%s has name %s and multiple roots. These roots are now separate unnamed modules.",
bufYAMLPath,
moduleFullName.String(),
Expand Down Expand Up @@ -288,7 +291,7 @@ func (m *migrator) addModuleDirectory(
}
m.moduleDependencies = append(m.moduleDependencies, bufYAML.ConfiguredDepModuleRefs()...)
case bufconfig.FileVersionV2:
m.logger.Sugar().Warnf("%s is already at v2", bufYAMLPath)
m.warnf("%s is already at v2", bufYAMLPath)
return nil
default:
return syserror.Newf("unexpected version: %v", bufYAML.FileVersion())
Expand Down Expand Up @@ -327,13 +330,9 @@ func (m *migrator) addModuleDirectory(
return nil
}

func (m *migrator) migrateAsDryRun(
ctx context.Context,
writer io.Writer,
) (retErr error) {
fmt.Fprintf(
writer,
"In an actual run, these files will be removed:\n%s\n\nThe following files will be overwritten or created:\n\n",
func (m *migrator) migrateAsDryRun(ctx context.Context) (retErr error) {
m.infof(
"In an actual run, these files will be removed:\n%s\n\nThe following files will be overwritten or created:\n",
strings.Join(slicesext.MapKeysToSortedSlice(m.filesToDelete), "\n"),
)
if len(m.moduleConfigs) > 0 {
Expand All @@ -345,9 +344,8 @@ func (m *migrator) migrateAsDryRun(
if err := bufconfig.WriteBufYAMLFile(&bufYAMLBuffer, migratedBufYAML); err != nil {
return err
}
fmt.Fprintf(
writer,
"%s:\n%s\n",
m.infof(
"%s:\n%s",
filepath.Join(m.destinationDir, bufconfig.DefaultBufYAMLFileName),
bufYAMLBuffer.String(),
)
Expand All @@ -356,9 +354,8 @@ func (m *migrator) migrateAsDryRun(
if err := bufconfig.WriteBufLockFile(&bufLockBuffer, migratedBufLock); err != nil {
return err
}
fmt.Fprintf(
writer,
"%s:\n%s\n",
m.infof(
"%s:\n%s",
filepath.Join(m.destinationDir, bufconfig.DefaultBufLockFileName),
bufLockBuffer.String(),
)
Expand All @@ -369,9 +366,8 @@ func (m *migrator) migrateAsDryRun(
if err := bufconfig.WriteBufGenYAMLFile(&bufGenYAMLBuffer, migratedBufGenYAML); err != nil {
return err
}
fmt.Fprintf(
writer,
"%s will be written:\n%s\n",
m.infof(
"%s will be written:\n%s",
bufGenYAMLPath,
bufGenYAMLBuffer.String(),
)
Expand Down Expand Up @@ -587,6 +583,22 @@ func (m *migrator) appendModuleConfig(moduleConfig bufconfig.ModuleConfig, paren
return nil
}

func (m *migrator) info(message string) {
_, _ = m.messageWriter.Write([]byte(fmt.Sprintf("%s\n", message)))
}

func (m *migrator) infof(format string, args ...any) {
_, _ = m.messageWriter.Write([]byte(fmt.Sprintf("%s\n", fmt.Sprintf(format, args...))))
}

func (m *migrator) warn(message string) {
_, _ = m.messageWriter.Write([]byte(fmt.Sprintf("Warning: %s\n", message)))
}

func (m *migrator) warnf(format string, args ...any) {
_, _ = m.messageWriter.Write([]byte(fmt.Sprintf("Warning: %s\n", fmt.Sprintf(format, args...))))
}

func resolvedDeclaredAndLockedDependencies(
moduleToRefToCommit map[string]map[string]*modulev1beta1.Commit,
commitIDToCommit map[string]*modulev1beta1.Commit,
Expand Down

0 comments on commit 942d5d8

Please sign in to comment.