Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support line comments in config #10592

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions config/serialize/serialize.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package fsrepo
package serialize

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/ipfs/kubo/config"

Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/serialize/serialize_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fsrepo
package serialize

import (
"os"
Expand Down
10 changes: 2 additions & 8 deletions plugin/loader/loader.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package loader

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading