diff --git a/CHANGELOG.md b/CHANGELOG.md index ba9221312..83cdb771c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,9 @@ The following emojis are used to highlight certain changes: ### Removed +- `cmd/boxo-migrate`: removed code for go-ipfs migration -- no longer needed. +- `cmd/deprecator`: removed code to deprecare relocated ipfs packages -- no longer needed. + ### Fixed ### Security diff --git a/cmd/boxo-migrate/boxomigrate.go b/cmd/boxo-migrate/boxomigrate.go deleted file mode 100644 index 94f82ab5e..000000000 --- a/cmd/boxo-migrate/boxomigrate.go +++ /dev/null @@ -1,164 +0,0 @@ -package main - -import ( - "fmt" - "log" - "os" - "path/filepath" - "strings" - - migrate "github.com/ipfs/boxo/cmd/boxo-migrate/internal" - "github.com/urfave/cli/v2" -) - -func loadConfig(configFile string) (migrate.Config, error) { - if configFile != "" { - f, err := os.Open(configFile) - if err != nil { - return migrate.Config{}, fmt.Errorf("opening config file: %w", err) - } - defer f.Close() - return migrate.ReadConfig(f) - } - return migrate.DefaultConfig, nil -} - -func buildMigrator(dryrun bool, configFile string) (*migrate.Migrator, error) { - config, err := loadConfig(configFile) - if err != nil { - return nil, err - } - dir, err := os.Getwd() - if err != nil { - return nil, fmt.Errorf("getting working dir: %w", err) - } - return &migrate.Migrator{ - DryRun: dryrun, - Dir: dir, - Config: config, - }, nil -} - -func main() { - app := &cli.App{ - Name: "migrate", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "config", - Usage: "a JSON config file", - }, - }, - Commands: []*cli.Command{ - { - Name: "update-imports", - Usage: "rewrites imports of the current module for go-libipfs repos", - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "dryrun", - }, - &cli.BoolFlag{ - Name: "force", - Usage: "run even if no .git folder is found", - }, - }, - Action: func(clictx *cli.Context) error { - dryrun := clictx.Bool("dryrun") - force := clictx.Bool("force") - configFile := clictx.String("config") - - migrator, err := buildMigrator(dryrun, configFile) - if err != nil { - return err - } - - fmt.Printf("\n\n") - - if !force { - p, err := os.Getwd() - if err != nil { - return fmt.Errorf("failed to fetch current working directory: %w", err) - } - - for { - g := filepath.Join(p, ".git") - _, err := os.Stat(g) - if err == nil { - break - } - newP := filepath.Dir(p) - if p == newP { - return fmt.Errorf(` -⚠️ Version Control System Check ⚠️ - -We couldn't locate a .git folder in any parent paths. We strongly recommend -using a Version Control System to help you easily compare and revert to a -previous state if needed, as this tool doesn't have an undo feature. - -If you're using a different VCS or like to live dangerously, you can bypass this -check by adding the --force flag.`) - } - p = newP - } - } - - if !dryrun { - err := migrator.GoGet("github.com/ipfs/boxo@v0.8.0") - 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 - }, - }, - { - Name: "check-dependencies", - Usage: "checks the current module for dependencies that have migrated to go-libipfs", - Action: func(clictx *cli.Context) error { - configFile := clictx.String("config") - - migrator, err := buildMigrator(false, configFile) - if err != nil { - return err - } - - deps, err := migrator.FindMigratedDependencies() - if err != nil { - return err - } - if len(deps) > 0 { - fmt.Println(strings.Join([]string{ - "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.", - "", - "Dependent module versions:", - "", - strings.Join(deps, "\n"), - }, "\n")) - } - return nil - }, - }, - }, - } - err := app.Run(os.Args) - if err != nil { - log.Fatal(err) - } -} diff --git a/cmd/boxo-migrate/go.mod b/cmd/boxo-migrate/go.mod deleted file mode 100644 index 904f9a9c6..000000000 --- a/cmd/boxo-migrate/go.mod +++ /dev/null @@ -1,11 +0,0 @@ -module github.com/ipfs/boxo/cmd/boxo-migrate - -go 1.24 - -require github.com/urfave/cli/v2 v2.25.1 - -require ( - github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect - github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect -) diff --git a/cmd/boxo-migrate/go.sum b/cmd/boxo-migrate/go.sum deleted file mode 100644 index f003bf9e6..000000000 --- a/cmd/boxo-migrate/go.sum +++ /dev/null @@ -1,8 +0,0 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= -github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/urfave/cli/v2 v2.25.1 h1:zw8dSP7ghX0Gmm8vugrs6q9Ku0wzweqPyshy+syu9Gw= -github.com/urfave/cli/v2 v2.25.1/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= -github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= -github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= diff --git a/cmd/boxo-migrate/internal/config.go b/cmd/boxo-migrate/internal/config.go deleted file mode 100644 index 1859a5de7..000000000 --- a/cmd/boxo-migrate/internal/config.go +++ /dev/null @@ -1,114 +0,0 @@ -package migrate - -import ( - "encoding/json" - "fmt" - "io" -) - -type Config struct { - ImportPaths map[string]string - Modules []string -} - -var DefaultConfig = Config{ - ImportPaths: map[string]string{ - "github.com/ipfs/go-bitswap": "github.com/ipfs/boxo/bitswap", - "github.com/ipfs/go-ipfs-files": "github.com/ipfs/boxo/files", - "github.com/ipfs/tar-utils": "github.com/ipfs/boxo/tar", - "github.com/ipfs/interface-go-ipfs-core": "github.com/ipfs/boxo/coreiface", - "github.com/ipfs/go-unixfs": "github.com/ipfs/boxo/ipld/unixfs", - "github.com/ipfs/go-pinning-service-http-client": "github.com/ipfs/boxo/pinning/remote/client", - "github.com/ipfs/go-path": "github.com/ipfs/boxo/path", - "github.com/ipfs/go-namesys": "github.com/ipfs/boxo/namesys", - "github.com/ipfs/go-mfs": "github.com/ipfs/boxo/mfs", - "github.com/ipfs/go-ipfs-provider": "github.com/ipfs/boxo/provider", - "github.com/ipfs/go-ipfs-pinner": "github.com/ipfs/boxo/pinning/pinner", - "github.com/ipfs/go-ipfs-keystore": "github.com/ipfs/boxo/keystore", - "github.com/ipfs/go-filestore": "github.com/ipfs/boxo/filestore", - "github.com/ipfs/go-ipns": "github.com/ipfs/boxo/ipns", - "github.com/ipfs/go-blockservice": "github.com/ipfs/boxo/blockservice", - "github.com/ipfs/go-ipfs-chunker": "github.com/ipfs/boxo/chunker", - "github.com/ipfs/go-fetcher": "github.com/ipfs/boxo/fetcher", - "github.com/ipfs/go-ipfs-blockstore": "github.com/ipfs/boxo/blockstore", - "github.com/ipfs/go-ipfs-posinfo": "github.com/ipfs/boxo/filestore/posinfo", - "github.com/ipfs/go-ipfs-util": "github.com/ipfs/boxo/util", - "github.com/ipfs/go-ipfs-ds-help": "github.com/ipfs/boxo/datastore/dshelp", - "github.com/ipfs/go-verifcid": "github.com/ipfs/boxo/verifcid", - "github.com/ipfs/go-ipfs-exchange-offline": "github.com/ipfs/boxo/exchange/offline", - "github.com/ipfs/go-ipfs-routing": "github.com/ipfs/boxo/routing", - "github.com/ipfs/go-ipfs-exchange-interface": "github.com/ipfs/boxo/exchange", - "github.com/ipfs/go-merkledag": "github.com/ipfs/boxo/ipld/merkledag", - "github.com/boxo/ipld/car": "github.com/ipld/go-car", - - // Pre Boxo rename - "github.com/ipfs/go-libipfs/gateway": "github.com/ipfs/boxo/gateway", - "github.com/ipfs/go-libipfs/bitswap": "github.com/ipfs/boxo/bitswap", - "github.com/ipfs/go-libipfs/files": "github.com/ipfs/boxo/files", - "github.com/ipfs/go-libipfs/tar": "github.com/ipfs/boxo/tar", - "github.com/ipfs/go-libipfs/coreiface": "github.com/ipfs/boxo/coreiface", - "github.com/ipfs/go-libipfs/unixfs": "github.com/ipfs/boxo/ipld/unixfs", - "github.com/ipfs/go-libipfs/pinning/remote/client": "github.com/ipfs/boxo/pinning/remote/client", - "github.com/ipfs/go-libipfs/path": "github.com/ipfs/boxo/path", - "github.com/ipfs/go-libipfs/namesys": "github.com/ipfs/boxo/namesys", - "github.com/ipfs/go-libipfs/mfs": "github.com/ipfs/boxo/mfs", - "github.com/ipfs/go-libipfs/provider": "github.com/ipfs/boxo/provider", - "github.com/ipfs/go-libipfs/pinning/pinner": "github.com/ipfs/boxo/pinning/pinner", - "github.com/ipfs/go-libipfs/keystore": "github.com/ipfs/boxo/keystore", - "github.com/ipfs/go-libipfs/filestore": "github.com/ipfs/boxo/filestore", - "github.com/ipfs/go-libipfs/ipns": "github.com/ipfs/boxo/ipns", - "github.com/ipfs/go-libipfs/blockservice": "github.com/ipfs/boxo/blockservice", - "github.com/ipfs/go-libipfs/chunker": "github.com/ipfs/boxo/chunker", - "github.com/ipfs/go-libipfs/fetcher": "github.com/ipfs/boxo/fetcher", - "github.com/ipfs/go-libipfs/blockstore": "github.com/ipfs/boxo/blockstore", - "github.com/ipfs/go-libipfs/filestore/posinfo": "github.com/ipfs/boxo/filestore/posinfo", - "github.com/ipfs/go-libipfs/util": "github.com/ipfs/boxo/util", - "github.com/ipfs/go-libipfs/datastore/dshelp": "github.com/ipfs/boxo/datastore/dshelp", - "github.com/ipfs/go-libipfs/verifcid": "github.com/ipfs/boxo/verifcid", - "github.com/ipfs/go-libipfs/exchange/offline": "github.com/ipfs/boxo/exchange/offline", - "github.com/ipfs/go-libipfs/routing": "github.com/ipfs/boxo/routing", - "github.com/ipfs/go-libipfs/exchange": "github.com/ipfs/boxo/exchange", - - // Unmigrated things - "github.com/ipfs/go-libipfs/blocks": "github.com/ipfs/go-block-format", - "github.com/ipfs/boxo/blocks": "github.com/ipfs/go-block-format", - }, - Modules: []string{ - "github.com/ipfs/go-bitswap", - "github.com/ipfs/go-ipfs-files", - "github.com/ipfs/tar-utils", - "gihtub.com/ipfs/go-block-format", - "github.com/ipfs/interface-go-ipfs-core", - "github.com/ipfs/go-unixfs", - "github.com/ipfs/go-pinning-service-http-client", - "github.com/ipfs/go-path", - "github.com/ipfs/go-namesys", - "github.com/ipfs/go-mfs", - "github.com/ipfs/go-ipfs-provider", - "github.com/ipfs/go-ipfs-pinner", - "github.com/ipfs/go-ipfs-keystore", - "github.com/ipfs/go-filestore", - "github.com/ipfs/go-ipns", - "github.com/ipfs/go-blockservice", - "github.com/ipfs/go-ipfs-chunker", - "github.com/ipfs/go-fetcher", - "github.com/ipfs/go-ipfs-blockstore", - "github.com/ipfs/go-ipfs-posinfo", - "github.com/ipfs/go-ipfs-util", - "github.com/ipfs/go-ipfs-ds-help", - "github.com/ipfs/go-verifcid", - "github.com/ipfs/go-ipfs-exchange-offline", - "github.com/ipfs/go-ipfs-routing", - "github.com/ipfs/go-ipfs-exchange-interface", - "github.com/ipfs/go-libipfs", - }, -} - -func ReadConfig(r io.Reader) (Config, error) { - var config Config - err := json.NewDecoder(r).Decode(&config) - if err != nil { - return Config{}, fmt.Errorf("reading and decoding config: %w", err) - } - return config, nil -} diff --git a/cmd/boxo-migrate/internal/json.go b/cmd/boxo-migrate/internal/json.go deleted file mode 100644 index 293c196f9..000000000 --- a/cmd/boxo-migrate/internal/json.go +++ /dev/null @@ -1,23 +0,0 @@ -package migrate - -import "path/filepath" - -type pkgJSON struct { - Dir string - GoFiles []string - IgnoredGoFiles []string - TestGoFiles []string - XTestGoFiles []string - CgoFiles []string -} - -func (p *pkgJSON) allSourceFiles() []string { - var files []string - lists := [...][]string{p.GoFiles, p.IgnoredGoFiles, p.TestGoFiles, p.CgoFiles, p.XTestGoFiles} - for _, l := range lists { - for _, f := range l { - files = append(files, filepath.Join(p.Dir, f)) - } - } - return files -} diff --git a/cmd/boxo-migrate/internal/migrator.go b/cmd/boxo-migrate/internal/migrator.go deleted file mode 100644 index c4c12d613..000000000 --- a/cmd/boxo-migrate/internal/migrator.go +++ /dev/null @@ -1,194 +0,0 @@ -package migrate - -import ( - "bufio" - "bytes" - "encoding/json" - "fmt" - "go/ast" - "go/format" - "go/parser" - "go/token" - "io" - "os" - "os/exec" - "strconv" - "strings" -) - -type Migrator struct { - DryRun bool - Dir string - Config Config -} - -func (m *Migrator) updateFileImports(filePath string) error { - fset := token.NewFileSet() - astFile, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments) - if err != nil { - return fmt.Errorf("parsing %q: %w", filePath, err) - } - - var fileChanged bool - - var errr error - ast.Inspect(astFile, func(n ast.Node) bool { - switch x := n.(type) { - case *ast.ImportSpec: - val, err := strconv.Unquote(x.Path.Value) - if err != nil { - errr = err - return false - } - // we take the first matching prefix, so you need to make sure you don't have ambiguous mappings - for from, to := range m.Config.ImportPaths { - if strings.HasPrefix(val, from) { - var newVal string - switch { - case len(val) == len(from): - newVal = to - case val[len(from)] != '/': - continue - default: - newVal = to + val[len(from):] - } - fmt.Printf("changing %s => %s in %s\n", x.Path.Value, newVal, filePath) - if !m.DryRun { - x.Path.Value = strconv.Quote(newVal) - fileChanged = true - } - } - } - } - return true - }) - if errr != nil { - return errr - } - - if !fileChanged { - return nil - } - - f, err := os.Create(filePath) - if err != nil { - return err - } - defer f.Close() - err = format.Node(f, fset, astFile) - if err != nil { - f.Close() - return fmt.Errorf("formatting %q: %w", filePath, err) - } - err = f.Close() - if err != nil { - return fmt.Errorf("closing %q: %w", filePath, err) - } - - return nil -} - -func (m *Migrator) run(cmdName string, args ...string) (int, string, string, error) { - cmd := exec.Command(cmdName, args...) - stdout := &bytes.Buffer{} - stderr := &bytes.Buffer{} - cmd.Stdout = stdout - cmd.Stderr = stderr - cmd.Dir = m.Dir - err := cmd.Start() - if err != nil { - return 0, "", "", fmt.Errorf("running %s %v: %w", cmdName, args, err) - } - state, err := cmd.Process.Wait() - if err != nil { - return 0, "", "", fmt.Errorf("waiting for %s %v: %w", cmdName, args, 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 - for _, mod := range m.Config.Modules { - exitCode, stdout, stderr, err := m.run("go", "list", "-m", mod) - if err != nil { - return nil, err - } - if exitCode == 0 { - scanner := bufio.NewScanner(strings.NewReader(stdout)) - for scanner.Scan() { - modVersions = append(modVersions, scanner.Text()) - } - } else { - if !strings.Contains(stderr, "not a known dependency") { - return nil, fmt.Errorf("non-zero exit code %d finding if current module depends on %q, stderr:\n%s", exitCode, mod, stderr) - } - } - } - return modVersions, nil -} - -func (m *Migrator) findSourceFiles() ([]string, error) { - stdout, err := m.runOrErr("go", "list", "-json", "./...") - if err != nil { - return nil, fmt.Errorf("finding source files: %w", err) - } - - var files []string - dec := json.NewDecoder(strings.NewReader(stdout)) - for { - var pkg pkgJSON - err = dec.Decode(&pkg) - if err == io.EOF { - return files, nil - } - if err != nil { - return nil, fmt.Errorf("decoding 'go list' JSON: %w", err) - } - files = append(files, pkg.allSourceFiles()...) - } -} - -// UpdateImports rewrites the imports of the current module for any import paths that have been migrated to go-libipfs. -func (m *Migrator) UpdateImports() error { - sourceFiles, err := m.findSourceFiles() - if err != nil { - return err - } - for _, sourceFile := range sourceFiles { - err := m.updateFileImports(sourceFile) - if err != nil { - return fmt.Errorf("updating imports in %q: %w", sourceFile, err) - } - } - 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 -} diff --git a/cmd/boxo-migrate/staticcheck.conf b/cmd/boxo-migrate/staticcheck.conf deleted file mode 100644 index 4282b2d9e..000000000 --- a/cmd/boxo-migrate/staticcheck.conf +++ /dev/null @@ -1 +0,0 @@ -checks = ["-ST1005"] diff --git a/cmd/deprecator/go.mod b/cmd/deprecator/go.mod deleted file mode 100644 index b0fded707..000000000 --- a/cmd/deprecator/go.mod +++ /dev/null @@ -1,17 +0,0 @@ -module github.com/ipfs/boxo/cmd/deprecator - -go 1.24 - -require ( - github.com/dave/dst v0.27.2 - github.com/urfave/cli/v2 v2.25.3 -) - -require ( - github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect - github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect - golang.org/x/tools v0.1.12 // indirect -) diff --git a/cmd/deprecator/go.sum b/cmd/deprecator/go.sum deleted file mode 100644 index 63ff39864..000000000 --- a/cmd/deprecator/go.sum +++ /dev/null @@ -1,20 +0,0 @@ -github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= -github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/dave/dst v0.27.2 h1:4Y5VFTkhGLC1oddtNwuxxe36pnyLxMFXT51FOzH8Ekc= -github.com/dave/dst v0.27.2/go.mod h1:jHh6EOibnHgcUW3WjKHisiooEkYwqpHLBSX1iOBhEyc= -github.com/dave/jennifer v1.5.0 h1:HmgPN93bVDpkQyYbqhCHj5QlgvUkvEOzMyEvKLgCRrg= -github.com/dave/jennifer v1.5.0/go.mod h1:4MnyiFIlZS3l5tSDn8VnzE6ffAhYBMB2SZntBsZGUok= -github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= -github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= -github.com/urfave/cli/v2 v2.25.3 h1:VJkt6wvEBOoSjPFQvOkv6iWIrsJyCrKGtCtxXWwmGeY= -github.com/urfave/cli/v2 v2.25.3/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= -github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= -github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= diff --git a/cmd/deprecator/main.go b/cmd/deprecator/main.go deleted file mode 100644 index 02dd2ae04..000000000 --- a/cmd/deprecator/main.go +++ /dev/null @@ -1,186 +0,0 @@ -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "go/parser" - "go/token" - "io" - "io/fs" - "log" - "os" - "os/exec" - "path/filepath" - "strings" - - "github.com/dave/dst" - "github.com/dave/dst/decorator" - "github.com/urfave/cli/v2" -) - -func main() { - app := &cli.App{ - Name: "deprecator", - Usage: "Adds deprecation comments to all exported types in the module, with pointers to a new location. You should run this in your module root.", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "path", - Usage: "the new package path that the deprecated message should point users to", - Required: true, - }, - }, - Action: func(ctx *cli.Context) error { - newPkgPath := ctx.String("path") - wd, err := os.Getwd() - if err != nil { - return fmt.Errorf("getting working dir: %w", err) - } - fileToPackage, err := buildFileToPackage(wd) - if err != nil { - return fmt.Errorf("building mapping of files to packages: %w", err) - } - - modPath, err := getModulePath(wd) - if err != nil { - return fmt.Errorf("finding current module path: %w", err) - } - - fset := token.NewFileSet() - return filepath.Walk(wd, func(path string, info fs.FileInfo, err error) error { - if err != nil { - return err - } - if info.IsDir() || filepath.Ext(path) != ".go" { - return nil - } - - addComment := func(name string, decs *dst.Decorations) { - oldPkg := fileToPackage[path] - newPkg := strings.Replace(oldPkg, modPath, newPkgPath, 1) - newSym := newPkg + "." + name - comment := fmt.Sprintf("// Deprecated: use %s", newSym) - if len(decs.All()) > 0 { - decs.Append("//") - } - decs.Append(comment) - } - - file, err := decorator.ParseFile(fset, path, nil, parser.ParseComments) - if err != nil { - return fmt.Errorf("parsing %s: %w", path, err) - } - - if _, ok := fileToPackage[path]; !ok { - // this happens in the case of e.g. test files, which we want to skip - return nil - } - - // process the AST, adding comments where necessary - dst.Inspect(file, func(n dst.Node) bool { return inspectASTNode(addComment, n) }) - - outFile, err := os.Create(path) - if err != nil { - return fmt.Errorf("creating %s to write: %w", path, err) - } - defer outFile.Close() - err = decorator.Fprint(outFile, file) - if err != nil { - return fmt.Errorf("writing %s: %w", path, err) - } - - return nil - }) - }, - } - err := app.Run(os.Args) - if err != nil { - log.Fatal(err) - } -} - -type pkg struct { - Dir string - ImportPath string - GoFiles []string -} - -func inspectASTNode(addComment func(string, *dst.Decorations), n dst.Node) bool { - switch x := n.(type) { - case *dst.GenDecl: - if x.Tok == token.CONST || x.Tok == token.VAR || x.Tok == token.TYPE { - for _, spec := range x.Specs { - switch s := spec.(type) { - case *dst.ValueSpec: - // if parenthesized, put a comment above each exported type in the group - if x.Lparen { - for _, name := range s.Names { - if !name.IsExported() { - continue - } - addComment(name.Name, &s.Decs.Start) - } - } else { - name := s.Names[0] - if !name.IsExported() { - continue - } - addComment(name.Name, &x.Decs.Start) - } - case *dst.TypeSpec: - name := s.Name - if !name.IsExported() { - continue - } - addComment(name.Name, &x.Decs.Start) - } - } - } - case *dst.FuncDecl: - // don't add notices to methods - if x.Name.IsExported() && x.Recv == nil { - addComment(x.Name.Name, &x.Decs.Start) - } - } - return true -} - -func getModulePath(dir string) (string, error) { - cmd := exec.Command("go", "list", "-m") - cmd.Dir = dir - stdout := &bytes.Buffer{} - cmd.Stdout = stdout - err := cmd.Run() - if err != nil { - return "", err - } - return strings.TrimSpace(stdout.String()), nil -} - -func buildFileToPackage(dir string) (map[string]string, error) { - cmd := exec.Command("go", "list", "-json", "./...") - cmd.Dir = dir - stdout := &bytes.Buffer{} - stderr := &bytes.Buffer{} - cmd.Stdout = stdout - cmd.Stderr = stderr - err := cmd.Run() - if err != nil { - return nil, err - } - dec := json.NewDecoder(stdout) - fileToPackage := map[string]string{} - for { - var p pkg - err := dec.Decode(&p) - if err == io.EOF { - return fileToPackage, nil - } - if err != nil { - return nil, err - } - for _, f := range p.GoFiles { - fileToPackage[filepath.Join(p.Dir, f)] = p.ImportPath - } - } -}