This commit is contained in:
Marco Munizaga 2026-02-17 13:25:27 -08:00 committed by GitHub
commit 969964ddd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 12 deletions

View File

@ -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

View File

@ -1,4 +1,4 @@
package fsrepo
package serialize
import (
"os"

View File

@ -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
}

View File

@ -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"