mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
Merge 109343977d into c5776476bc
This commit is contained in:
commit
969964ddd4
@ -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
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package fsrepo
|
||||
package serialize
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user