diff --git a/config/serialize/serialize.go b/config/serialize/serialize.go index 37c6ed718..6b9c21ef3 100644 --- a/config/serialize/serialize.go +++ b/config/serialize/serialize.go @@ -1,12 +1,14 @@ -package fsrepo +package serialize import ( + "bufio" "encoding/json" "errors" "fmt" "io" "os" "path/filepath" + "strings" "github.com/ipfs/kubo/config" @@ -17,6 +19,25 @@ import ( // repo doesn't exist. var ErrNotInitialized = errors.New("ipfs not initialized, please run 'ipfs init'") +// removeCommentLines reads from the provided io.Reader, removes lines that +// start with "//", and writes the result to the provided io.Writer. +func removeCommentLines(r io.Reader, w io.Writer) error { + scanner := bufio.NewScanner(r) + writer := bufio.NewWriter(w) + defer writer.Flush() + + for scanner.Scan() { + line := scanner.Text() + trimmed := strings.TrimLeft(line, " ") + if !strings.HasPrefix(trimmed, "//") { + if _, err := writer.WriteString(line + "\n"); err != nil { + return err + } + } + } + return scanner.Err() +} + // ReadConfigFile reads the config from `filename` into `cfg`. func ReadConfigFile(filename string, cfg any) error { f, err := os.Open(filename) @@ -27,7 +48,17 @@ func ReadConfigFile(filename string, cfg any) error { return err } defer f.Close() - if err := json.NewDecoder(f).Decode(cfg); err != nil { + + // Remove line comments (any line that has `\s*//`) + r, w := io.Pipe() + go func() { + if err := removeCommentLines(f, w); err != nil { + w.CloseWithError(err) + return + } + w.Close() + }() + if err := json.NewDecoder(r).Decode(cfg); err != nil { return fmt.Errorf("failure to decode config: %w", err) } return nil diff --git a/config/serialize/serialize_test.go b/config/serialize/serialize_test.go index bf4b8b16f..2117ea21e 100644 --- a/config/serialize/serialize_test.go +++ b/config/serialize/serialize_test.go @@ -1,4 +1,4 @@ -package fsrepo +package serialize import ( "os" diff --git a/plugin/loader/loader.go b/plugin/loader/loader.go index 624907614..0e2173159 100644 --- a/plugin/loader/loader.go +++ b/plugin/loader/loader.go @@ -1,7 +1,6 @@ package loader import ( - "encoding/json" "errors" "fmt" "io" @@ -13,6 +12,7 @@ import ( config "github.com/ipfs/kubo/config" "github.com/ipld/go-ipld-prime/multicodec" + "github.com/ipfs/kubo/config/serialize" "github.com/ipfs/kubo/core" "github.com/ipfs/kubo/core/coreapi" plugin "github.com/ipfs/kubo/plugin" @@ -132,13 +132,7 @@ func readPluginsConfig(repoRoot string, userConfigFile string) (config.Plugins, return config.Plugins{}, err } - cfgFile, err := os.Open(cfgPath) - if err != nil { - return config.Plugins{}, err - } - defer cfgFile.Close() - - err = json.NewDecoder(cfgFile).Decode(&cfg) + err = serialize.ReadConfigFile(cfgPath, &cfg) if err != nil { return config.Plugins{}, err } diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index 41b8c6285..b13e6589b 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -24,7 +24,7 @@ import ( lockfile "github.com/ipfs/go-fs-lock" logging "github.com/ipfs/go-log/v2" config "github.com/ipfs/kubo/config" - serialize "github.com/ipfs/kubo/config/serialize" + "github.com/ipfs/kubo/config/serialize" "github.com/ipfs/kubo/misc/fsutil" "github.com/ipfs/kubo/repo/fsrepo/migrations" ma "github.com/multiformats/go-multiaddr"