diff --git a/cmd/migrate/go.mod b/cmd/migrate/go.mod new file mode 100644 index 000000000..b06b24df8 --- /dev/null +++ b/cmd/migrate/go.mod @@ -0,0 +1,11 @@ +module github.com/ipfs/boxo/cmd/migrate + +go 1.19 + +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/migrate/go.sum b/cmd/migrate/go.sum new file mode 100644 index 000000000..f003bf9e6 --- /dev/null +++ b/cmd/migrate/go.sum @@ -0,0 +1,8 @@ +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/migrate/internal/config.go b/cmd/migrate/internal/config.go new file mode 100644 index 000000000..a377ec687 --- /dev/null +++ b/cmd/migrate/internal/config.go @@ -0,0 +1,114 @@ +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/ipld/go-car": "github.com/ipfs/boxo/ipld/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/migrate/internal/json.go b/cmd/migrate/internal/json.go new file mode 100644 index 000000000..293c196f9 --- /dev/null +++ b/cmd/migrate/internal/json.go @@ -0,0 +1,23 @@ +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/migrate/internal/migrator.go b/cmd/migrate/internal/migrator.go new file mode 100644 index 000000000..e93495134 --- /dev/null +++ b/cmd/migrate/internal/migrator.go @@ -0,0 +1,168 @@ +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 +} + +// 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) { + exitCode, stdout, stderr, err := m.run("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)) + 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 +} diff --git a/cmd/migrate/migrate.go b/cmd/migrate/migrate.go new file mode 100644 index 000000000..af958a442 --- /dev/null +++ b/cmd/migrate/migrate.go @@ -0,0 +1,109 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" + + migrate "github.com/ipfs/boxo/cmd/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", + }, + }, + Action: func(clictx *cli.Context) error { + dryrun := clictx.Bool("dryrun") + configFile := clictx.String("config") + + migrator, err := buildMigrator(dryrun, configFile) + if err != nil { + return err + } + if err := migrator.UpdateImports(); err != nil { + return err + } + + 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 go-libipfs.", + "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/examples/go.mod b/examples/go.mod index cc896c060..b59fa9ca7 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -137,7 +137,7 @@ require ( golang.org/x/mod v0.7.0 // indirect golang.org/x/net v0.7.0 // indirect golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.5.0 // indirect + golang.org/x/sys v0.6.0 // indirect golang.org/x/text v0.7.0 // indirect golang.org/x/tools v0.3.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect diff --git a/examples/go.sum b/examples/go.sum index a76c6e7d4..453953be3 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -846,8 +846,8 @@ golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/gateway/handler_codec.go b/gateway/handler_codec.go index 6e071d5e1..c301de724 100644 --- a/gateway/handler_codec.go +++ b/gateway/handler_codec.go @@ -17,6 +17,12 @@ import ( mc "github.com/multiformats/go-multicodec" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" + + // Ensure basic codecs are registered. + _ "github.com/ipld/go-ipld-prime/codec/cbor" + _ "github.com/ipld/go-ipld-prime/codec/dagcbor" + _ "github.com/ipld/go-ipld-prime/codec/dagjson" + _ "github.com/ipld/go-ipld-prime/codec/json" ) // codecToContentType maps the supported IPLD codecs to the HTTP Content diff --git a/go.mod b/go.mod index f18ab4c73..d7c4faf91 100644 --- a/go.mod +++ b/go.mod @@ -76,7 +76,7 @@ require ( golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb golang.org/x/oauth2 v0.4.0 golang.org/x/sync v0.1.0 - golang.org/x/sys v0.5.0 + golang.org/x/sys v0.6.0 ) require ( diff --git a/go.sum b/go.sum index 048dd47a0..08ecc37f7 100644 --- a/go.sum +++ b/go.sum @@ -947,8 +947,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/namesys/namesys.go b/namesys/namesys.go index e341b63d7..d031064e6 100644 --- a/namesys/namesys.go +++ b/namesys/namesys.go @@ -20,7 +20,6 @@ import ( "time" lru "github.com/hashicorp/golang-lru" - iface "github.com/ipfs/boxo/coreiface" opts "github.com/ipfs/boxo/coreiface/options/namesys" "github.com/ipfs/boxo/path" "github.com/ipfs/go-cid" @@ -88,23 +87,6 @@ func WithDatastore(ds ds.Datastore) Option { } } -func loadStaticMap(list string) (map[string]path.Path, error) { - staticMap := make(map[string]path.Path) - for _, pair := range strings.Split(list, ",") { - mapping := strings.SplitN(pair, ":", 2) - key := mapping[0] - value := path.FromString(mapping[1]) - - ipnsKey, err := peer.Decode(key) - if err == nil { - key = iface.FormatKeyID(ipnsKey) - } - - staticMap[key] = value - } - return staticMap, nil -} - // NewNameSystem will construct the IPFS naming system based on Routing func NewNameSystem(r routing.ValueStore, opts ...Option) (NameSystem, error) { var staticMap map[string]path.Path @@ -114,11 +96,12 @@ func NewNameSystem(r routing.ValueStore, opts ...Option) (NameSystem, error) { // Example: // IPFS_NS_MAP="dnslink-test.example.com:/ipfs/bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am" if list := os.Getenv("IPFS_NS_MAP"); list != "" { - var err error - staticMap, err = loadStaticMap(list) - - if err != nil { - return nil, err + staticMap = make(map[string]path.Path) + for _, pair := range strings.Split(list, ",") { + mapping := strings.SplitN(pair, ":", 2) + key := mapping[0] + value := path.FromString(mapping[1]) + staticMap[key] = value } } @@ -232,7 +215,7 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts. cacheKey := key if err == nil { - cacheKey = iface.FormatKeyID(ipnsKey) + cacheKey = string(ipnsKey) } if p, ok := ns.cacheGet(cacheKey); ok { diff --git a/namesys/namesys_test.go b/namesys/namesys_test.go index a3f9df01a..52fce6794 100644 --- a/namesys/namesys_test.go +++ b/namesys/namesys_test.go @@ -88,32 +88,6 @@ func TestNamesysResolution(t *testing.T) { testResolution(t, r, "/ipns/bafzbeickencdqw37dpz3ha36ewrh4undfjt2do52chtcky4rxkj447qhdm", 1, "/ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n", ErrResolveRecursion) } -func TestNamesysResolutionWithCache(t *testing.T) { - nsMap := "dnslink-test.example.com:/ipfs/bafyaaeykceeaeeqlgiydemzngazc2mrtbimaw,12D3KooWQbpsnyzdBcxw6GUMbijV8WgXE4L8EtfnbcQWLfyxBKho:/ipfs/bafyaagakcyeaeeqqgiydemzngazc2mrtfvuxa3ttbimba,k51qzi5uqu5dkwkqm42v9j9kqcam2jiuvloi16g72i4i4amoo2m8u3ol3mqu6s:/ipfs/bafyaahikdmeaeeqvgiydemzngazc2mrtfvuxa3ttfvsgk5lybimbk" - - staticMap, err := loadStaticMap(nsMap) - if err != nil { - t.Fatal(err) - } - - r := &mpns{ - ipnsResolver: mockResolverOne(), - dnsResolver: mockResolverTwo(), - staticMap: staticMap, - } - - testResolution(t, r, "/ipns/dnslink-test.example.com", opts.DefaultDepthLimit, "/ipfs/bafyaaeykceeaeeqlgiydemzngazc2mrtbimaw", nil) - - testResolution(t, r, "/ipns/bafzaajaiaejcbw5i6oyqsktsn36r2vxgl2jzosyao46rybqztxt4rx4tfa3hpogg", opts.DefaultDepthLimit, "/ipfs/bafyaagakcyeaeeqqgiydemzngazc2mrtfvuxa3ttbimba", nil) - testResolution(t, r, "/ipns/k51qzi5uqu5dlnojhwrggtpty9c0cp5hvnkdozowth4eqb726jvoros8k9niyu", opts.DefaultDepthLimit, "/ipfs/bafyaagakcyeaeeqqgiydemzngazc2mrtfvuxa3ttbimba", nil) - testResolution(t, r, "/ipns/12D3KooWQbpsnyzdBcxw6GUMbijV8WgXE4L8EtfnbcQWLfyxBKho", opts.DefaultDepthLimit, "/ipfs/bafyaagakcyeaeeqqgiydemzngazc2mrtfvuxa3ttbimba", nil) - - testResolution(t, r, "/ipns/bafzaajaiaejcbpltl72da5f3y7ojrtsa7hsfn5bbnkjbkwyesziqqtdry6vjilku", opts.DefaultDepthLimit, "/ipfs/bafyaahikdmeaeeqvgiydemzngazc2mrtfvuxa3ttfvsgk5lybimbk", nil) - testResolution(t, r, "/ipns/k51qzi5uqu5dkwkqm42v9j9kqcam2jiuvloi16g72i4i4amoo2m8u3ol3mqu6s", opts.DefaultDepthLimit, "/ipfs/bafyaahikdmeaeeqvgiydemzngazc2mrtfvuxa3ttfvsgk5lybimbk", nil) - testResolution(t, r, "/ipns/12D3KooWNZuG8phqhoNK9KWcUhwfzA3biDKNCUNVWEaJgigr6Acj", opts.DefaultDepthLimit, "/ipfs/bafyaahikdmeaeeqvgiydemzngazc2mrtfvuxa3ttfvsgk5lybimbk", nil) - -} - func TestPublishWithCache0(t *testing.T) { dst := dssync.MutexWrap(ds.NewMapDatastore()) priv, _, err := ci.GenerateKeyPair(ci.RSA, 2048) diff --git a/version.json b/version.json index 9f6d6fca3..27325fd23 100644 --- a/version.json +++ b/version.json @@ -1,3 +1,3 @@ { - "version": "v0.7.0" + "version": "v0.8.0-rc2" }