diff --git a/cmd/boxo-migrate/boxo-migrate b/cmd/boxo-migrate/boxo-migrate new file mode 100755 index 000000000..71ba1fe60 Binary files /dev/null and b/cmd/boxo-migrate/boxo-migrate differ diff --git a/cmd/migrate/migrate.go b/cmd/boxo-migrate/boxomigrate.go similarity index 78% rename from cmd/migrate/migrate.go rename to cmd/boxo-migrate/boxomigrate.go index af958a442..287620358 100644 --- a/cmd/migrate/migrate.go +++ b/cmd/boxo-migrate/boxomigrate.go @@ -6,7 +6,7 @@ import ( "os" "strings" - migrate "github.com/ipfs/boxo/cmd/migrate/internal" + migrate "github.com/ipfs/boxo/cmd/boxo-migrate/internal" "github.com/urfave/cli/v2" ) @@ -64,10 +64,31 @@ func main() { if err != nil { return err } + + fmt.Printf("\n\n") + + if !dryrun { + err := migrator.GoGet("github.com/ipfs/boxo@v0.8.0-rc3") + if err != nil { + return err + } + } + if err := migrator.UpdateImports(); err != nil { return err } + if dryrun { + return nil + } + + if err := migrator.GoModTidy(); err != nil { + return err + } + + fmt.Printf("Your code has been successfully updated. Note that you might still need to manually fix up parts of your code.\n\n") + fmt.Printf("You should also consider running the 'boxo-migrate check-dependencies' command to see if you have any other dependencies on migrated code.\n\n") + return nil }, }, @@ -88,7 +109,7 @@ func main() { } if len(deps) > 0 { fmt.Println(strings.Join([]string{ - "You still have dependencies on repos which have migrated to go-libipfs.", + "You still have dependencies on repos which have migrated to Boxo.", "You should consider not having these dependencies to avoid multiple versions of the same code.", "You can use 'go mod why' or 'go mod graph' to find the reason for these dependencies.", "", diff --git a/cmd/migrate/go.mod b/cmd/boxo-migrate/go.mod similarity index 84% rename from cmd/migrate/go.mod rename to cmd/boxo-migrate/go.mod index b06b24df8..0d6a9cfa4 100644 --- a/cmd/migrate/go.mod +++ b/cmd/boxo-migrate/go.mod @@ -1,4 +1,4 @@ -module github.com/ipfs/boxo/cmd/migrate +module github.com/ipfs/boxo/cmd/boxo-migrate go 1.19 diff --git a/cmd/migrate/go.sum b/cmd/boxo-migrate/go.sum similarity index 100% rename from cmd/migrate/go.sum rename to cmd/boxo-migrate/go.sum diff --git a/cmd/migrate/internal/config.go b/cmd/boxo-migrate/internal/config.go similarity index 100% rename from cmd/migrate/internal/config.go rename to cmd/boxo-migrate/internal/config.go diff --git a/cmd/migrate/internal/json.go b/cmd/boxo-migrate/internal/json.go similarity index 100% rename from cmd/migrate/internal/json.go rename to cmd/boxo-migrate/internal/json.go diff --git a/cmd/migrate/internal/migrator.go b/cmd/boxo-migrate/internal/migrator.go similarity index 83% rename from cmd/migrate/internal/migrator.go rename to cmd/boxo-migrate/internal/migrator.go index e93495134..c4c12d613 100644 --- a/cmd/migrate/internal/migrator.go +++ b/cmd/boxo-migrate/internal/migrator.go @@ -106,6 +106,17 @@ func (m *Migrator) run(cmdName string, args ...string) (int, string, string, err return state.ExitCode(), strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), nil } +func (m *Migrator) runOrErr(cmdName string, args ...string) (string, error) { + exitCode, stdout, stderr, err := m.run(cmdName, args...) + if err != nil { + return "", err + } + if exitCode != 0 { + return "", fmt.Errorf("non-zero exit code %d, stderr:\n%s", exitCode, stderr) + } + return stdout, nil +} + // FindMigratedDependencies returns a list of dependent module versions like 'module v0.1.0' that have been migrated to go-libipfs. func (m *Migrator) FindMigratedDependencies() ([]string, error) { var modVersions []string @@ -129,13 +140,10 @@ func (m *Migrator) FindMigratedDependencies() ([]string, error) { } func (m *Migrator) findSourceFiles() ([]string, error) { - exitCode, stdout, stderr, err := m.run("go", "list", "-json", "./...") + stdout, err := m.runOrErr("go", "list", "-json", "./...") if err != nil { return nil, fmt.Errorf("finding source files: %w", err) } - if exitCode != 0 { - return nil, fmt.Errorf("non-zero exit code %d finding source files, stderr:\n%s", exitCode, stderr) - } var files []string dec := json.NewDecoder(strings.NewReader(stdout)) @@ -166,3 +174,21 @@ func (m *Migrator) UpdateImports() error { } return nil } + +func (m *Migrator) GoModTidy() error { + fmt.Printf("\n\nRunning 'go mod tidy'...\n\n") + _, err := m.runOrErr("go", "mod", "tidy") + if err != nil { + return fmt.Errorf("running 'go mod tidy': %w", err) + } + return nil +} + +func (m *Migrator) GoGet(mod string) error { + fmt.Printf("Adding module: %q\n\n", mod) + _, err := m.runOrErr("go", "get", mod) + if err != nil { + return fmt.Errorf("running 'go get %s': %w", mod, err) + } + return nil +}