From 711e6c6b2e741fb3514c4063cfce60f65edd737a Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Tue, 19 Nov 2024 10:05:54 -0800 Subject: [PATCH 1/2] feat: support line comments in config --- config/serialize/serialize.go | 33 ++++++++++++++++++++++++++++++++- plugin/loader/loader.go | 10 ++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/config/serialize/serialize.go b/config/serialize/serialize.go index 7cb479f6bad..2c7d6d1cf7c 100644 --- a/config/serialize/serialize.go +++ b/config/serialize/serialize.go @@ -1,12 +1,14 @@ package fsrepo 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 interface{}) error { f, err := os.Open(filename) @@ -27,7 +48,17 @@ func ReadConfigFile(filename string, cfg interface{}) 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/plugin/loader/loader.go b/plugin/loader/loader.go index 4ea3ac22600..47e52357543 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" + serialize "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 } From 109343977d29177a154cff0951dd67f2ebda84bf Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Tue, 19 Nov 2024 10:09:09 -0800 Subject: [PATCH 2/2] refactor: finish rename ?? I'm assuming this was a vestige of a prior refactor. Finishes the renaming of the package to match folder structure and semantics. --- config/serialize/serialize.go | 2 +- config/serialize/serialize_test.go | 2 +- plugin/loader/loader.go | 2 +- repo/fsrepo/fsrepo.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/serialize/serialize.go b/config/serialize/serialize.go index 2c7d6d1cf7c..d9cb8d1fd1a 100644 --- a/config/serialize/serialize.go +++ b/config/serialize/serialize.go @@ -1,4 +1,4 @@ -package fsrepo +package serialize import ( "bufio" diff --git a/config/serialize/serialize_test.go b/config/serialize/serialize_test.go index bf4b8b16f70..2117ea21ead 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 47e52357543..9a6ca135357 100644 --- a/plugin/loader/loader.go +++ b/plugin/loader/loader.go @@ -12,7 +12,7 @@ import ( config "github.com/ipfs/kubo/config" "github.com/ipld/go-ipld-prime/multicodec" - serialize "github.com/ipfs/kubo/config/serialize" + "github.com/ipfs/kubo/config/serialize" "github.com/ipfs/kubo/core" "github.com/ipfs/kubo/core/coreapi" plugin "github.com/ipfs/kubo/plugin" diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index b21b555cf7d..a9fb4d1ac88 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -23,7 +23,7 @@ import ( lockfile "github.com/ipfs/go-fs-lock" logging "github.com/ipfs/go-log" 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"