@cryptotaxi247 / kubo / commits / 40bc2372f

go-ipfs-config: make it easier to detect an uninitialized repo

Steven Allen committed Aug 29, 2019 at 11:48 UTC 40bc2372f975792227f48a6ac2dc06f56470555c
1 file changed +7 -6
config/serialize/serialize.go
+7 -6
@@ -11,13 +11,19 @@ import (
11 "github.com/ipfs/go-ipfs-config"
12
13 "github.com/facebookgo/atomicfile"
14 - "github.com/ipfs/go-ipfs-util"
14 )
15
16 +// ErrNotInitialized is returned when we fail to read the config because the
17 +// repo doesn't exist.
18 +var ErrNotInitialized = errors.New("ipfs not initialized, please run 'ipfs init'")
19 +
20 // ReadConfigFile reads the config from `filename` into `cfg`.
21 func ReadConfigFile(filename string, cfg interface{}) error {
22 f, err := os.Open(filename)
23 if err != nil {
24 + if os.IsNotExist(err) {
25 + err = ErrNotInitialized
26 + }
27 return err
28 }
29 defer f.Close()
@@ -56,11 +62,6 @@ func encode(w io.Writer, value interface{}) error {
62
63 // Load reads given file and returns the read config, or error.
64 func Load(filename string) (*config.Config, error) {
59 - // if nothing is there, fail. User must run 'ipfs init'
60 - if !util.FileExists(filename) {
61 - return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
62 - }
63 -
65 var cfg config.Config
66 err := ReadConfigFile(filename, &cfg)
67 if err != nil {