From 40bc2372f975792227f48a6ac2dc06f56470555c Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 29 Aug 2019 11:48:53 -0700 Subject: [PATCH] go-ipfs-config: make it easier to detect an uninitialized repo --- config/serialize/serialize.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/config/serialize/serialize.go b/config/serialize/serialize.go index eedf7e1aa53..af11774b409 100644 --- a/config/serialize/serialize.go +++ b/config/serialize/serialize.go @@ -11,13 +11,19 @@ import ( "github.com/ipfs/go-ipfs-config" "github.com/facebookgo/atomicfile" - "github.com/ipfs/go-ipfs-util" ) +// ErrNotInitialized is returned when we fail to read the config because the +// repo doesn't exist. +var ErrNotInitialized = errors.New("ipfs not initialized, please run 'ipfs init'") + // ReadConfigFile reads the config from `filename` into `cfg`. func ReadConfigFile(filename string, cfg interface{}) error { f, err := os.Open(filename) if err != nil { + if os.IsNotExist(err) { + err = ErrNotInitialized + } return err } defer f.Close() @@ -56,11 +62,6 @@ func encode(w io.Writer, value interface{}) error { // Load reads given file and returns the read config, or error. func Load(filename string) (*config.Config, error) { - // if nothing is there, fail. User must run 'ipfs init' - if !util.FileExists(filename) { - return nil, errors.New("ipfs not initialized, please run 'ipfs init'") - } - var cfg config.Config err := ReadConfigFile(filename, &cfg) if err != nil {